thread2.java

来自「一些简单的java程序。老师很认真」· Java 代码 · 共 50 行

JAVA
50
字号
package com.resource;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;

import javax.swing.*;
public class Thread2 extends JPanel implements Runnable{
	private boolean stop;
	String fileName;
	String direction;
	int x;
	int y;
	public Thread2(boolean stop,String fileName,String direction,int x,int y){
		super();
		this.stop=stop;
		this.fileName=fileName;
		this.direction=direction;
		this.x=x;
	    this.y=y;
		}
	public  void paintComponent(Graphics g){
		super.paintComponent(g);
		Toolkit tk=Toolkit.getDefaultToolkit();
		Image img=tk.getImage(this.fileName);
		this.setLocation(x,y);
		g.drawImage(img,this.x,this.y,this);
	}
	public void run(){
		while (stop == false){
			try{
				Thread.sleep(100);
				if (direction=="ToLeft")
					x -= 10;
				else
					x+=10;
				repaint();
				System.out.println("----------x = " + x + " y = " + y);
				if (x> 800 || x<0)
					stop = true;
				}catch (Exception e){
					e.printStackTrace();
					}
				}
		}
}

⌨️ 快捷键说明

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