java - 在 JPanel 或 JComponent 上绘制同心形状

标签 java swing jpanel paint jcomponent

我基本上是一个使用 swing 的新手。所以请耐心听我说。我可以使用 swing 库中的预制组件完成简单的 GUI 操作。然而,现在我正在尝试弄清楚如何在 JPanel 上绘制基本形状。在本例中,它是我递归收集的 Square 对象的集合,并且应该彼此同心地显示。

几周前,我们做了一个涉及绘制形状的较小项目,只不过这些形状是直接绘制到 JFrame 上的。现在,我尝试在 JPanel 或扩展 JComponent 的类中执行此操作,但我遇到了太多的障碍。此时,JPanel 上没有显示任何内容。

这是我迄今为止所上的类(class)。

方形类。这只是创建了一个简单的 Square

public class Square
{
    private int x, y, width, height;
    private Color theColor;

    public Square(int xS, int yS, int widthS, int heightS, Color squareColor)
    {
        x = xS;
        y = yS;
        width = widthS;
        height = heightS;
        theColor = squareColor;
    }

    public void draw(Graphics2D g2)
    {
        g2.setColor(theColor);
        Rectangle rectDraw = new Rectangle(x,y,width,height);
        g2.draw(rectDraw);
    }
}

GUI 类

public class SquareGUI extends JFrame
{
    private JComboBox colorChoices, shapeChoices;
    private JTextArea numberOfTimes;
    private SquarePanel thisPanel;

    public SquareGUI()
    {
        thisPanel = new SquarePanel();
        JPanel northPanel = new JPanel(new FlowLayout());
        setSize(640, 480);
        setLayout(new BorderLayout());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        ActionListener listener = new CommandListener();

        colorChoices = new JComboBox();
        shapeChoices = new JComboBox();
        numberOfTimes = new JTextArea(1,3);
        colorChoices.addItem("Black");
        colorChoices.addItem("Blue");
        colorChoices.addItem("Red");
        colorChoices.addItem("Green");
        shapeChoices.addItem("Square");
        shapeChoices.addItem("Circle");

        colorChoices.addActionListener(listener);
        shapeChoices.addActionListener(listener);



        northPanel.add(colorChoices);
        northPanel.add(shapeChoices);
        northPanel.add(new JLabel("Number of Shapes:"));
        northPanel.add(numberOfTimes);
        add(northPanel, BorderLayout.NORTH);

        add(thisPanel, BorderLayout.CENTER);

        setVisible(true);
    }

    public void addShapesRecursively(int x, int y, int width, int height, int times)
    {
        if (times == 0) { return; }
        Color colorChoice = null;
        switch (colorChoices.getSelectedIndex())
        {
            case 0: colorChoice = Color.BLACK; break;
            case 1: colorChoice = Color.BLUE; break;
            case 2: colorChoice = Color.RED; break;
            case 3: colorChoice = Color.GREEN; break;
        }
        if (shapeChoices.getSelectedIndex() == 0)
            thisPanel.add(new Square(x, y, width, height, colorChoice));
        else
            System.out.println("todo");

        addShapesRecursively(x-15, y-15, width + 15, height + 15, times - 1);
    }

    class CommandListener implements ActionListener
    {

        @Override
        public void actionPerformed(ActionEvent arg0) 
        {
            addShapesRecursively(getWidth()/2,getHeight()/2,20,20,Integer.parseInt(numberOfTimes.getText()));
        }

    }

    public static void main(String[]args)
    {
        new SquareGUI();
    }
}

我的 JPanel 类应该显示方 block 。

public class SquarePanel extends JPanel
{

    private ArrayList<Square> squareList;
    public SquarePanel()
    {
        setBackground(Color.WHITE);
        squareList = new ArrayList<Square>();

    }

    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;

        for (int i = 0; i < squareList.size(); i++)
        {
            Square tempSquare = squareList.get(i);
            tempSquare.draw(g2);
        }
    }

    public void add(Square addSquare)
    {
        squareList.add(addSquare);
    }
}

我很抱歉,目前还没有评论等。我一直在努力让这个工作发挥作用。我知道递归位有效,因为运行后该 ArrayList 中有 x 个 Square 对象。这只是在 JPanel 上绘制这些方 block 的问题。

我首先使用扩展 JComponent 的单独类进行了尝试,但永远无法让重写的 PaintComponent 在其中触发。所以我环顾四周,发现你也可以覆盖 JPanel 中的paintComponent。因此它按预期触发,但 JPanel 本身上没有出现任何内容。

我的总体问题是,如何让方 block 正确显示?

最佳答案

您需要鼓励小组自行更新。

在面板的 add 方法中添加对 repaint 的调用...

public void add(Square addSquare) {
    squareList.add(addSquare);
    repaint();
}

关于java - 在 JPanel 或 JComponent 上绘制同心形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13670787/

相关文章:

java - jsoup 意外获取 shopwiki 图像

java - JButton 在调整大小时以一种奇怪的方式复制自己

java - 简单的JFrame程序但看不到JTextfield

java - 绘制 JPanel 并将 JPanel 添加到 JFrame

java - 使用 WebLogic 放置 Web 应用程序的 Jar

java - 无法连接到 Eureka 服务器。异常 : java.net.ConnectException:连接被拒绝:连接

java - 计数器达到限制后不停止

java - 如何将事件添加到复选框?

java - JFrame 中的问题

java - 将 JLabel 放在 JLabel 的顶部,图像在