PHP Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.applet.AudioClip;
import java.io.*;
import java.net.*;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;
import java.applet.*;
import sun.audio.*;
import java.util.ArrayList;
public class go9 extends JFrame {
JTextArea displayArea;
JTextField typingArea;
static JPanel panel;
static Grp G1;
JFrame frame;
JButton button1;
JButton button2;
public static void main (String[] args) {
go9 gui = new go9();
gui.start();
}
public void start() {
frame = new JFrame();
G1= new Grp();
frame.repaint();
panel=new JPanel (new GridLayout(1,2));
button1 = new JButton("Buttons #1");
button1.addActionListener(new B1Listener());
button2 = new JButton("Button #2");
button2.addActionListener(new B2Listener());
panel.add(button1);
panel.add(button2);
frame.getContentPane().add(G1, BorderLayout.CENTER);
frame.getContentPane().add(panel, BorderLayout.PAGE_END);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setSize(400,400);
frame.setVisible(true);
// for (int i=0; i<10; i++) {G1.run();}
}
class B1Listener implements ActionListener {
public void actionPerformed (ActionEvent e) {G1.run();}
}
class B2Listener implements ActionListener {
public void actionPerformed (ActionEvent e) {System.out.println(e.getID());}
}
}
class Grp extends JPanel {
static int x=100;
static int y=100;
int rand;
void run() {
if (x==0 || x==getWidth() || y==0 || y==getHeight()) {
if (x==0) {
rand=(int)((Math.random())*2);
System.out.println(rand);
if (rand==0) {NW();}
if (rand==1) {SW();}
}
if (y==0) {
rand=(int)((Math.random())*2);
System.out.println(rand);
if (rand==0) {SW();}
if (rand==1) {SE();}
}
if (x==getWidth()) {
rand=(int)((Math.random())*2);
System.out.println(rand);
if (rand==0) {NE();}
if (rand==1) {SE();}
}
if (y==getHeight()) {
rand=(int)((Math.random())*2);
System.out.println(rand);
if (rand==0) {NW();}
if (rand==1) {NE();}
}
} else {
rand=(int)((Math.random())*4);
if (rand==0) {NE();}
if (rand==1) {NW();}
if (rand==2) {SE();}
if (rand==3) {SW();}
}
}
void SW() {
x++;
y++;
while (x>0 && x<getWidth() && y>0 && y<getHeight()) {
x++;
y++;
repaint();
try {
Thread.sleep(10);
} catch (Exception e) {}
}
}
void SE() {
x--;
y++;
while (x>0 && x<getWidth() && y>0 && y<getHeight()) {
x--;
y++;
repaint();
try {
Thread.sleep(10);
} catch (Exception e) {}
}
}
void NE() {
x--;
y--;
while (x>0 && x<getWidth() && y>0 && y<getHeight()) {
x--;
y--;
repaint();
try {
Thread.sleep(10);
} catch (Exception e) {}
}
}
void NW() {
x++;
y--;
while (x>0 && x<getWidth() && y>0 && y<getHeight()) {
x++;
y--;
repaint();
try {
Thread.sleep(10);
} catch (Exception e) {}
}
}
public void paintComponent (Graphics g) {
//System.out.println(x+" "+this.getWidth()+" and "+y+" "+this.getHeight());
g.setColor(Color.WHITE);
g.fillRect(0,0,this.getWidth(),this.getHeight());
g.setColor(Color.RED);
g.fillOval(x,y,30,30);
}
}