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: Things Won't Move

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Things Won't Move

    What I want it to do is make the rectangle move across the screen. Here's my code:
    package game;
     
    import java.awt.*;
    import javax.swing.*;
     
    public class Panel extends JPanel {
     
    	private int txtx, txty;
    	private int rectx, recty;
    	private int ovalx, ovaly;
    	private boolean start, running;
     
    	public Panel() {
    		txtx = 50;
    		txty = 50;
     
    		rectx = 50;
    		recty = 80;
     
    		ovalx = 50;
    		ovaly = 210;
     
    		start = true;
     
    		setBackground(Color.WHITE);
    		setForeground(Color.BLACK);
    		setFont(new Font("Ariel", Font.PLAIN, 20));
     
    		run();
    	}
     
    	public void run(){
    		if(start){
    			running = true;
                            gameloop();
    		}
    	}
     
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		Graphics2D g2 = (Graphics2D) g;
     
    		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
    				RenderingHints.VALUE_ANTIALIAS_ON);
     
    		g2.drawString("diz iz mah texz", txtx, txty);
    		g2.setColor(Color.RED);
    		g2.fillRect(rectx, recty, 100, 100);
    		g2.setColor(Color.BLUE);
    		g2.fillOval(ovalx, ovaly, 100, 100);
    	}
     
    	public void gameloop() {
    		while (running) {
    			rectx++;
    			repaint();
     
    			try {
    				Thread.sleep(20);
    			} catch (Exception e) {
     
    			}
    		}
    	}
     
    }
     
    package game;
     
    import javax.swing.*;
     
    public class Game {
     
    	public static void main(String[] args) {
    		new Game();
    	}
     
    	public Game() {
    		JFrame f = new JFrame("My Graphics");
    		Panel p = new Panel();
     
    		f.setSize(800, 600);
    		f.setLocationRelativeTo(null);
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		f.add(p);
    		f.setVisible(true);
    	}
     
    }
    Last edited by zeraxis; September 2nd, 2011 at 08:32 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Things Won't Move

    Does you code ever call the gameloop method or the run method?
    Add printlns to those methods to see if they are ever called.

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Things Won't Move

    I tried that and they both were called.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Things Won't Move

    Did they return to allow the constructor to finish building the GUI and display it?

    The loop looks like it will go forever so the constructor never finishes and the GUI is not built.
    You need to put the forever loop in a new Thread and allow the constructor to exit.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Things Won't Move

    This looks like the same problem as this:
    http://www.javaprogrammingforums.com...html#post41496

Similar Threads

  1. A better way to do things?
    By SnarkKnuckle in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 18th, 2011, 12:52 PM
  2. XML, and other things.
    By Tortex in forum The Cafe
    Replies: 0
    Last Post: March 27th, 2010, 03:33 PM
  3. How would i do these things??
    By Curious in forum Java Theory & Questions
    Replies: 4
    Last Post: February 21st, 2010, 08:33 PM
  4. How to move an image (or how to delete one)
    By User in forum AWT / Java Swing
    Replies: 3
    Last Post: December 17th, 2009, 11:25 AM
  5. Want to move my button away from center.
    By Fendaril in forum AWT / Java Swing
    Replies: 2
    Last Post: November 6th, 2009, 10:05 PM