Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5998187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:22:12+00:00 2026-05-23T00:22:12+00:00

Here is my code: public class Main extends Applet implements Runnable, KeyListener, java.awt.event.MouseListener {

  • 0

Here is my code:

public class Main extends Applet implements Runnable, KeyListener,
    java.awt.event.MouseListener {
int x_pos = 300;
int y_pos = 200;
int radius = 20;
int appletsize_x = 600;
int appletsize_y = 400;
double x_speed = 0;
double y_speed = 0;

private Image dbImage;
private Graphics dbg;

public void init() {
    this.setSize(600, 400);

}

public void start() {

    this.addKeyListener(this);
    this.addMouseListener(this);
    Thread th = new Thread(this);
    th.start();
}

public void stop() {

}

public void destroy() {

}

public void run() {

    // lower ThreadPriority
    this.requestFocusInWindow();
    Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

    while (true) {

        repaint();
        x_pos += x_speed;
        y_pos += y_speed;

        // Hitting (right)
        if (x_pos > this.getSize().width - radius) {

            //x_speed = -x_speed;
            x_speed = 0;

        }
        // Hitting (left)
        if (x_pos < 0 + radius) {

            //x_speed = -x_speed;
            x_speed = 0;

        }
        // Hitting top
        if (y_pos < 0 + radius) {


            //y_speed = -y_speed;
            y_speed = 0;

        }

        // Hitting bottom
        if (y_pos > this.getSize().height - radius) {

            //y_speed = -y_speed;
            y_speed = 0;

        }

        try {
            // Stop thread for 1 milliseconds
            Thread.sleep(20);
        } catch (InterruptedException ex) {
            // do nothing
        }

        // set ThreadPriority to maximum value
        Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

    }
}

public void paint(Graphics g) {
    // set colour
    g.setColor(Color.red);

    // paint a filled coloured circle
    g.fillOval(x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
}

public void update(Graphics g) {

    if (dbImage == null) {
        dbImage = createImage(this.getSize().width, this.getSize().height);
        dbg = dbImage.getGraphics();
    }

    dbg.setColor(getBackground());
    dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);

    dbg.setColor(getForeground());
    paint(dbg);

    g.drawImage(dbImage, 0, 0, this);
}

@Override
public void keyPressed(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_LEFT) {
        if (x_speed > 0) {
            x_speed = +x_speed;
            y_speed = 0;
        }
        if (x_speed == 0) {
            x_speed = -4;
            y_speed = 0;
        }
        if (x_speed < 0) {
            x_speed = +x_speed;
            y_speed = 0;
        }
    }
    if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
        if (x_speed < 0) {
            x_speed = -x_speed;
            y_speed = 0;

            if (x_speed > 0) {
                x_speed = -x_speed;
                y_speed = 0;
            }
        }
        if (x_speed == 0) {
            x_speed = 4;
            y_speed = 0;
        }

    }
    if (e.getKeyCode() == KeyEvent.VK_UP) {
        if (y_speed > 0) {
            y_speed = -y_speed;
            x_speed = 0;
        }
        if (y_speed < 0) {
            y_speed = +y_speed;
            x_speed = 0;
        }
        if (y_speed == 0) {
            y_speed = -4;
            x_speed = 0;
        }
    }
    if (e.getKeyCode() == KeyEvent.VK_DOWN) {
        if (y_speed > 0) {
            y_speed = +y_speed;
            x_speed = 0;
        }
        if (y_speed < 0) {
            y_speed = -y_speed;
            x_speed = 0;
        }
        if (y_speed == 0) {
            y_speed = +4;
        }
        x_speed = 0;
    }
}

@Override
public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseClicked(MouseEvent e) {
    System.out.println("HIT!");
    // TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub

}
         }

How will I make the ball stop moving when no buttons are being pressed?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T00:22:12+00:00Added an answer on May 23, 2026 at 12:22 am

    Under your keyReleased method, you should be able to do the opposite of your keyPressed method. That is, if you press the right arrow key, add 1 to x_speed, and when you release it, subtract 1 from x_speed, using similar logic for the other keys.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

here is my example code: Public Class Parent Private _TestProperty As String Private WithEvents
Here is a VB.NET code snippet Public Class OOPDemo Private _strtString as String Public
Ok, here is the code and then the discussion follows: public class FlatArrayList {
I have seen the following code: [DefaultValue(100)] [Description(Some descriptive field here)] public int MyProperty{...}
What did I do wrong? Here is an excerpt from my code: public void
Using the obsolete System.Web.Mail sending email works fine, here's the code snippet: Public Shared
Here is the code currently used. public String getStringFromDoc(org.w3c.dom.Document doc) { try { DOMSource
So here is the simple code: [System.ComponentModel.DefaultValue(true)] public bool AnyValue { get; set; }
Edit: The code here still has some bugs in it, and it could do
See here: http://code.google.com/p/ie7-js/ Does anyone have any experience or remarks about this javascript? Is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.