Results 1 to 13 of 13
  1. #1
    Navarrano is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Problem with painting a JPanel

    Hi,
    I have crated two panales with override "paint" methods. One panel draws circles in a place where it is pressed with mouse button, second one draws squares. Everything works almost fine, when clicking just at one panel. If I create for example 5 circles on the first panel and then click on the second one, all circles are drawn and additional square.
    What should I make to separate graphics on both panels? That clicking on, let's say left one, will create only circles, and clicking on right only squares.

    Thanks in advance

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: Problem with painting a JPanel

    When you create a panel, you should tell the panel what kind of shape to draw when it is clicked on. For example: Pass a shape_to_draw indicating value in the constructor.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Navarrano is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: Problem with painting a JPanel

    Yes, I did so already and it works fine. The problem is that painting one panel, causes that also second panel in painted on it. So the whole contents of the second panel is drawn on the first panel. And I can't find the reason why it is so.

    Here is what happens when I click left panel ( "green" circle ): Problem with painting a JPanel-img1.jpg

    and here after clicking right panel ( "blue" square ): Problem with painting a JPanel-img2.jpg
    Last edited by Navarrano; 04-09-2012 at 02:51 PM.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: Problem with painting a JPanel

    You will need to post code that shows the problem. Make a small program that compiles, executes and shows the problem and post it here. Be sure to wrap the code in code tags.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Navarrano is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: Problem with painting a JPanel

    Here is my JPanel class thet objects are created in JFrame.

    Java Code:
    /////// myPanel.java ////////
    public class MyPanel extends javax.swing.JPanel{
    
        /**
         * Creates new form MyPanel
         */
        private Database array;
        private String figures;
        private Random rand;
        
        public MyPanel(){
        }
        public MyPanel(Figures fig) {
            initComponents();
            array = new Database();
            figures = fig.toString();
            rand = new Random();
        }
        
        //@Override
        public void paint(Graphics g){
            array.drawAll(g);
            printBorder(g);
        }
    
        /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
            this.setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 342, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 256, Short.MAX_VALUE)
            );
        }// </editor-fold>                        
        // Variables declaration - do not modify                     
        // End of variables declaration                   
    
        public void addFigure(MouseEvent e) { 
            Color color = new Color(rand.nextInt());
            switch(figures){
                case "Circle":
                {
                    array.add(new Circle(e.getX(),e.getY(),color));
                    break;
                }
                case "Square":
                {
                    array.add(new Square(e.getX(),e.getY(),color));
                    break;
                }
            }
        } 
    }
    ///// myFrame.java //////
    circlesPanel = new GUI.MyPanel(Figures.Circle);
    squaresPanel = new GUI.MyPanel(Figures.Square);                                         
    
        private void squaresPanelMousePressed(java.awt.event.MouseEvent evt) {                                          
    
            squaresPanel.addFigure(evt);
            squaresPanel.repaint();
        }                                         
    
        private void circlesPanelMousePressed(java.awt.event.MouseEvent evt) {
    
            circlesPanel.addFigure(evt);
            circlesPanel.repaint();
        }
    If you need, I'll paste more codes.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: Problem with painting a JPanel

    The code you posted will not execute to show the problem. It does not have a main() method.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Navarrano is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: Problem with painting a JPanel

    Well, I hoped it would be enought because there is a lot of code generated by NetBeans IDE. Here is the whole code.

    Java Code:
    //////// Figure.java
    package Figures;
    
    import java.awt.Color;
    import java.awt.Graphics;
    
    public abstract class Figure {
        int x, y;
        Color color;
        
        public Figure(int x, int y, Color color){
            this.x = x;
            this.y = y;
            this.color = color;
        }
        public abstract void draw(Graphics g);
    }
    ////// Square.java ////////
    package Figures;
    
    import java.awt.Color;
    import java.awt.Graphics;
    public class Square extends Figure{
        
        public Square( int x, int y, Color color){
            super(x,y,color);
        }
        @Override
        public void draw(Graphics g) {
            g.setColor(color);
            g.fillRect(x-10, y-10, 20, 20);
        }   
    }
    /////// Circle.java ////////
    
    package Figures;
    
    import java.awt.Color;
    import java.awt.Graphics;
    
    public class Circle extends Figure{
    
        public Circle( int x, int y, Color color){
            super(x,y,color);
        }
        @Override
        public void draw(Graphics g) {
            g.setColor(color);
            g.fillOval(x-10, y-10, 20, 20);
        }    
    }
    /////// Database.java ///////
    package Figures;
    
    import java.awt.Graphics;
    import java.util.ArrayList;
    
    
    public class Database extends ArrayList<Figure>{
        public void drawAll(Graphics g){
            for( Figure fig : this ){
                fig.draw(g);
            }
        }
    }
    ////// MyFrame.java ///////
    package GUI;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import Figures.*;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.util.Random;
    
    public class MyPanel extends javax.swing.JPanel{
    
        private Database array;
        private String figures;
        private Random rand;
        
        public MyPanel(){
        }
        public MyPanel(Figures fig) {
            initComponents();
            array = new Database();
            figures = fig.toString();
            rand = new Random();
        }
        
        //@Override
        public void paint(Graphics g){
            array.drawAll(g);
            printBorder(g);
        }
    
        /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
            this.setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 342, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 256, Short.MAX_VALUE)
            );
        }// </editor-fold>                        
        // Variables declaration - do not modify                     
        // End of variables declaration                   
    
        public void addFigure(MouseEvent e) { 
            Color color = new Color(rand.nextInt());
            switch(figures){
                case "Circle":
                {
                    array.add(new Circle(e.getX(),e.getY(),color));
                    break;
                }
                case "Square":
                {
                    array.add(new Square(e.getX(),e.getY(),color));
                    break;
                }
            }
        } 
    }
    ///// MyFrame.java /////////
    package GUI;
    
    public class MainFrame extends javax.swing.JFrame {
    
       
        public MainFrame() {
            initComponents();
        }
    
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            trianglesPanel = new GUI.MyPanel(Figures.Triangle);
            circlesPanel = new GUI.MyPanel(Figures.Circle);
            squaresPanel = new GUI.MyPanel(Figures.Square);
    
            trianglesPanel.setBorder(new javax.swing.border.MatteBorder(null));
            trianglesPanel.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mousePressed(java.awt.event.MouseEvent evt) {
                    trianglesPanelMousePressed(evt);
                }
            });
    
            javax.swing.GroupLayout trianglesPanelLayout = new javax.swing.GroupLayout(trianglesPanel);
            trianglesPanel.setLayout(trianglesPanelLayout);
            trianglesPanelLayout.setHorizontalGroup(
                trianglesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 122, Short.MAX_VALUE)
            );
            trianglesPanelLayout.setVerticalGroup(
                trianglesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 0, Short.MAX_VALUE)
            );
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("Figures drawing");
    
            circlesPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
            circlesPanel.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mousePressed(java.awt.event.MouseEvent evt) {
                    circlesPanelMousePressed(evt);
                }
            });
    
            javax.swing.GroupLayout circlesPanelLayout = new javax.swing.GroupLayout(circlesPanel);
            circlesPanel.setLayout(circlesPanelLayout);
            circlesPanelLayout.setHorizontalGroup(
                circlesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 134, Short.MAX_VALUE)
            );
            circlesPanelLayout.setVerticalGroup(
                circlesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 126, Short.MAX_VALUE)
            );
    
            squaresPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
            squaresPanel.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mousePressed(java.awt.event.MouseEvent evt) {
                    squaresPanelMousePressed(evt);
                }
            });
    
            javax.swing.GroupLayout squaresPanelLayout = new javax.swing.GroupLayout(squaresPanel);
            squaresPanel.setLayout(squaresPanelLayout);
            squaresPanelLayout.setHorizontalGroup(
                squaresPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 186, Short.MAX_VALUE)
            );
            squaresPanelLayout.setVerticalGroup(
                squaresPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 177, Short.MAX_VALUE)
            );
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(circlesPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 122, Short.MAX_VALUE)
                    .addComponent(squaresPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(87, 87, 87))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(1, 1, 1)
                            .addComponent(circlesPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(53, 53, 53)
                            .addComponent(squaresPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap(61, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>
    
        private void trianglesPanelMousePressed(java.awt.event.MouseEvent evt) {                                            
    
        }                                           
    
        private void squaresPanelMousePressed(java.awt.event.MouseEvent evt) {                                          
    
            squaresPanel.addFigure(evt);
            squaresPanel.repaint();
        }                                         
    
        private void circlesPanelMousePressed(java.awt.event.MouseEvent evt) {
    
            circlesPanel.addFigure(evt);
            circlesPanel.repaint();
        }
    
        // Variables declaration - do not modify
        private GUI.MyPanel circlesPanel;
        private GUI.MyPanel squaresPanel;
        private GUI.MyPanel trianglesPanel;
        // End of variables declaration
    
            public static void main(String args[]) {
            
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                public void run() {
                    new MainFrame().setVisible(true);
                }
            });
        }
    
    
    
    
    }
    enum Figures{
        Circle, Square, Triangle;
    }

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: Problem with painting a JPanel

    What is supposed to happen when the code is executed?
    I get a window with two boxes drawn, smaller in the upper left.
    When I click in the left box, a colored circle is drawn in that box.
    When I click on the right box, a colored square is drawn in that box.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Navarrano is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: Problem with painting a JPanel

    And no circles are in the right box? Cuz what is happening for you is that what I expected. But for me, the left box is drawn on the right box.

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: Problem with painting a JPanel

    circles in left box, squares in right box
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    Navarrano is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: Problem with painting a JPanel

    And this is what I get after one click on the left box and then one on the right: Problem with painting a JPanel-img3.jpg

    As you can see, left smaller box appeared in the right box :(

    PS. That is damn strange but after minimizing window, this square disappeared. I have no idea what has happened :P

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: Problem with painting a JPanel

    Try rewriting it and do not use the IDE generated code.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    Navarrano is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: Problem with painting a JPanel

    Finally had some time to work with that and found a solution. In the methods:
    Java Code:
    private void squaresPanelMousePressed(java.awt.event.MouseEvent evt) {                                         
     
            squaresPanel.addFigure(evt);
            squaresPanel.repaint();
        }
    I added simple
    Java Code:
    this.repaint()
    and it works fine now.

    Thanks for Your help!

Similar Threads

  1. painting to a JPanel in a JFrame
    By Parkournerd in forum AWT / Swing
    Replies: 1
    Last Post: 01-16-2012, 09:24 PM
  2. Replies: 2
    Last Post: 10-17-2011, 08:04 PM
  3. Problem painting on JPanel
    By Boatski in forum AWT / Swing
    Replies: 0
    Last Post: 12-01-2010, 04:36 AM
  4. JPanel not always painting everything
    By ekted in forum AWT / Swing
    Replies: 0
    Last Post: 11-26-2009, 11:24 AM
  5. Jpanel painting problem
    By kcakir in forum AWT / Swing
    Replies: 3
    Last Post: 04-15-2009, 10:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •