Results 1 to 5 of 5
- 12-30-2012, 08:56 PM #1
Member
- Join Date
- Dec 2012
- Posts
- 3
- Rep Power
- 0
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?
My frameJava 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; } } }
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(); } }
-
Re: Painting on Panel - Problem with buttons
You need to call the super.paintComponent(...) method within your paintComponent method.
- 12-31-2012, 09:23 AM #3
Member
- Join Date
- Dec 2012
- Posts
- 3
- Rep Power
- 0
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.
- 12-31-2012, 09:36 AM #4
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 12-31-2012, 11:19 AM #5
Member
- Join Date
- Dec 2012
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
How to close a form with a panel with buttons
By aborgeld in forum New To JavaReplies: 13Last Post: 03-27-2011, 01:57 PM -
painting problem
By hannes in forum New To JavaReplies: 3Last Post: 01-17-2010, 11:44 AM -
AWT: Painting buttons and text areas in a canvas
By chappa in forum AWT / SwingReplies: 6Last Post: 01-09-2010, 02:37 PM -
painting lines when clicking a panel
By yuriythebest in forum New To JavaReplies: 0Last Post: 12-14-2008, 09:56 PM -
Spacing buttons on a panel
By 2o2 in forum New To JavaReplies: 8Last Post: 10-20-2008, 10:44 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks