donghuaoval.java

来自「Java课程中所有的可运行程序 全部都是java课程中所涉及到的源码」· Java 代码 · 共 84 行

JAVA
84
字号
import java.awt.*;
import java.applet.*;

public class donghuaoval extends Applet implements Runnable{
   Color nowcolor=Color.black;

   public Thread runner;
   public int xpos,tag=0;
   public Image offscreenImg;
   public Graphics offscreenG;


    public void init(){
setBackground(Color.white);
setForeground(Color.white);
      xpos=50;
      offscreenImg=createImage(600,600);
      offscreenG=offscreenImg.getGraphics();

    }

     public void start() {
    if (runner == null); {
     runner = new Thread(this);
     runner.start();
    }
  }

  public void stop() {
    if (runner != null) {
         runner = null;
    }
  }

public void run() {

	int speed=200;

	  while(Thread.currentThread()==runner)
	  {

	         if(tag==0)
	          xpos=xpos+10;
	          else
	          xpos=xpos-10;

	          if(xpos==200)
	           tag=1;

	          if(xpos==20)
	           tag=0;
	  repaint();//no.1 statement
	   try { Thread.sleep(speed); }
	   catch (InterruptedException e) { }

	         }

 }


public void paint(Graphics g){
   offscreenG.setColor(Color.white);
   offscreenG.clearRect(xpos-20,xpos-20,250,250);
    int red,green,blue;

     red=(int)Math.floor(Math.random()*256);
 green=(int)Math.floor(Math.random()*256);
 blue=(int)Math.floor(Math.random()*256);
 offscreenG.setColor(new Color(red,green,blue));
   offscreenG.fillOval(xpos,xpos,xpos,xpos);
   g.drawImage(offscreenImg,0,0,this);

        }

    public void update(Graphics g){
        paint(g);

	}





}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?