954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Repaint() not repainting.

I wrote this simple java program to test the repaint mechanism in Java for my Eclipse IDE (not that it works on XCode either). The repaint() mechanism is not repainting, it does not erase the code previous to it in the graphic. It should according to everything I've read. Help if you can. Thanks.

import java.awt.*;

import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;

public class pp2 extends JApplet implements ActionListener
{
	JButton clearButton;
	int i;
	String output;
	
	public void init()
	{
		clearButton = new JButton("Clear");
		
		Container c = getContentPane();
		c.setLayout(new FlowLayout());
		
		c.add(clearButton);
		
		clearButton.addActionListener(this);
		
		i = 1;
		
		
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if ((i%2)!=0)
			output = "I is odd";
		else
			output = "I is even";
		
		i++;
		
		repaint();
		
	}
	
	
	public void paint(Graphics g)
	{
		g.drawString(output, 50, 50);
	}
}
curtissumpter
Light Poster
25 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

Just add a call to super.paint(g) prior to your own paint code.

public void paint(Graphics g)
	{
                super.paint(g);
		g.drawString(output, 50, 50);
	}
Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

How cool are you? Thanks. Would you mind telling me why mine didn't work? I know your accessing the JApplet's method of paint directly. But why doesn't my simple repaint() work? Whats the difference? Thanks.

curtissumpter
Light Poster
25 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

When repaint() doesn't work, I try to do validate() just before. This way the changes that took place are updated.

toucan
Newbie Poster
20 posts since Nov 2007
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You