Results 1 to 9 of 9

Thread: keybindings

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

    Question keybindings

    i am making a program and i need a variable d to increase by 1 if the h key is pressed and decrease by 1 if the g key is pressed can anyone help me i am making a pong game

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: keybindings

    Recommended reading: Key Bindings Java Tips Weblog
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: keybindings

    Insufficient context. Command line? AWT? Swing? other GUI toolkit?

    The question as posted doesn't even state that this is a Java program.

    Recommended reading: How to ask questions the smart way


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

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

    Default Re: keybindings

    swing but anyway is fine
    my code is below

    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);
    int d;
    d = 8; //I want d to be controlled by g and h
    Shape bally = new Rectangle.Float(d,250, 50, 8);
    g2.setColor(Color.CYAN);
    g2.fill(bally);
    }}

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: keybindings

    Oh look.
    Still no [code] tags [/code].
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: keybindings

    plz help

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: keybindings

    Quote Originally Posted by jsobel View Post
    plz help
    Did you read the link I gave you? Did you read the link that Darryl gave you? Did you try to add code tags? We did help you. You ignored that help.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: keybindings

    plz uz cd tgs.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: keybindings

    Quote Originally Posted by Tolls View Post
    plz uz cd tgs.
    s/pl/a/
    s/pl/e/
    s/z/se/g
    s/c/co/
    s/d/de/
    s/t/ta/

    glad I could be of help ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. KeyBindings Empty Key?
    By Unome in forum Java Applets
    Replies: 1
    Last Post: 10-24-2008, 08:28 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
  •