⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 example9_11.java

📁 不错的教程 适合中高级人员的使用
💻 JAVA
字号:
import java.awt.*;import java.awt.event.*;

public class Example9_11
{
	public static void main(String args[])
   {  MyFrame frame= new MyFrame();
	  frame.setBounds(10,10,500,500);
	  frame.setVisible(true);
	  frame.addWindowListener(new WindowAdapter()
                      {   public void windowClosing(WindowEvent e)
                           { System.exit(0);
                           }
                      }
                   );
}
}


 class MyFrame extends Frame implements Runnable
{  Thread 红色球,兰色球; 
   MyCanvas red,blue;
   double t=0;
   MyFrame()
   {  红色球=new Thread(this);
      兰色球=new Thread(this);
      red=new MyCanvas(Color.red);blue=new MyCanvas(Color.blue);
      setLayout(null);
	  add(red);
	  add(blue);
	  red.setLocation(60,100);
	  blue.setLocation(60,100);
	  红色球.start();
      兰色球.start();
   }
   
   public void run()
   {  while(true)
      {  t=t+0.2;
         if(t>20)  t=0;
         if(Thread.currentThread()==红色球) 
            {  int x=60;
		       int h=(int)(1.0/2*t*t*3.8)+60;
               red.setLocation(x,h);               
               try{  红色球.sleep(50);
                  }
               catch(InterruptedException e){}
             }
          else if(Thread.currentThread()==兰色球)
            {  int x=60+(int)(26*t);
              int h=(int)(1.0/2*t*t*3.8)+60;
		       blue.setLocation(x,h); 		      
               try{  兰色球.sleep(50);
                  }
               catch(InterruptedException e){}
            } 
      }   
   } 
}
class MyCanvas extends Canvas
{
	Color c;
	MyCanvas(Color c)
	{
		setSize(20,20);
		this.c=c;
	}
	public void paint(Graphics g)
	{
		g.setColor(c);
		g.fillOval(0,0,20,20);
	}
	
}

⌨️ 快捷键说明

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