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 6113469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:51:45+00:00 2026-05-23T14:51:45+00:00

I got a problem I couldn’t get to work after about 2 Hours of

  • 0

I got a problem I couldn’t get to work after about 2 Hours of trying. I want to have a loop that do 2 Methods (Draw and update) but also listen to Mouse/Keyboard events. I have a loop that Draws and Updates, but does nothing outside of the loop ( Listening to events ) I tried alot of things but nothing worked. Help Please?

I tried using the Runnable Thread, using different orders, using wait() and notify(), I’ve tried alot of things. But basicly I want to know how to run a loop and still check for User Input

Also when I try to quit the program clicking the red “X”, it won’t quit but still work

Here’s the Code:

import java.applet.Applet;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class main extends Applet implements MouseListener, Runnable {

    public main() {
        super();
        init();
    }
    Thread t;
    Screen screen = new Screen();
    String Text = "Hello";
    boolean Running = true;
    boolean Click = false;
    int R = 0x00;
    int G = 0x00;
    int B = 0x00;
    int xpoints[] = {25, 40, 40, 25, 25};
    int ypoints[] = {40, 40, 25, 25, 25};
    int npoints = 5;

    public void run() {
        while (Running) {
            GameLoop();
        }
    }

    public void init() {
        this.addMouseListener(this);
        this.setSize(400, 300); //manually set your Frame's size
        t = new Thread(this);
        t.start();
    }

    public void paint(Graphics g) {
        g.setColor(new Color(R, B, G));
        g.fillPolygon(xpoints, ypoints, npoints);
        Running = true;
        t.run();
    }

    public void mousePressed(MouseEvent e) { //On Mouse Click
        System.exit(0);
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
        System.exit(0);
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mouseClicked(MouseEvent e) {
    }

    public boolean keyDown(Event e, int key) {
        return true;
    }

    public void GameLoop() {
        if (Running) {
            if (R != 0xff) {
                R++;
            } else {
                if (G != 0xff) {
                    G++;
                } else {
                    if (B != 0xff) {
                        B++;

                    } else {
                        System.exit(0);
                    }
                }
            }
            try {
                sleep(20);
            } catch (InterruptedException e) {

                e.printStackTrace();
            }
            paint(getGraphics());
        }
    }

    public void sleep(int time) throws InterruptedException {
        Thread.sleep(time, 0);
    }
}
  • 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-23T14:51:46+00:00Added an answer on May 23, 2026 at 2:51 pm

    This tutorial should provide some insight as to how your program should be structured. And this one is helpful for the mouse listener.

    Issues you should address:
    1) You’re doing something fishy with the paint method. Why are you calling t.run() in there? The thread t is already running and looping constantly calling the paint() method to redraw the screen. Remove this call and see what you get.

    1) The destruction of your thread/applciation is poor. The first example above provides a better way for that to occur

    2) You have your System.Exit(0) on mousePressed() with the comment //on mouse click but nothing in mouseClicked()… it works but its bad convention

    3)Having your class named main is extremely poor convention that is both confusing and impractical. Rename your class to something like “Game” or similar.

    4) Why declare Screen if you don’t use it?

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

Sidebar

Related Questions

Got a problem with a query I'm trying to write. I have a table
I have got a problem here in terminating the threads. The problem is that
The problem I am trying to solve is that I want to check all
I have got a problem about running codedui test which was generated from manual
I've got a bit of a problem aligning my buttons. I want to get
I got this problem. I have a form that retrieves a table data using
We got a problem with NUnit 2.5.3: nunit-console.exe does not return after finishing all
I've got a QT problem. I want to make my program stop at place
we've got a real confusing problem. We're trying to test an SQL Bulk Load
I've got a problem with passing templates (I think that's the cause) from the

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.