public void init() { this.setLayout(new BorderLayout()); frame.addMouseListener( new MouseAdapter() { public void mousePressed(MouseEvent e) { System.out.println("MousePressed"); pressed = true; } public void mouseReleased(MouseEvent e) { System.out.println("MouseReleased"); released = true; } public void mouseClicked(MouseEvent e) { System.out.println("MouseClicked!!!!"); clicked = true; } }); frame.addMouseMotionListener( new MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { System.out.println("MouseDragged--" + e); dragged = true; } public void mouseMoved(MouseEvent e) {} }); } // End init()
public void go() { frame = new Frame(); textField = new TextField(); frame.add(textField, BorderLayout.SOUTH); frame.addMouseListener(this); frame.addMouseMotionListener(this); frame.addMouseListener(new MyMouselistener()); frame.addMouseMotionListener(new MyMouselistener()); frame.addWindowListener(this); frame.setSize(400, 400); frame.setVisible(true); }
public MovingBalls() { f = new Frame(); b = new Button("ok"); b1 = new Button("go"); p = new Panel() { public void paint(Graphics g) { Color c1 = new Color( (int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255)); g.setColor(c1); g.fillOval(bx[0], by[0], 2 * br[0], 2 * br[0]); Color c2 = new Color( (int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255)); g.setColor(c2); g.fillOval(bx[1], by[1], 2 * br[1], 2 * br[1]); Color c3 = new Color( (int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255)); g.setColor(c3); g.fillOval(bx[2], by[2], 2 * br[2], 2 * br[2]); Color c4 = new Color( (int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255)); g.setColor(c4); g.fillOval(bx[3], by[3], 2 * br[3], 2 * br[3]); Color c5 = new Color( (int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255)); g.setColor(c5); g.fillOval(bx[4], by[4], 2 * br[4], 2 * br[4]); Color c6 = new Color( (int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255)); g.setColor(c6); g.fillOval(bx[5], by[5], 2 * br[5], 2 * br[5]); } }; p1 = new Panel(); f.add(p1, BorderLayout.SOUTH); f.add(p, BorderLayout.CENTER); p1.add(b); p1.add(b1); b.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { if (i < 6) { x = 100; y = 100; r = (int) (Math.random() * 50); bx[i] = x + (int) (Math.random() * 50); by[i] = y + (int) (Math.random() * 50); br[i] = r; i++; p.repaint(); } else if (i == 6) { i = 0; } } }); b1.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { while (true) { for (int l = 0; l < 6; l++) { bx[l] = bx[l] + dx_m[l]; by[l] = by[l] + dy_m[l]; if (by[l] > p.getHeight()) { dy_m[l] = -dy_m[l]; } if (bx[l] > p.getWidth()) { dx_m[l] = -dx_m[l]; } if (by[l] < 0) { dy_m[l] = -dy_m[l]; } if (bx[l] < 0) { dx_m[l] = -dx_m[l]; } } /* bx[0] = bx[0] + dx; by[0] = by[0] + dy; if(by[0]>p.getHeight() ) { dy=-dy; } if(bx[0]>p.getWidth()) { dx=-dx; } if(by[0]<0 ) { dy=-dy; } if(bx[0]<0) { dx=-dx; } */ /* bx[1] = bx[1] + dx1; by[1] = by[1] - dy1; if(by[1]>p.getHeight() ) { dy1=-dy1; } if(bx[1]>p.getWidth()) { dx1=-dx1; } if(by[1]<0 ) { dy1=-dy1; } if(bx[1]<0) { dx1=-dx1; } bx[2] = bx[2] - dx2; by[2] = by[2] + dy2; if(by[2]>p.getHeight() ) { dy2=-dy2; } if(bx[2]>p.getWidth()) { dx2=-dx2; } if(by[2]<0 ) { dy2=-dy2; } if(bx[2]<0) { dx2=-dx2; } bx[3] = bx[3] - dx3; by[3] = by[3] - dy3; if(by[3]>p.getHeight() ) { dy3=-dy3; } if(bx[3]>p.getWidth()) { dx3=-dx3; } if(by[3]<0 ) { dy3=-dy3; } if(bx[3]<0) { dx3=-dx3; } bx[4] = bx[4]; by[4] = by[4] - dy4; if(by[4]>p.getHeight() ) { dy4=-dy4; } if(bx[4]>p.getWidth()) { dx4=-dx4; } if(by[4]<0 ) { dy4=-dy4; } if(bx[4]<0) { dx4=-dx4; } bx[5] = bx[5] - dx5; by[5] = by[5] ; if(by[5]>p.getHeight() ) { dy5=-dy5; } if(bx[5]>p.getWidth()) { dx5=-dx5; } if(by[5]<0 ) { dy5=-dy5; } if(bx[5]<0) { dx5=-dx5; } */ for (int i = 0; i < 10000000; i++) {} p.update(p.getGraphics()); } } }); f.addWindowListener(this); f.addMouseMotionListener(this); p.addMouseListener(this); f.setSize(600, 600); f.setVisible(true); }