Results 1 to 4 of 4

Thread: Pong help

  1. #1
    jsobel is offline Member
    Join Date
    Apr 2012
    Posts
    68
    Rep Power
    0

    Question Pong help

    hi i am making a pong game and i cant get a rectangle drawn at the the right cords i want it to be (paddle_x, 360, 60, 80)
    but whenever i try creating my rectangle it says variable not found.

    import java.applet.*;
    import java.awt.*;

    import javax.swing.*;import java.awt.event.MouseEvent;


    import java.awt.event.MouseMotionAdapter;
    import java.awt.geom.*;
    import java.util.concurrent.*;

    public class Pong extends JApplet
    {
    public static final int WIDTH = 350;
    public static final int HEIGHT = 300;

    private PaintSurface canvas;

    public void init()
    {
    this.setSize(WIDTH, HEIGHT);
    canvas = new PaintSurface();
    this.add(canvas, BorderLayout.CENTER);

    ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(3);
    executor.scheduleAtFixedRate(new AnimationThread(this),
    0L, 20L, TimeUnit.MILLISECONDS);
    }
    }

    class AnimationThread implements Runnable
    {
    JApplet c;

    public AnimationThread(JApplet c)
    {
    this.c = c;
    }

    public void run()
    {
    c.repaint();
    }
    }

    class PaintSurface extends JComponent
    {
    int x_pos = 0;
    int y_pos = 0;
    int x_speed = 1;
    int y_speed = 1;
    int d = 20;
    int width = Pong.WIDTH;
    int height = Pong.HEIGHT;

    public void paint(Graphics g)
    {Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(
    RenderingHints.KEY_ANTIALIASING,
    RenderingHints.VALUE_ANTIALIAS_ON);
    if (x_pos < 0 )
    {
    x_speed = -x_speed;
    }
    if (x_pos > width - d)
    {
    x_speed = -x_speed;
    }

    if (y_pos < 0 )
    {
    y_speed = -y_speed;
    }

    x_pos += x_speed;
    y_pos +=y_speed;
    Shape ball = new Ellipse2D.Float(
    x_pos, y_pos, d, d);
    g2.setColor(Color.CYAN);
    g2.fill(ball);

    {
    addMouseMotionListener(new MouseMotionAdapter(){
    public void mouseMoved(MouseEvent e)
    {
    int paddle_x = e.getX() - 30;
    }});}}


    public void paintu(Graphics g)
    {Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(
    RenderingHints.KEY_ANTIALIASING,
    RenderingHints.VALUE_ANTIALIAS_ON);
    }}

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

    Default Re: Pong help

    How many times do you need to be given the same advice?
    http://www.java-forums.org/new-java/...tml#post276398

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

  3. #3
    jsobel is offline Member
    Join Date
    Apr 2012
    Posts
    68
    Rep Power
    0

    Default Re: Pong help

    can anyone tell me whats wrong with my new code

    import java.applet.*;
    import java.awt.*;

    import javax.swing.*;

    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionAdapter;
    import java.awt.geom.*;
    import java.util.concurrent.*;

    public class Pong extends JApplet
    {
    public static final int WIDTH = 350;
    public static final int HEIGHT = 300;

    private PaintSurface canvas;

    public void init()
    {
    this.setSize(WIDTH, HEIGHT);
    canvas = new PaintSurface();
    this.add(canvas, BorderLayout.CENTER);

    ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(3);
    executor.scheduleAtFixedRate(new AnimationThread(this),
    0L, 20L, TimeUnit.MILLISECONDS);
    }
    }

    class AnimationThread implements Runnable
    {
    JApplet c;

    public AnimationThread(JApplet c)
    {
    this.c = c;
    }

    public void run()
    {
    c.repaint();
    }
    }

    class PaintSurface extends JComponent
    {
    int x_pos = 0;
    int y_pos = 0;
    int x_speed = 1;
    int y_speed = 1;
    int d = 20;
    int width = Pong.WIDTH;
    int height = Pong.HEIGHT;


    public void paint(Graphics g)
    {Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(
    RenderingHints.KEY_ANTIALIASING,
    RenderingHints.VALUE_ANTIALIAS_ON);
    if (x_pos < 0 )
    {
    x_speed = -x_speed;
    }
    if (x_pos > width - d)
    {
    x_speed = -x_speed;
    }

    if (y_pos < 0 )
    {
    y_speed = -y_speed;
    }

    x_pos += x_speed;
    y_pos +=y_speed;
    Shape ball = new Ellipse2D.Float(
    x_pos, y_pos, d, d);
    g2.setColor(Color.CYAN);
    g2.fill(ball);
    {
    addMouseMotionListener(new MouseMotionAdapter(){
    public void mouseMoved(MouseEvent e)
    {
    int paddle_x = e.getX() - 30;
    System.out.println(paddle_x);
    int f = paddle_x;
    }});
    Shape bally = new Ellipse2D.Float(
    f, 9, d, d);
    g2.setColor(Color.CYAN);
    g2.fill(bally);
    }}};

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,234
    Rep Power
    23

    Default Re: Pong help

    Yes.
    It isn't in [code] tags [/code].
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. And another pong problem!
    By ResidentBiscuit in forum New To Java
    Replies: 1
    Last Post: 11-18-2011, 04:07 AM
  2. ping pong
    By ahmed_ijsh in forum Threads and Synchronization
    Replies: 2
    Last Post: 11-01-2010, 01:56 PM
  3. ping pong
    By ahmed_ijsh in forum Networking
    Replies: 0
    Last Post: 10-31-2010, 03:11 AM
  4. ping pong
    By ahmed_ijsh in forum Java Gaming
    Replies: 0
    Last Post: 10-31-2010, 03:10 AM
  5. Help with pong game
    By Eric in forum New To Java
    Replies: 2
    Last Post: 07-03-2007, 07:02 PM

Tags for this Thread

Posting Permissions

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