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

Thread: In need of assistance!

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    11
    My Mood
    Amazed
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Exclamation In need of assistance!

    I'm trying to make the classic game of Mini Tennis, but can't seem to overcome this problem...
    This is the code:
    package java_1;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    @SuppressWarnings("serial")
    public class MiniTennis_5 extends JPanel {
     
    	Ball ball = new Ball(this);
    	Racquet racquet = new Racquet(this);
     
    	public MiniTennis_5() {
    		addKeyListener(new KeyListener() {
    			@Override
    			public void keyTyped(KeyEvent e) {
    			}
     
    			@Override
    			public void keyReleased(KeyEvent e) {
    				racquet.keyReleased(e);
    			}
     
    			@Override
    			public void keyPressed(KeyEvent e) {
    				racquet.keyPressed(e);
    			}
    		});
    		setFocusable(true);
    	}
     
    	private void move() {
    		ball.move();
    		racquet.move();
    	}
     
    	@Override
    	public void paint(Graphics g) {
    		super.paint(g);
    		Graphics2D g2d = (Graphics2D) g;
    		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
    				RenderingHints.VALUE_ANTIALIAS_ON);
    		ball.paint(g2d);
    		racquet.paint(g2d);
    	}
     
    	public static void main(String[] args) throws InterruptedException {
    		JFrame frame = new JFrame("Mini Tennis");
    		MiniTennis_5 game = new MiniTennis_5();
    		frame.add(game);
    		frame.setSize(300, 400);
    		frame.setVisible(true);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		while (true) {
    			game.move();
    			game.repaint();
    			Thread.sleep(10);
    		}
    	}
    }

    I compiled, but there was some kind of error while running it. I've tried debugging it, but I can't find the problem...


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: In need of assistance!

    please paste the error message

  3. The Following User Says Thank You to dicdic For This Useful Post:

    Nexcit (March 10th, 2014)

  4. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: In need of assistance!

    Yes, we need to see the error, but there are other problems that should be corrected:

    1 - A while(true) loop on the main thread (or any, for that matter) is not the way to stimulate a Swing app. However, it is very effective at causing the Swing app to become locked up and unresponsive.

    2 - Swing apps should be run on the EDT.

    3 - Do not override the paint() method in Swing apps. Instead, override the paintComponent() method. You can learn more about the proper way to paint in Swing apps by reviewing the "Swing custom painting" tutorial.

    4 - Use a container like a JFrame as the main app frame/window, and to that add a JPanel or other JComponent on which to do the painting and animation.

    There are other minor issues to resolve - like program structure - but this is a good start.

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    Nexcit (March 10th, 2014)

  6. #4
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: In need of assistance!

    In my opinion you might be better off by first going through a simple Java game tutorial. E.g., you can go through Space Invaders 101 | Coke And Code. You can also look through the follow-up tutorial at Space Invaders 102 | Coke And Code for a better way to handle the game loop.

    On EDT (Event Dispatching Thread), see Event dispatching thread - Wikipedia, the free encyclopedia and Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing).

    On custom painting, see Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing). While you're at it, notice the way example programs in this tutorial are started:

        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }

    You'd understand the significance of this if you have gone through the EDT links above.

  7. The Following 2 Users Say Thank You to jashburn For This Useful Post:

    GregBrannon (March 10th, 2014), Nexcit (March 10th, 2014)

  8. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    11
    My Mood
    Amazed
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: In need of assistance!

    I'll try to fix the problems in the code and will try out the simpler Java Tutorials as jashburn has suggested and will post the errors right here:
    Exception in thread "main" java.lang.NullPointerException
    at java_1.Ball.move(Ball.java:22)
    at java_1.MiniTennis_5.move(MiniTennis_5.java:36)
    at java_1.MiniTennis_5.main(MiniTennis_5.java:59)


    Also, as you can see there is "at java_1.Ball.move(Ball.java:22). And that is referring to another code which is this:

    package java_1;
    import java.awt.Graphics2D;
     
    public class Ball {
    	int x = 0;
    	int y = 0;
    	int xa = 1;
    	int ya = 1;
    	private MiniTennis_4 game;
     
    	public Ball(MiniTennis_4 game) {
    		this.game= game;
    	}
     
    	public Ball(MiniTennis_5 miniTennis_5) {
    		// TODO Auto-generated constructor stub
    	}
     
    	void move() {
    		if (x + xa < 0)
    			xa = 1;
    		if (x + xa > game.getWidth() - 30)
    			xa = -1;
    		if (y + ya < 0)
    			ya = 1;
    		if (y + ya > game.getHeight() - 30)
    			ya = -1;
     
    		x = x + xa;
    		y = y + ya;
    	}
     
    	public void paint(Graphics2D g) {
    		g.fillOval(x, y, 30, 30);
    	}
    }

    I'll try out the other Tutorials and fix the code...

  9. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: In need of assistance!

    You'll have to tell us which line is line 22, but look at that line and determine which variables have not been initialized and fix that.

Similar Threads

  1. [SOLVED] Homework assistance
    By hhman1 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 29th, 2013, 06:16 PM
  2. [SOLVED] for loop assistance
    By Kseidel in forum Loops & Control Statements
    Replies: 3
    Last Post: September 17th, 2012, 08:10 PM
  3. Need some assistance please
    By JavaPhish in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 25th, 2012, 10:00 AM
  4. New to the complmunity, need some assistance please.
    By nakedtriple in forum What's Wrong With My Code?
    Replies: 12
    Last Post: October 7th, 2011, 06:14 AM
  5. FileReader need assistance
    By tazjaime in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: November 8th, 2009, 01:12 AM

Tags for this Thread