Results 1 to 7 of 7
Thread: Thread.sleep/Init?
- 01-22-2009, 04:27 AM #1
Member
- Join Date
- Jan 2009
- Posts
- 92
- Rep Power
- 0
Thread.sleep/Init?
I'm attempting to display smaller squares inside the grid I created. I was going to use a Thread.sleep(1000) to seperate them being displayed by 1 second each (I wanted them to pop up on the screen 1 by 1 in 1 second intervals). This is not updating to the screen 1 by 1. It will complete the entire for loop pausing in the for loop and not displaying. Heres the piece of code i'm using:
Obviously this is bad I know right off the bat because its going to do this each time I have to do a repaint(), resize the window etc etc. When I was messing with applets they had an Init() that you could do things once which would be perfect for me here. Any advice? ThanksJava Code:public void paintComponent(Graphics g){ super.paintComponent(g); drawEnviroment(g); g.setColor(Color.red); g.drawString("Welcome to Project Gensis",320,70); g.setColor(Color.RED); g.fillRect(squareX,squareY,squareW,squareH); g.setColor(Color.BLACK); g.drawRect(squareX,squareY,squareW,squareH); drawSquares(g); } public void drawSquares(Graphics g){ g.setColor(Color.RED); for(int i=0; i<144; i++){ try { Thread.sleep(10); } catch (InterruptedException e) { } g.fillRect(GlobalInfo.getLocationsX(i),GlobalInfo.getLocationsY(i),10,10); } }
Monc
- 01-22-2009, 04:28 AM #2
Member
- Join Date
- Jan 2009
- Posts
- 92
- Rep Power
- 0
Oh and I tried to throw in a Repaint() in there before and after the g.fillRect to see if it would force to the screen but it was a no go. Infact nothing that is executed BEFORE the drawSquares() function is not even displaying, the screen just kind of stays gray until it does all the drawings.
I pretty much just want to display each small square 1 at a time the first time the application is opened. After that I'm done showing off. As I said before, if I had access to a Init() function like applets I could do this easily I believe.
But there maybe a problem with the way I'm trying to use the Thread.Sleep while expecting it to paint(). I thought the plan was simple:
1. Draw a square
2. Wait 1 second
3. Draw the next square
4. etcLast edited by Moncleared; 01-22-2009 at 04:41 AM. Reason: Better explanation I hope
- 01-22-2009, 08:01 AM #3
Never call Thread.sleep() on the EDT.
Use a javax.swing.Timer for delays and/or repeats.
More here:
Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI with JFC/Swing)
db
- 01-22-2009, 05:09 PM #4
Member
- Join Date
- Jan 2009
- Posts
- 92
- Rep Power
- 0
Darryl, thanks for your attempt to help. Although it wasn't helpful to me. I've tried to use a timer in the same manner and it still stalls the GUI. I read the Lesson on Concurrency and I'm not sure if you/it is suggesting I use a SwingWorker to do this or what, but it did not explain Timers.
Is it possible I get an example of a proper way to use the Timer for what I am trying to do? Even a link will work, I've been searching for Timer examples but they all seem to fail me in what I am doing. Its possible I'm implementing it wrong. Anytime I put a Timer around my drawing it just hangs until all of it has finished. Thanks
MoncLast edited by Moncleared; 01-22-2009 at 05:16 PM.
-
have fun
Java Code:import java.awt.*; import java.awt.event.*; import java.awt.geom.Rectangle2D; import java.util.ArrayList; import java.util.List; import java.util.Random; import javax.swing.*; public class FuSwing { private static final Dimension MAIN_SIZE = new Dimension(600, 450); private static final int TIMER_DELAY = 200; // in milliseconds private static final int SMALL_SQUARE_SIDE = 10; // JPanel to draw in private JPanel mainPanel = new JPanel() { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); myPaint(g); // call all drawing code from this method in the body of this class } }; private int squareX = 100; private int squareY = 100; private int squareW = 100; private int squareH = 100; // The timer will fill this List with rectangles to be drawn private List<Rectangle2D> rectList = new ArrayList<Rectangle2D>(); // TimerAction's actionPerformed method is called every TIMER_DELAY milliseconds private Timer swingTimer = new Timer(TIMER_DELAY, new TimerAction()); private Random random = new Random(); public FuSwing() { mainPanel.setPreferredSize(MAIN_SIZE); swingTimer.start(); // tell timer to start } private void myPaint(Graphics g) { //drawEnviroment(g); g.setColor(Color.red); g.drawString("Welcome to Project Gensis",320,70); g.setColor(Color.RED); g.fillRect(squareX,squareY,squareW,squareH); g.setColor(Color.BLACK); g.drawRect(squareX,squareY,squareW,squareH); //drawSquares(g); g.setColor(Color.blue); Graphics2D g2 = (Graphics2D)g; // now draw every blue square in the list for (int i = 0; i < rectList.size(); i++) { g2.fill(rectList.get(i)); } } public JComponent getPanel() { return mainPanel; } private class TimerAction implements ActionListener { // the swingTimer will call this method every TIMER_DELAY mSecs public void actionPerformed(ActionEvent arg0) { int x = random.nextInt(mainPanel.getWidth() - SMALL_SQUARE_SIDE); int y = random.nextInt(mainPanel.getHeight() - SMALL_SQUARE_SIDE); Rectangle2D rect2D = new Rectangle2D.Double(x, y, SMALL_SQUARE_SIDE, SMALL_SQUARE_SIDE); rectList.add(rect2D); mainPanel.repaint(); //mainPanel.repaint(rect2D.getBounds()); // this may be better actually } } private static void createAndShowGUI() { JFrame frame = new JFrame("FuSwing Application"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new FuSwing().getPanel()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { // call the Swing app in a thread-safe way javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }Last edited by Fubarable; 01-22-2009 at 11:59 PM. Reason: Code comments added
- 01-24-2009, 12:25 AM #6
Member
- Join Date
- Jan 2009
- Posts
- 92
- Rep Power
- 0
Thanks for the help Fubar,
Another situation for you that I've encountered and cannot overcome. I won't bother posting code because I don't have a lot yet until I get my idea down set. But Heres what I want to do:
I'm trying to build an app where 2 people can write java code to run their bots from. Its sorta like AI but its going to be so downgraded you can't consider it. These players will have access to certain functions out of a playerLink class such as a Move function, attack function, etc...
1. I have a playerLink class that links to another class that holds a bunch of global variables. When I say link I mean it will be directly accessing functions from the global variables class. But the actual players can only call stuff from the playerLink, so they can't directly change anything in the global class.
2. I have the player class that is pretty much where the players will put their code. right now I'm trying to go real simple, allow them to move to certain places at no restrictions.
My problem comes when I'm thinking about the entire concept. How can I identify which player class is calling the playerLink? for example: if the red player calls moveForward() in playerlink, how does playerlink know its the red player without expecting the redplayer's class to say so? I'm trying to avoid them having to send any information of their own over like a variable that would tell playerClass which player they are.
I could easily force a variable to be sent that would signify which player is which, but I want it to be general so that if I wanted more than 2 players, maybe 4, 5. Maybe I don't want redPlayerClass, bluePlayerClass etc. Especially if the redPlayerclass knows the bluePlayerClass secret variable is 3, then it could move the bluePlayer at his own turn.
This may not explain it very well. If only I had 15 total posts I could send you a PM! Anyways, if your interested in giving me ideas let me know. I don't expect you to write up more code as you have been for me (even though its been amazing help). If you could just give me a good direction to go I can try to code it up myself, if it falls through I can come back. Thanks
MoncLast edited by Moncleared; 01-24-2009 at 12:31 AM.
- 01-24-2009, 12:31 AM #7
Member
- Join Date
- Jan 2009
- Posts
- 92
- Rep Power
- 0
Oh and Fubar,
Sticking all of my graphics in a class of its own has helped a tremendous amount. It seems more flexible because I can call mainPanel.repaint() anywhere in the class or even have it called from another class which helps a lot. Before I have having trouble doing that.
Just some background information on my Java Skills, I'm attending college and our professor started us out our freshmen year using Java 1.5, after the first 2 semesters he switched us to C and C++ and now he wants us to do our senior sem in Java, so I'm relearning a lot fast and all your help is great. Thanks again
Monc
Similar Threads
-
Difference between Thread.yield() and Thread.sleep() methods
By Nageswara Rao Mothukuri in forum New To JavaReplies: 12Last Post: 07-30-2010, 05:37 PM -
Sleep in thread
By jithan in forum New To JavaReplies: 1Last Post: 08-27-2008, 02:27 PM -
How to use sleep method of the Thread class
By Java Tip in forum java.langReplies: 0Last Post: 04-09-2008, 06:42 PM -
Can't get my thread to sleep!
By jamesfrize in forum New To JavaReplies: 2Last Post: 03-25-2008, 05:14 AM -
How to use the sleep and thread?
By jiuhu in forum Java AppletsReplies: 4Last Post: 08-07-2007, 02:56 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks