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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:39:00+00:00 2026-06-10T14:39:00+00:00

This is a follow-up to my previous question. I’ve simplified things as much as

  • 0

This is a follow-up to my previous question. I’ve simplified things as much as I could, and it still doesn’t work! Although the good thing I got around using getGraphics().

A detailed explanation on what goes wrong here is massively appreciated. My suspicion is that something’s wrong with the the way I used addMouseListener() method here.

EDIT completely rewrote the code. Still does not work properly though.

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;


public class RunClass{

    static MainClass1 inst1 = new MainClass1();



    public static void main(String args[]){

        JFrame frame1 = new JFrame();
        frame1.add(inst1);
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame1.setTitle("NewPaintToolbox");
        frame1.setSize(200, 200);
        frame1.setLocationRelativeTo(null);     
        frame1.setVisible(true);                
    }
}

class MainClass1 extends JPanel implements MouseListener, MouseMotionListener{

    int xvar=30;
    int yvar=30;
    //static PaintClass22 inst1 = new PaintClass22();


    @Override
    public void mouseClicked(MouseEvent arg0) {
        // TODO Auto-generated method stub
        xvar = arg0.getX();
        yvar = arg0.getY();
        repaint(xvar,yvar,10,10);

    }   

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

    }

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

    }

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

    }

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

    }

    @Override
    public void mouseDragged(MouseEvent arg0) {
        // TODO Auto-generated method stub
        xvar = arg0.getX();
        yvar = arg0.getY();
        repaint(xvar,yvar,10,10);

    }

    @Override
    public void mouseMoved(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }
    @Override
    public void paintComponent(Graphics g){

        super.paintComponent(g);
        g.setColor(Color.RED);
        g.fillRect(xvar, yvar, 10, 10);     

    }



}
  • 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-10T14:39:02+00:00Added an answer on June 10, 2026 at 2:39 pm

    You must add the mouseListener to the panel. That doesn’t happen by default as you might have expected 😉

    MainClass1() {
        addMouseListener(this);
    }
    

    BTW: it’s not recommended to expose public api that’s only meant to be used internally. So instead of letting the panel implement MouseListener (which enforces the public exposure), let the panel create and use a MouseListener:

    private MouseListener mouseListener;
    MainClass1() {
       mouseListener = createMouseListener();
       addMouseListener(mouseListener);
    }
    
    protected MouseListener createMouseListener() {
        MouseListener l = new MouseListener() {
    
        }
       return l;
    }
    

    BTW 2: calling the repaint on the limited area isn’t exactly what you want (?) – it temporarily adds the squares to the painting, they are lost whenever the whole panel is repainted (same effect as with getGraphics). Depending on what you really want,

    • paint a single square at the most recently clicked position: call repaint()
    • paint squares at all locations ever clicked: store the locations in a list and implement repaint to loop over that list. Here you may call the repaint with parameters, but why bother?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a follow on from my previous question although this is about something
This is a follow-up to my previous question . I still find it very
This is a follow up to my previous question . My code is still
This is a follow up question to a previous question I asked about calculating
This is a follow up from my previous question I have this code basically
This is a follow-up to my previous question . Suppose I am writing the
This is a follow-up to my previous question I would like to find a
This is a follow up to my previous question: Problem passing parameters via Iframe
This is a follow on to my previous question: JSP / servlet / IE
This question is a follow up with a previous question Previous Question The previous

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.