java lover
java lover

Reputation: 39

how can I give the action to "MouseListener" to draw a shape in java?

this is my code I want to draw the rectangle at the position that I clicked but it is not drawing any thing :( , I don't know how I giving the action to "MouseListener"

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Transparenttxom extends JPanel implements MouseListener {

Graphics g=null;

public void init(){
    Transparenttxom panel=new Transparenttxom();
    panel.addMouseListener(this);
    }//end init
//********************************************************************
        public void paint(Graphics g){
            }
//********************************************************************
public void mouseClicked(MouseEvent e){
    int mousex=e.getX();
    int mousey=e.getY();

    g.drawRect(20,20,mousex,mousey);

    }//end mouseclicked method
//********************************************************************
    public void mouseEntered(MouseEvent e){
        }
        public void mouseExited(MouseEvent e){
        }
        public void mousePressed(MouseEvent e){
        }
        public void mouseReleased(MouseEvent e){
        }
//********************************************************************
    public static void main(String[] args) {
        Transparenttxom panel=new Transparenttxom();
                    JFrame frame = new JFrame("java lover");
                             frame.add(panel);
                            frame.setSize(300, 300);
                            frame.setVisible(true);
                            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
}

thanks for helping.

Upvotes: 1

Views: 1115

Answers (3)

camickr
camickr

Reputation: 324098

Check out Custom Painting Approaches for two common methods to do custom painting:

  1. Keep an ArrayList of objects to paint
  2. Paint onto a BufferedImage

In both examples the mouseReleased code actually saves the object to be painted. In your case you want to add the Rectangle on mousePressed.

Upvotes: 0

Mohsen Kamrani
Mohsen Kamrani

Reputation: 7457

Add listener like this:

public Transparenttxom() {
    // TODO Auto-generated constructor stub
    super();
    this.addMouseListener(this);
}

Your init method never gets called. and g is null!

and try this:

public void mouseClicked(MouseEvent e){
    int mousex=e.getX();
    int mousey=e.getY();
    Graphics g = this.getGraphics();
    g.drawRect(20,20,mousex,mousey);

}//end mouseclicked method

The whole code :

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Transparenttxom extends JPanel implements MouseListener {

    Graphics g=null;

    public Transparenttxom() {
        // TODO Auto-generated constructor stub
        super();
        this.addMouseListener(this);
    }
    public void init(){
        Transparenttxom panel=new Transparenttxom();
        System.out.println("no");
        panel.addMouseListener(this);
    }//end init
    //********************************************************************
    public void paint(Graphics g){
    }
    //********************************************************************
    public void mouseClicked(MouseEvent e){
        int mousex=e.getX();
        int mousey=e.getY();
        Graphics g = this.getGraphics();
        g.drawRect(20,20,mousex,mousey);

    }//end mouseclicked method
    //********************************************************************
    public void mouseEntered(MouseEvent e){
    }
    public void mouseExited(MouseEvent e){
    }
    public void mousePressed(MouseEvent e){
    }
    public void mouseReleased(MouseEvent e){
    }
    //********************************************************************
    public static void main(String[] args) {
        Transparenttxom panel=new Transparenttxom();
        JFrame frame = new JFrame("java lover");
        frame.add(panel);
        frame.setSize(300, 300);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
}

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347184

I would start with the fact the Graphics g=null; and since you've never assigned it anything, it's likely to remain null.

Then I'd move onto the point where init is never called, but your init method scares me...

public void init(){
    Transparenttxom panel=new Transparenttxom();
    panel.addMouseListener(this);
}//end init

Why are you creating a new instance of Transparenttxom? Simple call addMouseListener(this)...

But even then, this...

public void paint(Graphics g){
}

Means nothing will ever get painting...

Start by taking a look at Performing Custom Painting for more details

Instead overriding paint, you should override paintComponent, making sure you call super.paintComponent before you do any painting of your own.

In your mouseClicked event you should define what you want painted and simply call repaint which will trigger a paint event which will, eventually, call your paintComponent where in which you paint stuff.

Updated with a basic example

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Transparenttxom extends JPanel implements MouseListener {

    private Point mousePoint;

    public Transparenttxom() {
        addMouseListener(this);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (mousePoint != null) {
            g.drawRect(20, 20, mousePoint.x - 20, mousePoint.y - 20);
        }
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(200, 200);
    }



    public void mouseClicked(MouseEvent e) {
        mousePoint = e.getPoint();
        repaint();
    }//end mouseclicked method

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }
//********************************************************************

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new Transparenttxom());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

Upvotes: 2

Related Questions