![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2005
Posts: 1
Rep Power: 0
![]() |
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;
}
}I'd appreciate your help. Last edited by dotred; Mar 11th, 2005 at 8:10 PM. |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Mar 2005
Location: different places. constantly on the run.
Posts: 57
Rep Power: 4
![]() |
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++ ) ![]()
__________________
There's got to be more to life than being really, really ridiculously good looking |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|