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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:32:21+00:00 2026-05-24T20:32:21+00:00

I’ve been trying to do this assignment and for it to work I need

  • 0

I’ve been trying to do this assignment and for it to work I need the event mousePressed to work but for some reason it’s not responding to the mouse. Its purpose is to paint another yellow circle when the mouse is pressed.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;

public class CatchMonster extends JPanel
{
    private int height = 300;
    private int width = 600;
    private final int delay = 4001;

    private ImageIcon image;
    private Timer timer;
    private int x, y, moveX, moveY, xPoint, yPoint;

public CatchMonster() {

    DotListener dot = new DotListener();
    addMouseListener(dot);


    timer = new Timer(delay, new timerListener());
    x = 40;
    y = 40;

    moveX = moveY = 3;
    setPreferredSize(new Dimension(width, height));
    setBackground(Color.black);
    timer.start();

}

public void paintComponent(Graphics g) {
    super.paintComponents(g);
    g.setColor(Color.yellow);
    g.fillOval(x, y, 60, 60);
}

private class timerListener implements ActionListener 
{
    public void actionPerformed(ActionEvent e) {
        Random gn = new Random();
        x = gn.nextInt(width);
        y = gn.nextInt(height);

        repaint();
    }
    }

private class DotListener implements MouseListener
{
    public void mousePressed(MouseEvent event)
    {
        repaint();
    }

    @Override
    public void mouseClicked(MouseEvent event) {


    }

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

    }

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

    }

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

    }


}

}

  • 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-24T20:32:22+00:00Added an answer on May 24, 2026 at 8:32 pm

    I need the event mousePressed to work but for some reason it’s not responding to the mouse. Its purpose is to paint another yellow circle when the mouse is pressed.

    But it won’t paint another circle as all it does is call repaint(), and how is that going to paint anything new? If you want it to create another circle, you’ll have to give it logic to do so. For instance, if you want to paint more than one yellow oval, you’ll want to create an ArrayList of Point objects and add a Point into that array list in the mousePressed method. Then in the paintComponent method, you can iterate through the array list, painting ovals for each Point it contains.

    In addition, you want to change this:

       public void paintComponent(Graphics g) {
          super.paintComponents(g); // this is not the "super" method of paintComponent
          g.setColor(Color.yellow);
          g.fillOval(x, y, 60, 60);
       }
    

    to this:

       public void paintComponent(Graphics g) {
          super.paintComponent(g); // See the difference?
          g.setColor(Color.yellow);
          g.fillOval(x, y, 60, 60);
       }
    

    For example:

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Point;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    
    public class CatchMonster extends JPanel {
       private int height = 300;
       private int width = 600;
       private final int delay = 4001;
    
       private ImageIcon image;
       private Timer timer;
       private int x, y, moveX, moveY, xPoint, yPoint;
       private List<Point> points = new ArrayList<Point>();
    
       public CatchMonster() {
    
          DotListener dot = new DotListener();
          addMouseListener(dot);
    
          timer = new Timer(delay, new timerListener());
          x = 40;
          y = 40;
    
          moveX = moveY = 3;
          setPreferredSize(new Dimension(width, height));
          setBackground(Color.black);
          timer.start();
    
       }
    
       public void paintComponent(Graphics g) {
          super.paintComponent(g);
          g.setColor(Color.yellow);
          g.fillOval(x, y, 60, 60);
    
          int radius = 30;
          g.setColor(Color.green);
          for (Point p : points) {
             int x = p.x - radius;
             int y = p.y - radius;
             g.fillOval(x, y, 2 * radius, 2 * radius);
          }
       }
    
       private class timerListener implements ActionListener {
          public void actionPerformed(ActionEvent e) {
             Random gn = new Random();
             x = gn.nextInt(width);
             y = gn.nextInt(height);
    
             repaint();
          }
       }
    
       public static void main(String[] args) {
          JFrame frame = new JFrame("Foo");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(new CatchMonster());
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       private class DotListener extends MouseAdapter {
          public void mousePressed(MouseEvent event) {
             points.add(event.getPoint());
             repaint();
          }
    
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I need a function that will clean a strings' special characters. I do NOT
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.