Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Painting on Panel - Problem with buttons

  1. #1
    JMateo is offline Member
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default Painting on Panel - Problem with buttons

    Hello.
    The following code is to paint with a brush and erase using the eraser. However, when I click on the button and start painting, the image button also appears in the upper left corner. How to prevent it?
    Java Code:
    import javax.swing.*;
     
    import java.awt.*;
    import java.awt.event.*;
     
    public class C extends JPanel implements ActionListener,MouseMotionListener 
    {
        public int x, y,wybor;
        JButton pedzel,gumka;
        public C()
        {
                ImageIcon ii=new ImageIcon("d:/pedzel.png");
                    setLayout(null);
                    pedzel=new JButton(ii);
                    pedzel.setBounds(400,5,100,54);
                    pedzel.addActionListener(this);
                    add(pedzel);
     
                    ii=new ImageIcon("d:/gumka.png");
                    gumka=new JButton(ii);
                    gumka.setBounds(400,70,100,100);
                    gumka.addActionListener(this);
                    add(gumka);
                    addMouseMotionListener(this);
        }
     
        public void paintComponent(Graphics g) {
     
            Graphics2D g2d = (Graphics2D) g; 
     if(wybor==1)
     {
            g2d.setColor(Color.BLUE);
            g2d.fillOval(x, y, 20,20);
     }
     if(wybor==2)
     {
            g2d.setColor(getBackground());
            g2d.fillOval(x, y, 20,20);
     }
        }
            public void mouseDragged(MouseEvent e) {
            x = e.getX();
            y = e.getY();
            repaint();        
            }
            public void mouseMoved(MouseEvent e) {        
            }
            public void actionPerformed(ActionEvent e) {
     
                    if(e.getSource()==pedzel)
                    {
                            wybor=1;
                    }
                    if(e.getSource()==gumka)
                    {
                            wybor=2;
                    }
            }
    }
    My frame

    Java Code:
     import javax.swing.JFrame;
    import javax.swing.JPanel;
     
     
    public class Ramka extends JFrame
    {
    C j=new C();
            public Ramka()
            {
                    add(j);
                    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    setVisible(true);
                    setSize(600,500);
            }
            public static void main(String[] args) {
                    new Ramka();
     
            }
     
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,252
    Blog Entries
    1
    Rep Power
    24

    Default Re: Painting on Panel - Problem with buttons

    You need to call the super.paintComponent(...) method within your paintComponent method.

  3. #3
    JMateo is offline Member
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default Re: Painting on Panel - Problem with buttons

    If I do this, I'll lose effect of painting and erasing. When I add super.paintComponent my whole frame will refresh and waht I only paint is an Oval.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Moderator
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    9,928
    Rep Power
    16

    Default Re: Painting on Panel - Problem with buttons

    Oh yeah? And what happens if you resize the frame by a few pixels, or iconify (minimize) and restore it?

    A suggestion: draw your ellipses (they're not ovals, the method is badly named) to a BufferedImage of the desired painting canvas size and call repaint(). Then paintImage(...) the BufferedImage in a paintComponent(...) override that either paints the entire client area or invokes the super implementation.

    db
    Fubarable likes this.
    Why do they call it rush hour when nothing moves? - Robin Williams

  5. #5
    JMateo is offline Member
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default Re: Painting on Panel - Problem with buttons

    It works. Thank you very much:)

Similar Threads

  1. How to close a form with a panel with buttons
    By aborgeld in forum New To Java
    Replies: 13
    Last Post: 03-27-2011, 01:57 PM
  2. painting problem
    By hannes in forum New To Java
    Replies: 3
    Last Post: 01-17-2010, 11:44 AM
  3. AWT: Painting buttons and text areas in a canvas
    By chappa in forum AWT / Swing
    Replies: 6
    Last Post: 01-09-2010, 02:37 PM
  4. painting lines when clicking a panel
    By yuriythebest in forum New To Java
    Replies: 0
    Last Post: 12-14-2008, 09:56 PM
  5. Spacing buttons on a panel
    By 2o2 in forum New To Java
    Replies: 8
    Last Post: 10-20-2008, 10:44 AM

Tags for this Thread

Posting Permissions

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