Java: 在同一个 JPanel 上添加 2 个动画对象 (paintComponent)
Java: Add 2 animated objects (paintComponent) on the same JPanel
我有一个 class 可以通过计时器重新绘制来创建动画对象(类似蠕虫的动画)。
另一个 class 有我的框架和面板。
当我创建此对象的 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 个蠕虫。
这个概念是不是完全错误?
谢谢
panel.add(mov2);
panel.add(mov);
默认情况下,JPanel 使用 FlowLayout,所以是的,您的两个组件区域并排添加。
您可以尝试使用:
panel.setLayout(null);
panel.add(mov2);
panel.add(mov);
这将允许组件在面板内随机放置。但是现在您负责在您的组件上使用 setSize(...) 和 setLocation(...) 以便组件可以在面板中正确绘制。
但是,更好的方法是创建自己的 class 来实现自己的绘画方法。类似于:
public class Movimento()
{
...
public void paintMe(Graphics g)
{
for(int i = 0; i < matriz.length; i++)
{
g.drawRect(matriz[i][0],matriz[i][1],1,1);
}
}
}
然后您将创建组件来绘制所有 Movimento 对象。类似于:
public class MovimentoAnimator extends JPanel
{
private List<Movimento> movimentos = new ArrayList<Movimento>();
public void addMovimento(Movimento movimento)
{
movimentos.add( movimento );
}
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
for (Movimento movimento: movimentos
{
movimento.paintMe( g );
}
}
}
这个 class 也将负责动画。
我有一个 class 可以通过计时器重新绘制来创建动画对象(类似蠕虫的动画)。 另一个 class 有我的框架和面板。 当我创建此对象的 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 个蠕虫。
这个概念是不是完全错误? 谢谢
panel.add(mov2);
panel.add(mov);
默认情况下,JPanel 使用 FlowLayout,所以是的,您的两个组件区域并排添加。
您可以尝试使用:
panel.setLayout(null);
panel.add(mov2);
panel.add(mov);
这将允许组件在面板内随机放置。但是现在您负责在您的组件上使用 setSize(...) 和 setLocation(...) 以便组件可以在面板中正确绘制。
但是,更好的方法是创建自己的 class 来实现自己的绘画方法。类似于:
public class Movimento()
{
...
public void paintMe(Graphics g)
{
for(int i = 0; i < matriz.length; i++)
{
g.drawRect(matriz[i][0],matriz[i][1],1,1);
}
}
}
然后您将创建组件来绘制所有 Movimento 对象。类似于:
public class MovimentoAnimator extends JPanel
{
private List<Movimento> movimentos = new ArrayList<Movimento>();
public void addMovimento(Movimento movimento)
{
movimentos.add( movimento );
}
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
for (Movimento movimento: movimentos
{
movimento.paintMe( g );
}
}
}
这个 class 也将负责动画。