Results 1 to 6 of 6
- 08-29-2012, 02:13 AM #1
Member
- Join Date
- Aug 2012
- Posts
- 3
- Rep Power
- 0
How to repaint without erasing previous paint?
I have made a program that has a button when you click it, it draws a random circle on the panel. You can click the button again and again and it will keep drawing a new circle in a random location but it erases the previous cirlce meaning that there is only one circle being drawn at a time. How can I make it so that when you click the button it draws a new random circle but does not erase the other ones?
Java Code:import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Test extends JFrame { Board board; public Test() { setLayout(null); JPanel panel = new JPanel(); board = new Board(); board.setBounds(200,0,600,600); panel.setBounds (0,0,200,600); panel.setBackground(Color.blue); JButton exitButton = new JButton("exit"); exitButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { System.exit(0); } }); JButton drawCircle = new JButton ("Random Circle"); drawCircle.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { ClickRandomCircle(); } }); panel.add(exitButton); panel.add(drawCircle); add (panel); add (board); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(800, 600); setLocationRelativeTo(null); setVisible(true); setResizable(false); } public void ClickRandomCircle() { board.repaint(); } public static void main(String[] args) { new Test(); } }
Java Code:import javax.swing.JPanel; import java.awt.*; import java.awt.geom.Ellipse2D; import java.util.Random; public class Board extends JPanel { Random r; Graphics2D g2; public Board() { r = new Random(); } public void RandomCircle () { } public void paint (Graphics g) { super.paint(g); g2 = (Graphics2D) g; g2.draw(new Ellipse2D.Double(r.nextInt(500) + 1, r.nextInt(500) + 1, 100, 100)); } }
-
Re: How to repaint without erasing previous paint?
First off, read the Java painting tutorial as it will explain much that will help you. Next, don't draw in a JPanel's paint(...) method but rather its paintComponent method (as the tutorial will explain to you). Next, you have two options here: 1) on JButton press, create an Ellipse2D and put it into an ArrayList<Ellipse2D>, and call repaint(). You should then iterate through the list in the paintComponent(...) method drawing each ellipse in the list, or 2) Draw each oval on a BufferedImage and then display the BufferedImage in your paintComponent method via its drawImage(....) method.
- 08-30-2012, 07:04 AM #3
Member
- Join Date
- Aug 2012
- Posts
- 3
- Rep Power
- 0
Re: How to repaint without erasing previous paint?
That tutorial sounds like it would be helpful, could you post a link to it? I could not find it with a forum search. It's hard to figure out what is the best practice as some online tutorials tell you to do things one way and then others tell you to do them another way.
- 08-30-2012, 11:49 AM #4
Re: How to repaint without erasing previous paint?
If you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 08-31-2012, 08:31 PM #5
Member
- Join Date
- Aug 2012
- Posts
- 3
- Rep Power
- 0
Re: How to repaint without erasing previous paint?
Thanks man, I am sure google will allow me to find THE paint tutorial he is talking about because it must be the only ONE on the entire internet.
-
Re: How to repaint without erasing previous paint?
Well, it is the first hit if you Google it with the expected terms.
Similar Threads
-
Erasing Memory after user selections
By Wolfie in forum New To JavaReplies: 3Last Post: 04-16-2011, 12:21 AM -
paint and repaint problem
By koddy in forum NetBeansReplies: 2Last Post: 05-21-2010, 07:43 AM -
How to open a file without erasing its contents?
By spmchugh82 in forum New To JavaReplies: 2Last Post: 12-17-2008, 02:52 AM -
Writing to excel file erasing existing formatting
By jmHoekst in forum New To JavaReplies: 1Last Post: 09-16-2008, 06:58 PM -
other than paint repaint
By amith in forum Java 2DReplies: 1Last Post: 07-02-2008, 12:39 AM
Bookmarks