开发者

Trying to add a dynamically positioned image in a JPanel on a button click

开发者 https://www.devze.com 2023-04-05 20:14 出处:网络
I am trying to add/draw a single Graphics object to an existing JPanel. I am generating 10 initial Graphics objects randomly sized and place in the panel, but would like to add additional drawn object

I am trying to add/draw a single Graphics object to an existing JPanel. I am generating 10 initial Graphics objects randomly sized and place in the panel, but would like to add additional drawn objects one a time, randomly sized and placed like the initial 10.

Currently, the AddNewDrawItem class is not rendering the new Graphics object.

Thank you for input.

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();
   开发者_JAVA技巧         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


You've got some problems with your code, one of which is that you've got program logic in your paintComponent method: the code randomly changes values that are displayed within this method, meaning your display will change when it repaints, whether you want it to or not. To see that this is so, try to resize your GUI and you'll see some psychedelic changes in the drawn red and blue rectangles.

Now as to your current problem, the solution to it is similar to the solution to the problem I describe above. I suggest...

  • that you create an ArrayList<Rectangle2D>,
  • that you create your random rectangles in your class's constructor so that they're created only once, and then place them in the ArrayList above.
  • that you iterate through this ArrayList in your JPanel's paintComponent method, drawing them as you go. This way paintComponent does nothing but paint which is as it should be,
  • that you create a MouseAdapter-derived object and add it as a MouseListener and MouseMotionListener to your DrawingPanel
  • that you use the listener above to create a new Rectangle2D object and when done add it to the ArrayList and call repaint on the DrawingPanel
  • that you activate the mouse adapter via your button's action listener.

I'll stop there, but I think you get the idea, and if you don't, please ask any questions you may have.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号