Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 11th, 2005, 5:32 PM   #1
dotred
Newbie
 
Join Date: Mar 2005
Posts: 1
Rep Power: 0 dotred is on a distinguished road
Exclamation repaint() problem in very simple program

I'm new to Java and trying to experiment with threads, I found myself stuck in a much simpler problem: I am trying to force repaint in the following simple app but it doesnt work. Instead the panel is repainted when the window is resized, covered and uncovered etc. It seems to neglect repaint for some reason that I tried for several hours to understand. This is my few lines of code:
import javax.swing.*;

import java.awt.*;

public class ThreadingOne extends JFrame
{
	
	public ThreadingOne()
	{
		super("test test");

		setSize(500,500);
		
		Container mycont=getContentPane();
		
		MyPanelOne mypanel= new MyPanelOne();

		mycont.add(mypanel);
		
	}
	
	public static void main(String[] args){
		
		ThreadingOne obj = new ThreadingOne();
		
		obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		obj.setVisible(true);
		
	}
	
}


class MyPanelOne extends JPanel{
	
	int c, ff=30;
	
	public MyPanelOne()
	{
		
		for(c=0; c<10; c++)
		{
			try{
			     Thread.sleep(100);
			}
			catch(InterruptedException x)
			{
                          //unhandled exception
                        }
			repaint();
		}
		
	
	}
	
	public void paintComponent(Graphics g){
		
	super.paintComponent(g);

	g.drawString(Integer.toString(c),50,ff);

	ff=ff+30;
	
	}

}
As I said, it works like repaint call is not there.

I'd appreciate your help.

Last edited by dotred; Mar 11th, 2005 at 8:10 PM.
dotred is offline   Reply With Quote
Old Mar 17th, 2005, 5:17 AM   #2
Easter Bunny
Programmer
 
Easter Bunny's Avatar
 
Join Date: Mar 2005
Location: different places. constantly on the run.
Posts: 57
Rep Power: 4 Easter Bunny is on a distinguished road
ok, i'm not quite sure why it counts to ten, then paints "10" on the panel. was checking this out last night and didn't get that right. instinctively i started modifying your program(think of me as tim "the toolman" taylor... but only as a programmer). i added in a little thread class that does exactly what you want to do... sorta. what you're trying to do, is to paint a number on a panel, but the y-coordinate changes and paints it further down. i made that a constant, so that i could see what i was doing and then painted the value of "ff" on the panel.

this is my modified MyOnePanel:

class MyPanelOne extends JPanel
{
	int c, ff = 30;
	public MyPanelOne()
	{
		new NewThread().start();//creating an instance of the thread and starting it
	}

	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		
		g.drawString( Integer.toString( ff ), 50, 50 );
		
		ff += 30;
	}
	
	class NewThread extends Thread   //thread that will call repaint after aprox. 1sec
	{
		public void run()
		{
			while( true )   //lets the thread run in a loop. dies when program terminates
			{
				try
				{
				    Thread.sleep( 1000 ); //thread waits for aprox. 1sec
				}
				catch( InterruptedException x )
				{
					//unhandled exception
				}
				
				repaint(); //repaints the panel
			}
		}
	}
}

hope this is what you were trying to do. if not, let me know and i'll see if i can get the
if ( c = 0 ; c < 10 ; c++ )
thing to graft. cool.
__________________
There's got to be more to life than being really, really
ridiculously good looking
Easter Bunny is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:02 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC