【发布时间】:2015-04-01 15:51:14
【问题描述】:
我有一个类,它通过计时器重新绘制动画对象(蠕虫状动画)。 另一个类有我的框架和面板。 当我创建该对象的 2 个(mov 和 mov2)实例并将其添加到面板时,它们会出现在单独的面板中(或看起来像)。这是代码。
public class Movimento extends JComponent{
int t;
int a;
int[][] matriz;
public Movimento(int tamanho, int area){
t = tamanho;
a = area;
gerarMatriz();
gerarPanel();
ActionListener counter = new ActionListener() {
public void actionPerformed(ActionEvent ev){
movimentarMatriz();
repaint();
}
};
new Timer(1, counter).start();
}
public void gerarPanel(){
this.setPreferredSize(new Dimension(a, a));
}
public void gerarMatriz(){
/*
*Generates an array[][] with initial coordinates
*/
}
public void movimentarMatriz(){
/*
* add a new coordinate to the last space of the array
*/
}
public void paintComponent(Graphics g){
super.paintComponent(g);
for(int i = 0; i < matriz.length; i++){
g.drawRect(matriz[i][0],matriz[i][1],1,1);
}
}
然后我在这里创建新的 Movimento 对象
public class GeraImg{
JFrame frame = new JFrame("Gera Imagem");
JPanel panel = new JPanel();
Movimento mov = new Movimento(1000,400);
Movimento mov2 = new Movimento(100,400);
public GeraImg(){
fazerFrame();
}
public void fazerFrame(){
panel.setOpaque(true);
panel.setBackground(new Color(150,200,20,255));
panel.add(mov2);
panel.add(mov);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args){
new GeraImg();
}
}
然后我并排得到 2 个单独的动画面板,而不是同一个面板内的 2 个蠕虫。
这个概念完全错误吗? 谢谢
【问题讨论】:
标签: java swing animation graphics jpanel