📄 thread2.java
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -