Menu

java project:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;

public class Projects {
public static void main(String[] args) {
String[] options = {"IP Finder In Java","Create Animation App in Java","Puzzle Game In Java","Exit"};
while (true) {
int choice = JOptionPane.showOptionDialog(null,"Select a project to run","Project Submission",JOptionPane.DEFAULT_OPTION,JOptionPane.PLAIN_MESSAGE,null,options,options[0]);
if (choice == 0) {
IPFinder.showUI();
} else if (choice == 1) {
AnimationApp.showUI();
} else if (choice == 2) {
PuzzleGame.showUI();
} else {
System.exit(0);
}
}
}
static class IPFinder {
public static void showUI() {
JFrame f = new JFrame("IP Finder In Java - Project");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setSize(500,220);
f.setLayout(new BorderLayout());
JTextArea ta = new JTextArea();
ta.setEditable(false);
ta.setFont(new Font("Monospaced",Font.PLAIN,14));
f.add(new JScrollPane(ta),BorderLayout.CENTER);
JButton b = new JButton("Find IPs");
f.add(b,BorderLayout.SOUTH);
b.addActionListener(e -> {
ta.setText("");
try {
ta.append("Project: IP Finder In Java\n\n");
InetAddress local = InetAddress.getLocalHost();
ta.append("Local Host Name: "+local.getHostName()+"\n");
ta.append("Local IP Address: "+local.getHostAddress()+"\n\n");
} catch (Exception ex) {
ta.append("Local IP: Unable to determine\n\n");
}
try {
URL whatismyip = new URL("[https://api.ipify.org](https://api.ipify.org)");
BufferedReader in = new BufferedReader(new InputStreamReader(whatismyip.openStream()));
String publicIp = in.readLine();
ta.append("Public IP Address: "+publicIp+"\n");
in.close();
} catch (Exception ex) {
ta.append("Public IP: Unable to determine (network access required)\n");
}
});
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
static class AnimationApp {
public static void showUI() {
JFrame f = new JFrame("Create Animation App in Java - Project");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setSize(600,400);
AnimationPanel p = new AnimationPanel();
f.add(p);
JPanel controls = new JPanel();
JButton start = new JButton("Start");
JButton stop = new JButton("Stop");
JButton faster = new JButton("Faster");
JButton slower = new JButton("Slower");
controls.add(start);
controls.add(stop);
controls.add(faster);
controls.add(slower);
f.add(controls,BorderLayout.SOUTH);
start.addActionListener(a -> p.start());
stop.addActionListener(a -> p.stop());
faster.addActionListener(a -> p.faster());
slower.addActionListener(a -> p.slower());
f.setLocationRelativeTo(null);
f.setVisible(true);
}
static class AnimationPanel extends JPanel implements ActionListener {
int x = 0;
int y = 50;
int dx = 4;
int dy = 3;
int radius = 30;
javax.swing.Timer timer = new javax.swing.Timer(30,this);
Color bg = new Color(245,245,250);
public AnimationPanel() {
setBackground(bg);
}
public void start() { if (!timer.isRunning()) timer.start(); }
public void stop() { if (timer.isRunning()) timer.stop(); }
public void faster() { int d = Math.max(1,timer.getDelay()-5); timer.setDelay(d); }
public void slower() { timer.setDelay(timer.getDelay()+10); }
public void actionPerformed(ActionEvent e) {
x += dx;
y += dy;
if (x < 0 || x+radius*2 > getWidth()) dx = -dx;
if (y < 0 || y+radius*2 > getHeight()) dy = -dy;
repaint();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
GradientPaint gp = new GradientPaint(0,0,new Color(255,200,210),getWidth(),getHeight(),new Color(200,230,255));
g2.setPaint(gp);
g2.fillRect(0,0,getWidth(),getHeight());
g2.setColor(new Color(255,255,255,180));
g2.fillOval(x,y,radius*2,radius*2);
g2.setColor(new Color(100,100,200));
g2.setStroke(new BasicStroke(3));
g2.drawOval(x,y,radius*2,radius*2);
g2.setFont(new Font("SansSerif",Font.BOLD,14));
g2.drawString("Project: Create Animation App in Java",10,20);
}
}
}
static class PuzzleGame {
public static void showUI() {
JFrame f = new JFrame("Puzzle Game In Java - Project");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setSize(360,420);
PuzzlePanel p = new PuzzlePanel(3);
f.add(p);
JPanel bottom = new JPanel();
JButton shuffle = new JButton("Shuffle");
JButton solve = new JButton("Solve(Reset)");
bottom.add(shuffle);
bottom.add(solve);
f.add(bottom,BorderLayout.SOUTH);
shuffle.addActionListener(a -> p.shuffle());
solve.addActionListener(a -> p.reset());
f.setLocationRelativeTo(null);
f.setVisible(true);
}
static class PuzzlePanel extends JPanel {
int n;
JButton[] buttons;
int[] board;
int size;
public PuzzlePanel(int n) {
this.n = n;
setLayout(new GridLayout(n,n,3,3));
size = n*n;
buttons = new JButton[size];
board = new int[size];
for (int i=0;i<size;i++) board[i]=i;
for (int i=0;i<size;i++) {
buttons[i]=new JButton();
buttons[i].setFont(new Font("SansSerif",Font.BOLD,28));
final int idx=i;
buttons[i].addActionListener(e -> move(idx));
add(buttons[i]);
}
reset();
}
void reset() {
for (int i=0;i<size;i++) board[i]=i;
updateButtons();
}
void shuffle() {
List<Integer> list = new ArrayList<>();
for (int i=0;i<size;i++) list.add(i);
Collections.shuffle(list);
for (int i=0;i<size;i++) board[i]=list.get(i);
if (!isSolvable()) shuffle();
updateButtons();
}
boolean isSolvable() {
int inv=0;
for (int i=0;i<size;i++) for (int j=i+1;j<size;j++) if (board[i]!=0 && board[j]!=0 && board[i]>board[j]) inv++;
if (n%2==1) return inv%2==0;
int rowFromBottom = n - (indexOf(0)/n);
if (rowFromBottom%2==0) return inv%2==1;
else return inv%2==0;
}
int indexOf(int v) {
for (int i=0;i<size;i++) if (board[i]==v) return i;
return -1;
}
void move(int idx) {
int empty = indexOf(0);
int r1 = idx/n, c1 = idx%n;
int r2 = empty/n, c2 = empty%n;
if ((Math.abs(r1-r2)==1 && c1==c2) || (Math.abs(c1-c2)==1 && r1==r2)) {
int t = board[idx];
board[idx]=board[empty];
board[empty]=t;
updateButtons();
if (isSolved()) {
JOptionPane.showMessageDialog(this,"Congratulations! Puzzle solved.\nProject: Puzzle Game In Java");
}
}
}
boolean isSolved() {
for (int i=0;i<size-1;i++) if (board[i]!=i+1) return false;
return board[size-1]==0;
}
void updateButtons() {
for (int i=0;i<size;i++) {
if (board[i]==0) {
buttons[i].setText("");
buttons[i].setBackground(Color.LIGHT_GRAY);
} else {
buttons[i].setText(String.valueOf(board[i]));
buttons[i].setBackground(Color.WHITE);
}
}
}
}
}
}