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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:34:58+00:00 2026-06-10T10:34:58+00:00

I have written the following micro-paintbrush program in Java: import java.awt.BorderLayout; import java.awt.Color; import

  • 0

I have written the following micro-paintbrush program in Java:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


class AuxClass1 extends JFrame implements MouseListener, MouseMotionListener{

    private JPanel panel1 = new JPanel();
    private JPanel panel2 = new JPanel();
    private JLabel label1_x = new JLabel();
    private JLabel label1_y = new JLabel();
    private JLabel label1_x_info = new JLabel("");
    private JLabel label1_y_info = new JLabel("");
    //add a container keep panels with widgets 
    private Container con1 = getContentPane();

    private int xval1;
    private int yval1;

    private GridLayout layout1 = new GridLayout(2,2,2,2);

    private JOptionPane info1 = new JOptionPane();

    //get the class that controls the mouse
    public AuxClass1(){
        super("Mouse Experiment");
        panel1.setBackground(Color.WHITE);      
        panel1.setLayout(layout1);
        label1_x.setText("X Location");
        label1_x.setBorder(BorderFactory.createLineBorder(Color.BLUE, 2));
        label1_y.setBorder(BorderFactory.createLineBorder(Color.BLUE, 2));
        label1_x_info.setBorder(BorderFactory.createLineBorder(Color.RED, 2));
        label1_y_info.setBorder(BorderFactory.createLineBorder(Color.RED, 2));
        label1_y.setText("Y Location");     
        panel1.add(label1_x);
        panel1.add(label1_y);
        panel1.add(label1_x_info);
        panel1.add(label1_y_info);
        con1.add(panel1, BorderLayout.NORTH);
        panel2.setBackground(new Color(100,200,200));
        panel2.setBorder(BorderFactory.createLineBorder(new Color(255,255,0), 2));
        panel2.addMouseListener(this);
        panel2.addMouseMotionListener(this);
        con1.add(panel2, BorderLayout.CENTER);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(500, 500);
        setLocationRelativeTo(null);
        setVisible(true);


    }

    @Override
    public void mouseClicked(MouseEvent arg0) {


    }

    @Override
    public void mouseMoved(MouseEvent arg0) {
        // TODO Auto-generated method stub
        if (arg0.getSource()==panel2){
            x_var = arg0.getX();
            y_var = arg0.getY();
            label1_x_info.setText(Integer.toString(x_var));
            label1_y_info.setText(Integer.toString(y_var));
        }

    }

    @Override
    public void mouseDragged(MouseEvent e) {
        // TODO Auto-generated method stub
        if (e.getSource()==panel2){
            //info1.showMessageDialog(this, "This is an awesome Mouse toolbox!");
            xval1 = e.getX();
            yval1= e.getY();
            AuxClass2 Inst2 = new AuxClass2(xval1, yval1);
            Inst2.paintComponent(getGraphics());
            }

    }

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

    }

    @Override
    public void mouseExited(MouseEvent arg0) {
        if (arg0.getSource()==panel2){
            label1_x_info.setText("");
            label1_y_info.setText("");
        }
        // TODO Auto-generated method stub

    }

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

    }

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

    }


}

class AuxClass2 extends JPanel{

    //JOptionPane info2 = new JOptionPane();
    private int xval2;
    private int yval2;

    public AuxClass2(int input1, int input2){

        xval2 = input1;
        yval2 = input2;
        setSize(500,500);

    }

    @Override
    public void paintComponent(Graphics g){
        super.paintComponents(g);
        g.setColor(Color.BLUE);
        g.fillRect(xval2, yval2+70, 5, 5);  

    }

}

public class MainClass{

    private static AuxClass1 Inst1;

    public static void main(String args[]){

        Inst1 = new AuxClass1();


    }

}

It works alright except for the Y coordinate of the mouseDragged method (see paintComponent method in class t3_aux2). For some reason the Y coordinate used by the method is ~70 pixels less than the actual one in panel2. I suspect this is something to do with the inherited JPanel method in t3_aux2 class but not too sure.

If someone could clarify this point, it would be cool. Thanks.

UPD: If anyone has suggestions on how to improve style and/or optimize code, that would be massively appreciated too.

UPD2: Changed the names to comply with Java naming conventions.

  • 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-10T10:35:00+00:00Added an answer on June 10, 2026 at 10:35 am

    I tried your code. I think your problem comes from the fact that you paint on the t3_aux1 using t3_aux2 coordinates. I’ll try something to confirm that, and I come back here …

    EDIT: OK, that’s it.

    in t3_aux1 constructor, if you write

    System.out.println("panel1 height = " + panel1.getHeight());
    System.out.println("label1 height = " + label1_x.getHeight())
    

    it prints

    panel1 height = 42
    label1 height = 20
    

    So your offset is 42 + 20 + 4*2 = 70

    4*2 comes from your lines borders with a thickness of 2.

    Since it’s possible to calculate the exact offset, you can dynamically fix it.

    EDIT 2 :

    In fact, the coordinates you uses come from panel2 since the mouseListener is attached to panel2. But you draw on the JFrame Graphics, not on panel2 graphics.

    writing this should fix your coordinates problem.

    inst2.paintComponent(panel2.getGraphics());
    

    But as Kleopatra said, you’re not doing it the right way. You should never call getGraphics() or paintComponent(). I think you should consider using java.awt.Canvas as a super class for your “panel2” object.

    One more advice : Be aware that your drawings aren’t memorized, so, if you reduce the window or hide it behind another one, everything drawn will be lost.

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

Sidebar

Related Questions

I have written following code to get location name package demo.gps.locname; import java.io.IOException; import
i have written the following code class Program { static void Main(string[] args) {
I have written following code for getting location name : UseGpsActivity.java public class UseGpsActivity
I have written following script in python which works fine: from sys import argv
I have written following code on Textbox Validated event : private void txtHbhakt1_Validated(object sender,
I am new to java and i have written the following: package mypackage; public
I have written following code for the client of RMI. But getting java.rmi.ConnectException: Connection
I am a newbie in Python. I have written following program and expecting all
I have written following code to attach gesture recogniser to multiple imageviews. [imageview1 setUserInteractionEnabled:YES];
I am trying my hands on WPF MVVM. I have written following code in

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.