Results 1 to 4 of 4
  1. #1
    Krock999 is offline Member
    Join Date
    Jul 2013
    Posts
    10
    Rep Power
    0

    Angry Apparently, Time Moves My Icon..

    My code glitches out and doesn't move in one direction. Anyhow, thank you for helping me with the code below.

    Here is my code:

    Java Code:
    import java.awt.Color;
    import java.awt.Dimension;
    import java.util.concurrent.TimeUnit;
    
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    
    public class TestForLast {
    	static public int WIDTH = 800;
    	static public int HEIGHT = 800;
    	final static public int MAX_WIDTH = 900;
    	final static public int MAX_HEIGHT = 650;
    	static public int countUp = 0;
    	static public int bound = 400;
    	public static ImageIcon Enemy= new ImageIcon("C:\\Users\\Student\\Desktop\\Enemy.png");
      public static void waiting (int n){
            
            long t0, t1;
    
            t0 =  System.currentTimeMillis();
    
            do{
                t1 = System.currentTimeMillis();
            }
            while ((t1 - t0) < (n * 1000));
        }
    	public static void main(String[] args) throws InterruptedException {
    		JFrame Screen = new JFrame("AI Test");
    		JLabel Enem = new JLabel(Enemy);
    		Enem.setBounds(0,0,bound,bound);
    		Enem.setVisible(true);
    		Enem.setSize(200,200);
    		Screen.add(Enem);
    		Screen.setVisible(true);
    		Screen.getContentPane().setBackground(Color.cyan);
    		Screen.setLocation(650,225);
    		Screen.setResizable(true);
    		Screen.setSize(WIDTH, HEIGHT);
    		Screen.setPreferredSize(new Dimension(MAX_WIDTH,MAX_HEIGHT));
    		Screen.setEnabled(true);
    		while(0 == 0) {
    			waiting(1); // This also doesn't work.
    			bound = bound + 10; // When this is commented out, the glitch still happens but it doesn't move 10.
    			Enem.setBounds(0, 0, bound, bound);
    			//Thread.sleep(333); // When this is turned off, it moves slightly and the glitch is gone.
    			//Extra : TimeUnit.SECONDS.sleep(1);
    	}
    	}
    
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Apparently, Time Moves My Icon..

    Get rid of the busy loop and learn How to Use Swing Timers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features). Also follow recommendations in the Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing) with regard to invoking Swnig constructors and methods on the EDT.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Krock999 is offline Member
    Join Date
    Jul 2013
    Posts
    10
    Rep Power
    0

    Default Re: Apparently, Time Moves My Icon..

    You didn't answer my question.. If you can, just show me a bit of the code which will stop the first one pointer glitch.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Apparently, Time Moves My Icon..

    Did you go through the tutorials I linked? If you have, post your updated code that uses the knowledge you gained there.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. So apparently sin((pi * 0) / 180)) * 0 * -1 = 1
    By falconfetus8 in forum New To Java
    Replies: 1
    Last Post: 12-11-2011, 06:30 AM
  2. Replies: 7
    Last Post: 07-12-2011, 05:19 PM
  3. java gui that moves image based on time
    By androidsm in forum New To Java
    Replies: 10
    Last Post: 01-16-2011, 01:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •