Results 1 to 4 of 4
  1. #1
    bossanova is offline Member
    Join Date
    Oct 2010
    Rep Power

    Default Having issues with GUI while loops

    The goal of this code is to make a ball go from right to left on a panel in a GUI, and then when it reaches the end of the window to go from left to right and continue to alternate like this infinitely. My real issue is getting the functions to work with while loops. Here is the part of the code I'm having trouble with so far.

    Java Code:
    	public void run() {
    		// TODO Auto-generated method stub
    		int flag = 0;
    			int width = tile.getParent().getWidth();
    			int tileWidth = tile.getWidth();
    			int x = tile.getX();
    			int y = tile.getY();
    			while(flag == 0){
    				tile.setLocation(x+10, y);
    				if(x == 80){
    					flag = 1;
    			while(flag == 1){
    				tile.setLocation(x-10, y);
    				if(x == 10){
    					flag = 0;
    		     try {Thread.sleep(50);} catch (InterruptedException e) {System.out.println("thread problem");}
    In this case, I want the ball to stop when it reaches x distance 80, turn around, stop at x distance 10, and turn around again (rinse and repeat). I originally had flag as a boolean, where I set it to true or false, but both seem to give the same result. When I run the code, the ball doesn't move. But if I take tile.setLocation out of any nested while loops, the ball will move infinitely in the positive or negative x direction. Am I missing/forgetting some basic concept of while loops that is messing this up?

    Also, is there any way to debug GUIs using Eclipse?

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


    You need to go through this tutorial:
    Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    and maybe these also:
    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    Trail: 2D Graphics (The Java™ Tutorials)

    To get better help sooner, post a SSCCE which demonstrates where you are having problems.
    SSCCE : Java Glossary


  3. #3
    bossanova is offline Member
    Join Date
    Oct 2010
    Rep Power


    I'm still having trouble with this. I'm just going to put my full code on here, since I think that will help more than anything. I'm very unfamiliar with the syntax of swing, and I'm unsure if using swing is what we are supposed to be doing (since I cannot recall going over anything like this in class). If anything, I'm just looking for a step in the right direction or a confirmation that swing is what I should be using.

    The main:
    Java Code:
    public class TheMainClass {
    	public static void main(String[] args) {
    		MyGUI gui = new MyGUI();
    Here is the GUI class:
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class MyGUI extends JFrame implements Runnable {
    	private JButton button1 = new JButton("Press to Stop"); //#1
    	private JPanel peterPanel = new JPanel();
    	private Tile tile = new Tile();
    	private boolean move = true;
    	private JLabel hit = new JLabel("Click count: 0");
    	private int hitCount = 0;
    	private Thread fredTheThread = new Thread(this);
    	public MyGUI() { 
    		// this refers to "this" MyFirstGui object that is executing this   constructor.
    		this.setLayout(null); // no layout manager. I will layout the components myself
    		this.setTitle("This is my very first GUI!");
    		Font font = new Font("Arial", Font.BOLD, 20);
    		 * You need to do 5 things to make a component show up in your GUI
    		 * 1) Instantiate it (usually you want to make it a private instance field)
    		 * 2) Tell it to set its size
    		 * 3) Tell it to set its location
    		 * 4) Tell it to be visible
    		 * 5) Tell some container to add it. "this" MyFirstGui is a container.
    		button1.setSize(350, 50); // #2
    		button1.setLocation(170, 400); // #3
    		button1.setVisible(true); // #4
    		this.add(button1); // #5
    		button1.setBackground(Color.lightGray); // doesn't work on a Mac :(
    		peterPanel.setLayout(null); // tell the JPanel that we will layout its components.
    		peterPanel.setSize(675, 350); // #2
    		peterPanel.setLocation(10, 10); // #3
    		peterPanel.setVisible(true); // #4
    		this.add(peterPanel); // #5
    		tile.setLocation(10, 100);
    		 * This code instantiates a listener and then registers it to our button1
    		ButtonListener ears = new ButtonListener(); // #2 to create a listener
    		button1.addActionListener(ears); //#3 to create a listener
    	} // end of MyGUI constructor
    	// inner class we will use to instantiate a listener for a button    #1 to create a listener
    	public class ButtonListener implements ActionListener {   	
    		public void actionPerformed(ActionEvent arg0) {
    			System.out.println("Just here to test this listener");
    	}// end of ButtonListener class
    	public void run() {
    		// TODO Auto-generated method stub
    			int width = tile.getParent().getWidth();
    			int tileWidth = tile.getWidth();
    			int x = tile.getX();
    			int y = tile.getY();
    			tile.setLocation(x+10, y);
    			try {Thread.sleep(50);} catch (InterruptedException e) {System.out.println("thread problem");}
    }// end of MyGUI class
    Here is the tile class:
    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JComponent;
    public class Tile extends JComponent {
    	public void paintComponent(Graphics g){
    		// Change the tile's appearance if you like.
    		g.fillOval(0, 0, this.getWidth(), this.getHeight());
    Again I'm looking to take the tile and make it move to the right edge of the panel, once it hits the edge reverses direction, and once it hits the left edge, reverses direction again, and continues to do this infinitely. This code I posted above has the tile moving to the right infinitely using the setLocation(x+10, y) line.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    A snippet of your code with place-holder comments
    Java Code:
          while (true) {
             int width = tile.getParent().getWidth(); // #1
             int tileWidth = tile.getWidth();     // #1
             int x = tile.getX();    // #1
             int y = tile.getY();    // #1
             tile.setLocation(x + 10, y); // #2
    #1: you're getting a lot of useful data at these four spots, but you're not doing anything with it. You need to use this data with an if test or two to see if tile is at or beyond one of the boundaries.

    #2: You've hard-coded exactly how tile will move. With this code it will always move 10 blips in a positive direction no matter what. You're far better off using an int field here in place of the int literal 10. Then you can change the sign of the int field/variable depending on what the if blocks I mentioned above find. Also, the "stop" button could change this variable to 0, effectively stopping the tile.

    Myself, I'd not use a separate thread but rather a Swing Timer for my Game loop.

Similar Threads

  1. Help with loops
    By pg5678pg in forum New To Java
    Replies: 8
    Last Post: 10-17-2010, 06:51 PM
  2. need some help with loops!
    By Chewart in forum New To Java
    Replies: 2
    Last Post: 12-03-2009, 11:32 PM
  3. when should we use loops
    By shahemaan in forum New To Java
    Replies: 1
    Last Post: 10-31-2009, 01:38 AM
  4. these loops...
    By Blaedel in forum New To Java
    Replies: 0
    Last Post: 10-01-2009, 06:59 PM
  5. how to use do while loops
    By mikeitalydz in forum New To Java
    Replies: 32
    Last Post: 09-26-2009, 08:30 PM

Posting Permissions

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