Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
Win a copy of
The Mikado Method
this week in the
Agile and other Processes
forum!
JavaRanch
»
Java Forums
»
Java
»
Swing / AWT / SWT
Author
hard time with swing
Faiz Abdelhafid
Ranch Hand
Joined: Nov 21, 2009
Posts: 32
posted
Mar 19, 2010 14:22:52
0
Hello Guys
How can I make the drawn text moving with this thread ?
thread class
public class MovingThread extends Thread { InsidePane pane=new InsidePane(); public MovingThread() { } @Override public void run(){ int x=pane.getPosX(); int y=pane.getPosY(); while(x<pane.getWidth()){ x++; } pane.setPosX(x); pane.repaint(); try{ Thread.sleep(2000); }catch(InterruptedException e){ e.printStackTrace(); } } }
panel class
public class InsidePane extends JPanel{ private int posX=20; private int posY=20; private String str="Test"; public int getPosX() { return posX; } public void setPosX(int posX) { this.posX = posX; } public int getPosY() { return posY; } public void setPosY(int poxY) { this.posY = poxY; } public void paintComponent(Graphics g){ g.drawString(this.str,this.posX, this.posY); } }
frame class
import gui.newpackage.*; import threads.newpackage.*; import javax.swing.JFrame; public class MainWindow extends JFrame { InsidePane pane= new InsidePane(); public MainWindow() { Thread mvt=new MovingThread(); this.setSize(400,400); this.setContentPane(pane); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); mvt.start(); } }
test
class
public class test { public static void main(String args[]){ MainWindow mw=new MainWindow(); } }
Thanks
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
Mar 19, 2010 19:15:24
0
some lines changed, some comments added
(this is set up to run as a single .java file)
import javax.swing.*; import java.awt.*; class MainWindow extends JFrame { InsidePane pane= new InsidePane(); public MainWindow() { //Thread mvt=new MovingThread(); Thread mvt=new MovingThread(pane); this.setSize(400,400); this.setContentPane(pane); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); mvt.start(); } } class MovingThread extends Thread { //InsidePane pane=new InsidePane(); InsidePane pane;; //public MovingThread() { public MovingThread(InsidePane p) { pane = p;//<----added } @Override public void run(){ int x=pane.getPosX(); int y=pane.getPosY(); while(x<pane.getWidth()){ x++; //}<---moved, Thread.sleep needs to be inside loop pane.setPosX(x); pane.repaint(); try{ //Thread.sleep(2000);//<too slow for testing Thread.sleep(200); }catch(InterruptedException e){ e.printStackTrace(); } } } } class InsidePane extends JPanel{ private int posX=20; private int posY=20; private String str="Test"; public int getPosX() { return posX; } public void setPosX(int posX) { this.posX = posX; } public int getPosY() { return posY; } public void setPosY(int poxY) { this.posY = poxY; } public void paintComponent(Graphics g){ super.paintComponent(g);//<--------needs this to clear old painting g.drawString(this.str,this.posX, this.posY); } } class test { public static void main(String args[]){ MainWindow mw=new MainWindow(); } }
I agree. Here's the link:
http://aspose.com/file-tools
subject: hard time with swing
Similar Threads
what is need of super() in the foolowing line of code?
Custom drawing
Please help with this program
advice on drawing a square wave pulse train
Is this a good approach?
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter