moveball.java

来自「是一个用于显示球的哦,呵呵,代码简短,功能均已实现,不可多得啊,」· Java 代码 · 共 102 行

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

public class moveball extends Applet implements Runnable
{
 /*public static void main(String[] args){*/
 int x_pos = 10;        
 int y_pos = 100;      
 int radius = 20;     
 
 private Image dbImage; 
 private Graphics dbg; 


 public void init()
 {
  setBackground (Color.blue);
 }

 public void start ()
 {
  
  Thread th = new Thread (this);
  
  th.start ();
 }

 public void stop()
 {

 }

 public void destroy()
 {

 }

 public void run ()
 {
        
  Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

            while (true) {
  
         
   x_pos++ ;
     
          
   repaint();

   try
   {
           
    Thread.sleep (20);
   }
   catch (InterruptedException ex)
   {
   }

   
   Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
  }
 }


 public void update (Graphics g) 
 { 
   
 if (dbImage == null) 
 { 
 dbImage = createImage (this.getSize().width, this.getSize().height); 
 dbg = dbImage.getGraphics (); 

 } 

   
 dbg.setColor (getBackground ()); 
 dbg.fillRect (0, 0, this.getSize().width, this.getSize().height); 

    
 dbg.setColor (getForeground()); 
 paint (dbg); 

    
 g.drawImage (dbImage, 0, 0, this); 

 } 


 public void paint (Graphics g)
 {
   
  g.setColor  (Color.red);
  
  g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
 }

}/*}*/

 

⌨️ 快捷键说明

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