java - 添加/删除元素时创建新数组?

标签 java arrays swing arraylist paintcomponent

晚上好。我正在开发一个类似于旧游戏 LiteBrite 的程序,您将彩色钉子放在面板上,它就会亮起。在我的程序中,它的工作原理类似,当您单击面板时,它将创建一个新的椭圆(我将其命名为 ColorEllipse,它具有位置、大小和颜色的规范),并且它将存储它以供保存。目前它是一个数组列表,但我需要它位于常规数组中。有人告诉我,方法是创建一个新数组,并将旧数组的所有内容复制到新数组中。现在我使用数组列表,但不幸的是这个程序有规范,我们需要使用常规数组。

<小时/>
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.lang.reflect.Array;
import java.util.ArrayList;

public class LiteBritePanel extends javax.swing.JPanel{
    private final static int OFFSET = 5;
    private static int LINE_WIDTH = 2;
    private static int CELL_WIDTH = 25;


    public ArrayList <Colorable> _circles; // where ColorEllipses will be stored
    private ButtonPanel controlpanel; // used to set the color of peg that will be placed

    public LiteBritePanel() {
        this.setBackground(java.awt.Color.black);   

        _circles = new ArrayList<Colorable>();

        controlpanel = new ButtonPanel(this);
        this.addMouseListener(new MyMouseListener(this));
        this.add(controlpanel);
    }

    public void paintComponent(java.awt.Graphics aPaintBrush) {
        super.paintComponent(aPaintBrush);
        java.awt.Graphics2D pen = (java.awt.Graphics2D) aPaintBrush;

        java.awt.Color savedColor = pen.getColor();
        pen.setColor(java.awt.Color.black);
        for (int ball=0;ball<_circles.size();ball++)
            if(_circles.get(ball).isEmpty())
                return;
            else
                _circles.get(ball).fill(pen);
        pen.setColor(savedColor);
        this.repaint();
    }  


    public void mouseClicked(java.awt.event.MouseEvent e){
        boolean foundSquare = false;

        for (int ball=0; ball < _circles.size() && !foundSquare; ball++){
            if (_circles.get(ball).contains(e.getPoint()) == true){
                foundSquare = true;
                _circles.remove(ball);
                this.repaint();
            }
        }
    }

    private class MyMouseListener extends java.awt.event.MouseAdapter {
        private LiteBritePanel _this;
        public MyMouseListener(LiteBritePanel apanel){
            _this = apanel;
        }

        public void mouseClicked(java.awt.event.MouseEvent e){
            _circles.add(new ColorEllipse(controlpanel.getColor(), e.getPoint().x - (e.getPoint().x%CELL_WIDTH), e.getPoint().y - (e.getPoint().y%CELL_WIDTH), CELL_WIDTH-3,_this));
            _this.requestFocus();
            boolean foundSquare = false;
            for (int ball=0; ball < _circles.size() && !foundSquare; ball++){
                if (_circles.get(ball).contains(e.getPoint()) == true){
                    foundSquare = true;
                                // code for removing ball if one is placed
                    _this.repaint();
                }   
            }
            }
        }       
    }`
<小时/>

现在它被设置为 Arraylist,但我需要它按照此规范位于常规数组中。然后,当单击该面板时,它会在该特定位置的该数组中添加一个新的 ColorEllipse(并根据需要重新绘制以使其显示)。该程序的后续部分是当我触摸已经放置的钉子时,它会将其移除,但那是另一次了。现在我需要知道如何增加数组的大小并将其内容复制到其中。谁能告诉我应该改变什么?

最佳答案

要复制数组,您可以使用 System.arraycopy(...) 方法 ( System API ):

public static void arraycopy(
         Object src,
         int srcPos,
         Object dest,
         int destPos,
         int length)

首先创建一个目标数组,可能是源数组的两倍,然后传递旧数组、起始索引 (0)、新数组、目标起始索引 (0)、长度 (旧数组的长度),它应该完成其余的工作。

而且你不想在paintComponent内部调用repaint,相信我。请改用 Swing 计时器。 Google 可以帮助您找到这方面的一个很好的教程。

关于java - 添加/删除元素时创建新数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19779866/

相关文章:

java - 准备好的语句作为sql参数

java - 将引用对象添加到固定数组中

java - 我可以在弹出窗口中添加组合框和单选框吗?

java - PaintComponent() 绘制图形,但即使调用 repaint() 也不使用更新的值

java - 使用枚举时,Java 中未找到类错误

java - 将对象从 Activity 传递到 IntentService

java - 迷惑与势是有关系的

java - 使用 StringBuilder() 将数组解析为带标题的 CSV — 标题行问题

arrays - Fortran 字符数组

Java 将来自 URL 的图像添加到我的 JPanel