應用類
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
public class Application extends JFrame {
public Circle c1,c2,c3;
public Application() {
c1 =new Circle(100,100,2,2,new int[] {100,100,150,150},Color.blue);
c2 =new Circle(150,150,-2,0,new int[] {50,150,150,150},Color.red);
c3 =new Circle(50,150,2,-2,new int[] {100,10,150,150},Color.green);
setSize(1000,1000);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(c1.getColor());
g.fillOval(c1.getX(), c1.getY(), 10, 10);
g.setColor(c2.getColor());
g.fillOval(c2.getX(), c2.getY(), 10, 10);
g.setColor(c3.getColor());
g.fillOval(c3.getX(), c3.getY(), 10, 10);
}
public static void main(String[] args) {
Application a = new Application();
new Application();
}
}
圓班
import java.awt.Color;
public class Circle extends Thread {
private int x,y;
private int xs,ys;
private Color color;
private int[] boundries;
public Circle(int x ,int y ,int xs,int ys,int[]boundries,Color color) {
this.x=x;
this.y=y;
this.xs=xs;
this.ys=ys;
this.boundries=boundries; //kordinatlar ve buyukluk
this.color=color;
}
@Override
public void run() {
while(true) {
move();
}
}
public void move() {
UpdateSpeedInformation();
this.x =this.xs; //xs art?? kordinat h?z?
this.y =this.ys;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void UpdateSpeedInformation() {
if(x<boundries[0] && x>boundries[2]) { //eger x 0.indexten kücük 2.indexten büyükse
this.xs *= -1;
this.ys *= -1;
//kontrol operat?rü bu
}
else if(y<boundries[1] && y>boundries[3]) {
this.xs=xs;
this.ys=ys; //..
}
}
public int getX() {
return x ;
}
public Color getColor() {
return color;
}
public int getY() {
return y;
}
}
這些是這三個圈子必須移動的課程,但我不知道應該在哪里放置執行緒啟動器問我是否需要更多資訊謝謝
這些是這三個圈子必須移動的課程,但我不知道應該在哪里放置執行緒啟動器問我是否需要更多資訊謝謝
這些是這三個圈子必須移動的課程,但我不知道應該在哪里放置執行緒啟動器問我是否需要更多資訊謝謝
uj5u.com熱心網友回復:
Swing 不是執行緒安全的。這意味著您不應該從事件調度執行緒的背景關系之外更新 UI 或更新 UI 依賴的任何狀態。
Swing 是單執行緒的。這意味著您不應在事件調度執行緒的背景關系中執行任何長時間運行或阻塞的操作。
有關更多詳細資訊,請參閱Swing 中的并發,以及如何使用 Swing 計時器以獲得通用解決方案。
我不知道應該在哪里放置執行緒啟動器
沒有可靠的方法來確定組件何時在螢屏上可見(至少以有意義的方式)。你可以看看,WindowListener但ComponentListener我發現它們在這種情況下并不太可靠。
我傾向于使用的一個技巧是覆寫add/removeNotify,雖然這不會告訴您組件何時在螢屏上可見,但它確實可以很好地指示組件何時可能進入“可用”狀態。
以下使用了一個簡單的 SwingTimer并修復了您的移動邏輯(圓圈不能同時小于和大于邊界框)
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private List<Circle> circles = new ArrayList<>(5);
private Timer timer;
public TestPane() {
circles.add(new Circle(100, 100, 2, 2, new int[]{100, 100, 150, 150}, Color.blue));
circles.add(new Circle(150, 150, -2, 0, new int[]{50, 150, 150, 150}, Color.red));
circles.add(new Circle(50, 150, 2, -2, new int[]{100, 10, 150, 150}, Color.green));
}
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
@Override
public void addNotify() {
super.addNotify();
if (timer != null) {
timer.stop();
}
timer = new Timer(5, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (Circle circle : circles) {
circle.tick();
}
repaint();
}
});
timer.start();
}
@Override
public void removeNotify() {
super.removeNotify();
if (timer != null) {
timer.stop();
}
timer = null;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
for (Circle circle : circles) {
g2d.setColor(circle.getColor());
g2d.fillOval(circle.getX(), circle.getY(), 10, 10);
}
g2d.dispose();
}
}
public class Circle {
private int x, y;
private int xs, ys;
private Color color;
private int[] boundries;
public Circle(int x, int y, int xs, int ys, int[] boundries, Color color) {
this.x = x;
this.y = y;
this.xs = xs;
this.ys = ys;
this.boundries = boundries; //kordinatlar ve buyukluk
this.color = color;
}
public void tick() {
move();
}
protected void move() {
updateSpeed();
this.x = this.xs; //xs art?? kordinat h?z?
this.y = this.ys;
}
protected void updateSpeed() {
if (x <= boundries[0]) {
xs *= -1;
x = boundries[0];
} else if (x >= boundries[2]) {
xs *= -1;
x = boundries[2];
}
if (y <= boundries[1]) {
ys *= -1;
y = boundries[1];
} else if (y >= boundries[3]) {
ys *= -1;
y = boundries[2];
}
}
public int getX() {
return x;
}
public Color getColor() {
return color;
}
public int getY() {
return y;
}
}
}
然后,您可以通過添加start和stop方法來擴展它,TestPane并使用 aWindowListener進一步觸發基于視窗狀態的操作。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/529408.html
標籤:爪哇多线程摇摆
下一篇:新執行緒上的C SIGABRT
