Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Fubarable

Thread: How to repaint without erasing previous paint?

  1. #1
    neal8929 is offline Member
    Join Date
    Aug 2012
    Posts
    3
    Rep Power
    0

    Default 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));
        	
        }
    }

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

    Default 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.

  3. #3
    neal8929 is offline Member
    Join Date
    Aug 2012
    Posts
    3
    Rep Power
    0

    Default 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.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: How to repaint without erasing previous paint?

    Quote Originally Posted by neal8929 View Post
    That tutorial sounds like it would be helpful, could you post a link to it?
    Java painting tutorial

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    neal8929 is offline Member
    Join Date
    Aug 2012
    Posts
    3
    Rep Power
    0

    Default 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.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Erasing Memory after user selections
    By Wolfie in forum New To Java
    Replies: 3
    Last Post: 04-15-2011, 11:21 PM
  2. paint and repaint problem
    By koddy in forum NetBeans
    Replies: 2
    Last Post: 05-21-2010, 06:43 AM
  3. How to open a file without erasing its contents?
    By spmchugh82 in forum New To Java
    Replies: 2
    Last Post: 12-17-2008, 01:52 AM
  4. Writing to excel file erasing existing formatting
    By jmHoekst in forum New To Java
    Replies: 1
    Last Post: 09-16-2008, 05:58 PM
  5. other than paint repaint
    By amith in forum Java 2D
    Replies: 1
    Last Post: 07-01-2008, 11:39 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
  •