我正在研究應該使矩形從上到下移動然后從左到右移動的程式,一旦矩形穿過框架的邊界,它應該從右到左和從下到上反轉其運動。我設法用一個簡單的 if 陳述句完成了第一部分,但我不知道如何扭轉它。
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
public class RectangleAnimation extends JFrame {
private static final int FRAME_WIDTH = 800;
private static final int FRAME_HEIGHT = 800;
public RectangleAnimation() {
setLayout(new BorderLayout( ));
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setTitle("Rectangle Animation 2D");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
PanelRectangleAnimation panel = new PanelRectangleAnimation();
add(panel, BorderLayout.CENTER);
pack();
setVisible(true);
}
public static void main(String[] args) {
new RectangleAnimation();
}
}
class PanelRectangleAnimation extends JPanel implements Runnable {
private static final int PANEL_WIDTH = 400;
private static final int PANEL_HEIGHT = 400;
private static final int RW = 60;
private static final int RH = 40;
int x, y;
Rectangle2D.Double rec;
Thread mythread;
public PanelRectangleAnimation() {
setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT));
this.setBackground(Color.white);
mythread = new Thread(this);
mythread.start();
}
public void run() {
// animation loop
boolean flag = true;
while (flag) {
try{
Thread.sleep(10);
}
catch(InterruptedException e){}
if(y == 355){
x = x 5;
}else{
y = y 5;
}
if ( x > PANEL_WIDTH || y > PANEL_HEIGHT) {
x = 0;
y = 0;
}
repaint();
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.red);
rec = new Rectangle2D.Double(x, y, RW, RH);
g2.fill(rec);
}
}
uj5u.com熱心網友回復:
因此,您的基本問題可以描述為基本的“碰撞檢測”問題。
基本概念是,如果盒子的邊緣之一超出可視區域,您想要反轉增量,例如......
box = delta;
if box > viewable_width {
box = viewable_width
delta *= -1; // reverse delta
}
現在,您還需要檢查左/上邊緣(即< 0),但基本思想是相同的。
Swing 也不是執行緒安全的,并且是單執行緒的。這意味著您不應該從事件調度執行緒的背景關系之外對 UI 或 UI 依賴的任何狀態進行更改,并且您不應該在事件調度的背景關系中執行任何長時間運行/阻塞操作線。
有關更多詳細資訊,請參閱Swing 中的并發以及如何使用 Swing 計時器以獲得可能/常見的解決方案
例如...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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 Timer timer;
private Rectangle box = new Rectangle(0, 0, 64, 40);
public TestPane() {
timer = new Timer(5, new ActionListener() {
private int xDelta = 1;
private int yDelta = 1;
@Override
public void actionPerformed(ActionEvent e) {
box.x = xDelta;
box.y = yDelta;
if (box.x box.width > getWidth()) {
box.x = getWidth() - box.width;
xDelta *= -1;
} else if (box.x < 0) {
box.x = 0;
xDelta *= -1;
}
if (box.y box.height > getHeight()) {
box.y = getHeight() - box.height;
yDelta *= -1;
} else if (box.y < 0) {
box.y = 0;
yDelta *= -1;
}
repaint();
}
});
}
@Override
public Dimension getPreferredSize() {
return new Dimension(800, 400);
}
@Override
public void addNotify() {
super.addNotify();
if (timer == null) {
return;
}
timer.start();
}
@Override
public void removeNotify() {
super.removeNotify();
if (timer == null) {
return;
}
timer.stop();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(Color.RED);
g2d.fill(box);
g2d.dispose();
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/449634.html
