Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Repaint method in Thread not moving String

  1. #1
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Question Repaint method in Thread not moving String

    The String I'm painting will move...but only if I minimize and maximize the window. It's not repainting automatically. How come?

    import java.lang.Thread;
    import java.awt.*;
    import java.util.*;
    import java.io.*;
    import javax.swing.*;
     
     
     
    public class Melissa extends Thread 
    {
    Background b;
    private class Background extends JFrame
    {
    private String text;
    public Background(String name)
    {
    super(name);
    setVisible(true);
    setString(name);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
     
    public void paint(Graphics g)
    {
    super.paint(g);
    Random r = new Random();
    Random r2 = new Random();
    int x = r.nextInt(300);
    int y = r.nextInt(300);
     
    int red = r2.nextInt(256);
    int green = r2.nextInt(256);
    int blue = r2.nextInt(256);
     
     
    g.setColor(new Color(red,green,blue));
    g.drawString(getString(), x, y);
     
    }
     
    public void setString(String text)
    {
    this.text = text;
    }
     
    public String getString()
    {
    return text;
    }
    }
    public Melissa(String name)
    {
    super(name);
    b = new Background(name);
     
    System.out.println(this);
    start();
    }
     
    public void run() {
    		//Display info about this particular thread
    		System.out.println(Thread.currentThread().getName());
    	try
    	{
    		Thread.sleep(9000);
    		b.repaint();
    	}
     
    	catch(InterruptedException ieRef)
    	{
    	System.out.println("Error!");
    	}
     
     
    	}
     
    public static void main(String[] args)
    {
    Scanner console = new Scanner(System.in);
     
    System.out.println("Enter text");
    String text3 = console.nextLine();
     
    Melissa m2 = new Melissa(text3);
    m2.run();
     
    }
     
    }


  2. #2
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Repaint method in Thread not moving String

    The way you structured that program annoys me. Anyways, in order to update the image without max/min-imizing you need an infinite loop that calls repaint().

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Cool Re: Repaint method in Thread not moving String

    Quote Originally Posted by Brt93yoda View Post
    The way you structured that program annoys me. Anyways, in order to update the image without max/min-imizing you need an infinite loop that calls repaint().

    Ok. But will that run me out of memory?


  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Repaint method in Thread not moving String

    Could you tell me how to stop that crazy bouncing 10000 times a second?

  5. #5
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Repaint method in Thread not moving String

    while(true)
    {
        //do all of the updates
     
      Thread.sleep(16);  //60 frames per second
    }
    sleep isn't exact, but it should work for what you're doing.

  6. The Following User Says Thank You to Brt93yoda For This Useful Post:

    javapenguin (February 10th, 2011)

Similar Threads

  1. Using Thread waiting() method
    By nicoeschpiko in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 11th, 2010, 11:55 AM
  2. Thread Hangs at split method.
    By kailasvilaskore in forum Threads
    Replies: 1
    Last Post: November 26th, 2010, 12:13 PM
  3. Getting a string value from within a method in another class
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 11th, 2010, 04:17 PM
  4. string method
    By dstha in forum Object Oriented Programming
    Replies: 3
    Last Post: February 25th, 2010, 08:07 PM
  5. wheres the thread about inverted String?
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: July 29th, 2009, 11:13 AM