似乎我的图形对象是最终的,因为错误说应该是我永远无法改变它。我一直在阅读将变量分配给最终变量,然后在我的定时器循环中使用它们以解决这个问题,但我甚至不知道如何开始接近图形对象。我是否需要将最终的图形对象复制回普通的图形对象?这是一些代码。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Graphics2D;
import java.awt.Graphics;
import java.util.ArrayList;
public class Test extends JPanel{
abstract class graphic {
public Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
public int[] location = new int[] {screenSize.width/2,screenSize.height/2};
}
public class gladiator extends graphic {
void draw(Graphics g) {
g.setColor(Color.green);
g.fillArc(location[0], location[1], 100, 100, 45, 90);
g.setColor(Color.black);
g.fillArc((location[0]+50-10),(location[1]+50-10), 20, 20, 0, 360);
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
gladiator[] gladiator = new gladiator[2];
ArrayList<gladiator> gladiatorList = new ArrayList<gladiator>();
for (int a =0; a < 2; a++) {
final gladiator[a] = new gladiator();
final gladiatorList.add(gladiator[a]);
}
new Timer(200, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (int a = 0; a < gladiatorList.size(); a++) {
gladiator[a].draw(g);
}
repaint();
System.out.println("repainting");
}
}).start();
}
public void setLocation(int x, int y){
//this.location[0] = x;
//this.location[1] = y;
}
public static void main(String[] args){
JFrame jf=new JFrame();
jf.setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
jf.setPreferredSize(Toolkit.getDefaultToolkit().getScreenSize());
jf.add(new Test());
jf.pack();
jf.setVisible(true);
}
}
这个位返回几乎所有for循环中的所有行都应该是最终的。
new Timer(200, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (int a = 0; a < gladiatorList.size(); a++) {
gladiator[a].draw(g);
}
repaint();
System.out.println("repainting");
}
}).start();
谢谢!
答案 0 :(得分:2)
要在同一方法中定义的匿名类中使用局部变量,必须使局部变量为final。
这不会阻止您修改引用变量指向的对象。
在您的情况下,您的匿名班级正在使用g
,gladiator
和gladiatorList
。所以最后标记所有这些:
protected void paintComponent( final Graphics g) {
...
final gladiator[] gladiator = new gladiator[2];
final ArrayList<gladiator> gladiatorList = new ArrayList<gladiator>();
答案 1 :(得分:1)
如果您创建变量final
,则意味着该变量将始终是对同一对象实例的引用。这并不意味着无法更改对象实例的内容。您不能为该变量分配新的引用,但您可以对实例本身执行任何操作(调用可能修改其状态的方法,读取/写入字段等)。
答案 2 :(得分:1)
你真的不应该在paintComponent
中使用计时器,只要操作系统感觉像绘制组件,它就会触发一个新的计时器。您只需将重新绘制sysout
更改为System.out.println("repainting in: " + this);
关于Graphics
变量的终结性:
final Graphics2D g2d = (Graphics2D) g.create();
在计时器内使用g2d
。
编辑: 一个完整的例子:
public class ExampleAnimationOfMyStuff extends JPanel {
MovingRectangle[] rectangles = new MovingRectangle[20];
public ExampleAnimationOfMyStuff() {
for (int i = 0; i < rectangles.length; i++) {
rectangles[i] = new MovingRectangle();
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Animated rectangles");
ExampleAnimationOfMyStuff anime = new ExampleAnimationOfMyStuff();
frame.getContentPane().add(anime);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
anime.animate();
frame.setVisible(true);
}
@Override
@Transient
public Dimension getPreferredSize() {
return new Dimension(1000, 1000);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (MovingRectangle rectangle : rectangles) {
g.setColor(rectangle.color);
g.fillRect(rectangle.x, rectangle.y, rectangle.width,
rectangle.height);
}
}
public void animate() {
new Timer(100, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (MovingRectangle rectangle : rectangles) {
rectangle.tick();
}
repaint();
System.out.println("repainting");
}
}).start();
}
public static class MovingRectangle extends Rectangle {
public static Random random = new Random();
int speedX, speedY;
Color color;
public void tick() {
if (getX() + speedX > 1000 || getX() + speedX < 0) {
speedX *= -1;
}
if (getY() + speedY > 1000 || getY() + speedY < 0) {
speedY *= -1;
}
setRect(getX() + speedX, getY() + speedY, getWidth(), getHeight());
}
public MovingRectangle() {
super(random.nextInt(1000), random.nextInt(1000), random
.nextInt(40), random.nextInt(40));
this.speedX = (random.nextDouble() > 0.5) ? 4 : -4;
this.speedY = (random.nextDouble() > 0.5) ? 4 : -4;
this.color = new Color(random.nextInt(256), random.nextInt(256),
random.nextInt(256));
}
}
}
上面的代码使用一组自定义对象(就像你有的那样)从paintcomponent中分离计时器,不需要声明最终结果,也可以消除你遇到的闪烁(由于新的计时器触发)。它在屏幕上绘制漂亮的矩形,可以移动;)