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

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

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.