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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:37:49+00:00 2026-06-03T18:37:49+00:00

I am having a problem with a game that I am trying to make

  • 0

I am having a problem with a game that I am trying to make in Java. I am trying to attach a MouseListener to my canvas, however, when I click on the canvas, nothing happens. I think I may be attaching the MouseListener to the wrong thing, but I don’t know what to attach it to. I have tried attaching it to the JFrame and the canvas. Here is my code:

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

import java.io.*;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.util.Random;

public class Gravity extends Canvas {

    public static final int screenw = 1024;
    public static final int screenh = 768;

    public static Random gen = new Random();

    public static Boolean started = false;

    public static int[] starx = new int[100];
    public static int[] stary = new int[100];
    public static Color[] starc = new Color[100];

    public static JFrame frame;
    public static Gravity canvas;
    public static Image buffer;
    public static Graphics bg;

    public static int[] xh = new int[1000];
    public static int[] yh = new int[1000];

    public static int i = 0;

    public static Image title;

    public static ArrayList<Integer> ptx = new ArrayList<Integer>();
    public static ArrayList<Integer> pty = new ArrayList<Integer>();
    double x = 100;
    double y = 100;

    public Gravity(){
    }

    public void paint (Graphics g) {
        frame.addMouseListener(new MouseListener(){
            public void mouseClicked(MouseEvent e){
                started = true;
                System.out.println("Mouse was clicked");
            }

            public void mouseEntered(MouseEvent arg0) {}
            public void mouseExited(MouseEvent arg0) {}
            public void mousePressed(MouseEvent arg0) {}
            public void mouseReleased(MouseEvent arg0) {}
        });


        buffer = createImage(screenw, screenh);
        bg = buffer.getGraphics();

        int w = getWidth();
        int h = getHeight();

        double px = getWidth()/2; 
        double py = getHeight()/2;

        bg.setColor(Color.BLACK);
        bg.fillRect(0, 0, w, h); //black background

        for (int j=0; j < 100; j++){ //make stars
            starx[j] = gen.nextInt(w);
            stary[j] = gen.nextInt(h);
            starc[j] = new Color(gen.nextInt(100)+156, gen.nextInt(100)+156, gen.nextInt(100)+156);
            bg.setColor(starc[j]);
            bg.drawLine(starx[j], stary[j], starx[j]+2, stary[j]+2);
            bg.drawLine(starx[j], stary[j]+2, starx[j]+2, stary[j]);
        }

        try {
            title = ImageIO.read(new ByteArrayInputStream(Base64.decode(""))); //I have omitted the Base64 code for the image for my title screen
        } catch (IOException e) {
            e.printStackTrace();
        }

        bg.drawImage(title, 100, 100, null);
        g.drawImage(buffer, 0, 0, null);

        while (!started){
            try {
                Thread.sleep(50);
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }
        }

        double xvel = -15;
        double yvel = 10;

        for (int j=0; j < 100; j++){ //store stars
            starx[j] = gen.nextInt(w);
            stary[j] = gen.nextInt(h);
            starc[j] = new Color(gen.nextInt(100)+156, gen.nextInt(100)+156, gen.nextInt(100)+156);
        }

        Image test = createImage(200,200);
        Graphics testg = test.getGraphics();
        testg.drawLine(50,50,150,150);

        while(true){
            g.drawImage(buffer, 0,0, null);
            try {
                Thread.sleep(33);
            } catch (Exception e) {
                e.printStackTrace();
            }

            bg.setColor(Color.BLACK);
            bg.fillRect(0, 0, w, h); //black background


            for (int j=0; j < 100; j++){ //draw stars
                bg.setColor(starc[j]);
                bg.drawLine(starx[j], stary[j], starx[j]+2, stary[j]+2);
                bg.drawLine(starx[j], stary[j]+2, starx[j]+2, stary[j]);
            }

            bg.setColor(Color.BLUE);

            if (i > 0){
                for (int z=0; z < i-1; z++){
                    bg.drawLine(ptx.get(z), pty.get(z), ptx.get(z+1), pty.get(z+1));
                }
            }

            bg.setColor(Color.CYAN);
            bg.fillOval((int)px, (int)py, 25, 25); //planet

            bg.setColor(Color.RED);
            bg.fillRect((int)(x-5),(int)(y-5),10,10); //ship

            double fg = (5*50000)/(Math.pow(dist(x,y,px,py),2));

            double m = (y-py)/(x-px);
            double ms = Math.sqrt(Math.abs(m));
            if (m < 0) ms = -ms;

            double xchg = fg;
            double ychg = fg*ms;

            if (x > px){
                xchg = -xchg;
                ychg = -ychg;
            }

            xvel += xchg;
            yvel += ychg;

            x += xvel;
            y += yvel;

            ptx.add((int)x);
            pty.add((int)y);

            i++;
        }
    }

    public static void main(String[] args){

        canvas = new Gravity();
        frame = new JFrame();
        frame.setSize(screenw, screenh);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(canvas);

        frame.setVisible(true);
    }


    public static double dist(double x1, double y1, double x2, double y2){
        double x = x2-x1;
        double y = y2-y1;
        return Math.sqrt((x*x)+(y*y));
    }
}
  • 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-06-03T18:37:51+00:00Added an answer on June 3, 2026 at 6:37 pm

    General tips, in point form:

    1. Don’t mix AWT (e.g. Canvas) with Swing (e.g. JFrame) components. Instead of the Canvas, use a JPanel and override paintComponent(Graphics) rather than paint(Graphics).
    2. Don’t call Thread.sleep(n) on the EDT. Instead use a Swing based Timer to call repaint()
    3. Don’t perform long running operations on the paint method, especially starting an infinite loop! Even the creation of the buffer image should only be done in the case the screen size changes. (left as ‘TODO’ – BNI)
    4. Add the MouseListener once in the constructor or an init() method rather than every time paint is called.
    5. Highly recommend all of Nate’s numbered list of notes.

    Try this code, and compare it carefully to the original to see the changes.

    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Dimension;
    import java.awt.event.*;
    import java.util.ArrayList;
    
    import java.io.*;
    
    import javax.imageio.ImageIO;
    import javax.swing.*;
    import java.util.Random;
    
    public class Gravity extends JPanel {
    
        public static final int screenw = 800;
        public static final int screenh = 600;
    
        public static Random gen = new Random();
    
        public static int[] starx = new int[100];
        public static int[] stary = new int[100];
        public static Color[] starc = new Color[100];
    
        public static Image buffer;
        public static Graphics bg;
    
        public static int[] xh = new int[1000];
        public static int[] yh = new int[1000];
    
        public static int i = 0;
    
        public static ArrayList<Integer> ptx = new ArrayList<Integer>();
        public static ArrayList<Integer> pty = new ArrayList<Integer>();
        double x = 100;
        double y = 100;
    
        Timer timer;
    
        public Gravity(){
            // set thre PREFERRED size!
            setPreferredSize(new Dimension(screenw, screenh));
            addMouseListener(new MouseListener(){
                        public void mouseClicked(MouseEvent e){
                            System.out.println("Mouse was clicked");
                            timer.start();
                        }
    
                        public void mouseEntered(MouseEvent arg0) {}
                        public void mouseExited(MouseEvent arg0) {}
                        public void mousePressed(MouseEvent arg0) {}
                        public void mouseReleased(MouseEvent arg0) {}
            });
            ActionListener animation = new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent ae) {
                    repaint();
                }
            };
            timer = new Timer(50, animation);
        }
    
        @Override
        public void paintComponent(Graphics g) {
            buffer = createImage(screenw, screenh);
            bg = buffer.getGraphics();
    
            int w = getWidth();
            int h = getHeight();
    
            double px = getWidth()/2;
            double py = getHeight()/2;
    
            bg.setColor(Color.BLACK);
            bg.fillRect(0, 0, w, h); //black background
    
            for (int j=0; j < 100; j++){ //make stars
                starx[j] = gen.nextInt(w);
                stary[j] = gen.nextInt(h);
                starc[j] = new Color(gen.nextInt(100)+156, gen.nextInt(100)+156, gen.nextInt(100)+156);
                bg.setColor(starc[j]);
                bg.drawLine(starx[j], stary[j], starx[j]+2, stary[j]+2);
                bg.drawLine(starx[j], stary[j]+2, starx[j]+2, stary[j]);
            }
    
            g.drawImage(buffer, 0, 0, null);
    
            double xvel = -15;
            double yvel = 10;
    
            for (int j=0; j < 100; j++){ //store stars
                starx[j] = gen.nextInt(w);
                stary[j] = gen.nextInt(h);
                starc[j] = new Color(gen.nextInt(100)+156, gen.nextInt(100)+156, gen.nextInt(100)+156);
            }
    
            Image test = createImage(200,200);
            Graphics testg = test.getGraphics();
            testg.drawLine(50,50,150,150);
    
            g.drawImage(buffer, 0,0, null);
            try {
                Thread.sleep(33);
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            bg.setColor(Color.BLACK);
            bg.fillRect(0, 0, w, h); //black background
    
    
            for (int j=0; j < 100; j++){ //draw stars
                bg.setColor(starc[j]);
                bg.drawLine(starx[j], stary[j], starx[j]+2, stary[j]+2);
                bg.drawLine(starx[j], stary[j]+2, starx[j]+2, stary[j]);
            }
    
            bg.setColor(Color.BLUE);
    
            if (i > 0){
                for (int z=0; z < i-1; z++){
                    bg.drawLine(ptx.get(z), pty.get(z), ptx.get(z+1), pty.get(z+1));
                }
            }
    
            bg.setColor(Color.CYAN);
            bg.fillOval((int)px, (int)py, 25, 25); //planet
    
            bg.setColor(Color.RED);
            bg.fillRect((int)(x-5),(int)(y-5),10,10); //ship
    
            double fg = (5*50000)/(Math.pow(dist(x,y,px,py),2));
    
            double m = (y-py)/(x-px);
            double ms = Math.sqrt(Math.abs(m));
            if (m < 0) ms = -ms;
    
            double xchg = fg;
            double ychg = fg*ms;
    
            if (x > px){
                xchg = -xchg;
                ychg = -ychg;
            }
    
            xvel += xchg;
            yvel += ychg;
    
            x += xvel;
            y += yvel;
    
            ptx.add((int)x);
            pty.add((int)y);
    
            i++;
        }
    
        public static void main(String[] args){
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    JFrame frame = new JFrame();
    
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.getContentPane().add(new Gravity());
                    frame.setResizable(false);
                    frame.pack();
    
                    frame.setLocationByPlatform(true);
                    frame.setVisible(true);
                }
            });
        }
    
    
        public static double dist(double x1, double y1, double x2, double y2){
            double x = x2-x1;
            double y = y2-y1;
            return Math.sqrt((x*x)+(y*y));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For quite awhile I have been trying to make a simple game in Java
I'm trying to build a simple guessing game. I'm having a weird problem: -(int)setRandom
Im trying to make a quite simple game but its having an error. I
I'm trying to make a sort of game using the HTML5 canvas, JavaScript and
I am having a problem with my cocos2d game that supports retina display. Everything
I'm having some difficulties with the following problem: I'm making a little game where
I'm currently trying to make a RPG game and I'm running into some trouble.
Hey guys im having a problem trying to program out a set of logic.
I have read a book and am trying to make a game of tic-tac-toe
I'm trying to implement a fairly simple card game in Python so that two

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.