尝试在按钮单击时在JPanel中添加动态定位的图像

jam*_*mer 3 java swing graphics2d

我试图将一个Graphics对象添加/绘制到现有的JPanel.我正在生成10个随机大小的初始Graphics对象并放置在面板中,但是想要一次添加额外的绘制对象,随机调整大小并放置像初始10.

目前,AddNewDrawItem类不呈现新的Graphics对象.

谢谢你的意见.

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class Painter{

private DrawingPanel dp = new DrawingPanel();

    //constructor
    public Painter(){
        buildGUI();
    }

    private void buildGUI(){
        JFrame frame = new JFrame();
        frame.setLayout(new BorderLayout());
        frame.setTitle("Paint drawing demonstration");
        JPanel headerPanel = new JPanel();
        headerPanel.add(new JLabel("The drawing panel is below"));
        JButton addNew = new JButton("Add New Graphic");
        addNew.addActionListener(new addNewClickHandler());
        headerPanel.add(addNew);            
        frame.add(BorderLayout.NORTH,headerPanel);
        frame.add(BorderLayout.SOUTH,this.dp);  
        frame.pack();
        frame.setVisible(true);
    }

    class DrawingPanel extends JPanel {

        public void paintComponent(Graphics g) {
            super.paintComponent(g);       
            this.setBackground(Color.white);

            int x, posx, posy, width, height;

            for(x=0;x<=10;x++){
                //even number differentiation
                if(x % 2 == 0){
                    g.setColor(Color.red);
                }else{
                    g.setColor(Color.blue);
                }

                Random rand = new Random();
                posx = rand.nextInt(300);
                posy = rand.nextInt(300);
                width = rand.nextInt(40);
                height = rand.nextInt(40);

                //System.out.println("the ran x pos is: " + posx);
                g.fillRect(posx, posy, width, height);
            }//end for  
         }//end paintComponent  

        public Dimension getPreferredSize() {
           return new Dimension(400,400);
        }
    }// end DrawingPanel

    private class addNewClickHandler implements ActionListener{
        public void actionPerformed(ActionEvent e){
            System.out.print("in addNew_click_handler click handler");//trace debug
            AddNewDrawItem newItem = new AddNewDrawItem();
            newItem.repaint();
            System.out.print("after repaint() in addNew_click_handler click  handler");//trace debug
            }
        }

    class AddNewDrawItem extends JPanel {
        public void paintComponent(Graphics g) {
            super.paintComponent(g);       
            this.setBackground(Color.white);
            int posx, posy, width, height;

            Random rand = new Random();
            posx = rand.nextInt(300);
            posy = rand.nextInt(300);
            width = rand.nextInt(40);
            height = rand.nextInt(40);
            g.setColor(Color.cyan);
            g.fillRect(posx, posy, width, height);

        }//end paintComponent   
    }//end AddNewDrawItem 

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

    }//end class Painter
Run Code Online (Sandbox Code Playgroud)

Hov*_*els 5

您的代码遇到了一些问题,其中一个问题就是您在paintComponent方法中有了程序逻辑:代码随机更改此方法中显示的值,这意味着您的显示将在重新绘制时更改,无论您是否需要它与否.要看到这是如此,请尝试调整GUI的大小,您将看到绘制的红色和蓝色矩形中的一些迷幻变化.

现在关于你当前的问题,它的解决方案类似于我上面描述的问题的解决方案.我建议...

  • 你创建了一个ArrayList<Rectangle2D>,
  • 您在类的构造函数中创建随机矩形,以便它们只创建一次,然后将它们放在上面的ArrayList中.
  • 您在JPanel的paintComponent方法中遍历此ArrayList,随时绘制它们.这样paintComponent什么都不做,但油漆应该是,它应该是,
  • 您创建一个MouseAdapter派生的对象,并将其作为MouseListener和MouseMotionListener添加到您的DrawingPanel
  • 您使用上面的侦听器来创建一个新的Rectangle2D对象,完成后将其添加到ArrayList并在DrawingPanel上调用重绘
  • 你通过按钮的动作监听器激活鼠标适配器.

我会停在那里,但我认为你明白了,如果你不这样做,请问你有什么问题.