日期:2014-05-20 浏览次数:20759 次
import java.applet.Applet; import java.awt.Color; import java.awt.Frame; import java.awt.Graphics; import java.awt.event.MouseEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.ArrayList; import java.util.List; import java.awt.*; import java.awt.event.*; import java.awt.Graphics; public class Main extends Applet implements Runnable,MouseListener{ int x = 60, y = 100; int dx = 5, dy = 5; int rgb = 0; Color color = new Color((int)(Math.random() * 0xFFFFFF)); public static void main(String[] args) { new Main(); } public Main(){ setSize(350, 350); setBackground(Color.gray); this.addMouseListener(this); this.setLayout(null); setVisible(true); } public void doColor() { rgb = (int)(Math.random()*0xFFFFFF); color = new Color(rgb); } public void run() { while (true) { if (x <= 0) { dx = -dx; dy = -dy; doColor(); }else if ((x + 50) >= this.getWidth()) { dx = -dx; dy = -dy; doColor(); } if (y <= 0) { dx = -dx; dy = -dy; doColor(); }else if ((y + 50) >= this.getHeight()){ dx = -dx; dy = -dy; doColor(); } x = x + dx; y = y + dy; try { Thread.sleep(50); repaint(); } catch (InterruptedException e) { e.printStackTrace(); } } } public void paint(Graphics g) { Color c = g.getColor(); g.setColor(color); g.fillOval(x, y, 50, 50); g.setColor(c); } @Override public void mouseClicked(MouseEvent e) { } @Override public void mousePressed(MouseEvent e){ System.out.print(this); new Thread(this).start(); } @Override public void mouseReleased(MouseEvent e) { } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { } }