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 10 of 10

Thread: Need Some Help...

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

    Default Need Some Help...

    When I run this code nothing happens and all I want it to do is make the rectangle move across the screen.

    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);
    	}
     
    }
     
    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 running;
     
    	public Panel() {
    		txtx = 50;
    		txty = 50;
     
    		rectx = 50;
    		recty = 80;
     
    		ovalx = 50;
    		ovaly = 210;
     
    		running = true;
     
    		setBackground(Color.WHITE);
    		setForeground(Color.BLACK);
    		setFont(new Font("Ariel", Font.PLAIN, 20));
     
    		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) {
     
    			}
    		}
    	}
     
    }

  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Need Some Help...

    "Need some help" provides zero information.
    "My code doesn't work" provides zero information.
    Improving the world one idiot at a time!

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

    Default Re: Need Some Help...

    Well, that's all there is. When I run it nothing happens.

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

    Default Re: Need Some Help...

    Add lots of println statements to the code in all of the methods to see where the execution flow goes.
    You need to see where the code is executing and where it is not executing.

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

    Default Re: Need Some Help...

    Alright, I'll try that.

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

    Default Re: Need Some Help...

    Well, I checked and it goes into the game loop, but for some reason my window doesn't appear.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Need Some Help...

    it goes into the game loop
    Does it come out of the gameloop method?
    If it never exits the gameloop method, then it never returns from the Panel class's constructor and it nevers finishes the Game class's constructor.

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

    Default Re: Need Some Help...

    Okay, any suggestions on how to end the loop?

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Need Some Help...

    Don't enter an infinite loop until the initialization is finished.
    Better to Create a Thread and do the infinite loop there.

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

    Default Re: Need Some Help...

    K, thanks.