【发布时间】:2015-03-25 14:14:39
【问题描述】:
在另一个程序上我遇到了同样的问题,所以我给了你我正在尝试用一个较小的类做的事情,这样你就可以更轻松地阅读代码。 基本上,我正在尝试改变 X 的位置,但它似乎只是变大了。
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.RepaintManager;
public class test extends JPanel{
public int x = 200;
public int y = 620;
Rectangle cube = new Rectangle(x, y, 80, 80);
int cubex = cube.x;
public void paint(Graphics g){
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.fill(cube);
}
public void moveCube(){
cube.setLocation(cubex -= 1, y);
cube.setSize(80, 80);
repaint();
}
public static void main(String[] args) throws InterruptedException {
test t = new test();
JFrame frame = new JFrame();
frame.add(t);
frame.setSize(700, 1000);
frame.setVisible(true);
frame.setTitle("The Cube");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
frame.setLocationRelativeTo(null);
frame.setBackground(new Color(240, 84, 84));
while(true){
t.moveCube();
Thread.sleep(10);
}
}
}
非常感谢您的帮助:)
【问题讨论】: