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

📄 threadex.java

📁 how to make a game using java.
💻 JAVA
字号:
// ThreadLines.java
// [Imperial Snowman Soft]

import java.awt.*;
import java.applet.*;
import GameLib.*;

public class ThreadEx extends Applet implements Runnable {

  Thread appletThread;
  Color lineColor;

  int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
  float red, green, blue;
  int times = 0;

  public void init()
  {
    times = Integer.parseInt(getParameter("times")); 
    setBackground(Color.black);
  }

  public void start()
  {
    if(appletThread==null)
    {
      appletThread = new Thread(this); //make a Thread of this Applet
      appletThread.start();	//and start it
    }
  }

  public void stop()
  {
    if(appletThread!=null)
    {
      appletThread.stop();	//stop the Thread
      appletThread = null;
    }
  }

  public void paint(Graphics g)
  {
    g.setColor(lineColor);
    g.drawString("Times to go: " + times, 10, 40);
    g.drawLine(x1,y1,x1,y2);
    g.drawLine(x2,y1,x2,y2);
    g.drawLine(x1,y1,x2,y2);
    g.drawLine(x2,y1,x1,y2);
  }

  public void run()
  {
    while(times>=0)
    {

      x1 = Misc.rnd(this.size().width);	//use our own GameLib method
      x2 = Misc.rnd(this.size().width);
      y1 = Misc.rnd(10, 20);		//the other version of the rnd - method
      y2 = Misc.rnd(this.size().height);

      red = (float)Math.random();
      green = (float)Math.random();
      blue = (float)Math.random();

      lineColor = new Color(red, green, blue); //create a new RGB color

      repaint();	//force a call of the paint() method

      try
      {
        appletThread.sleep(50);  //makes this Applet (Thread) sleeping for 50 msec
      }
      catch(InterruptedException e) {}
 
     times--;
    }
  }

}

⌨️ 快捷键说明

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