Sam
Sam

Reputation: 374

Free Drawing inside JPanel

I am new to Java. I want to create a program, in which i need to draw freely on JPanel by dragging the mouse. I go through the basics of paint function and able to achieve this.

public class DrawLine extends JPanel {  
public void paint(Graphics g)
{

g.drawLine(0, 0, 50, 50);   
}
public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable(){
        public void run()
        {
            JFrame frame=new JFrame("Top Level Demo");
            frame.setSize(300, 250);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            JPanel myPanel=new JPanel();
            myPanel.setLayout(null);
            frame.add(myPanel);
            frame.add(new DrawLine());
            frame.setVisible(true);

        }});
    }}

But this generates an out put like this where the straight line is determined by coordinates. enter image description here

Please some one help me to implement free drawing inside JPanel.

Upvotes: 0

Views: 2528

Answers (3)

subash
subash

Reputation: 3140

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 DrawLine extends JFrame implements MouseMotionListener {
    int x1,y1,x,y;
    private boolean first = true;
     public DrawLine() {
         super("Top Level Demo");
         setSize(300, 250);
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         setVisible(true);
         setBackground(Color.white);
         addMouseMotionListener(this);
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        x =x1; y = y1;
        x1 = e.getX();
        y1 = e.getY();
        if(first){
            x = x1;
            y = y1;
            first  = false;
        }
        repaint();
    }

    @Override
    public void mouseMoved(MouseEvent e) {

    }

    public void paint(Graphics graphics){
        graphics.drawLine(x, y, x1, y1);
    }

    public static void main(String[] args) {
        new DrawLine();
    }
}

Upvotes: 0

alex2410
alex2410

Reputation: 10994

Try to use MouseListener and paintComponent method.

Try next simple example for drawing your line.

public class Test extends JPanel{

public static int xS = 0;
public static int yS = 0;
public static int xF = 0;
public static int yF = 0;

public static void main(String[] args){

    JFrame frame = new JFrame("Movement of 2d Shapes");
    frame.setSize(400,400);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Test t = new Test();
    t.setOpaque(true);
    t.addMouseListener(getMouseListener(t));
    frame.getContentPane().add(t);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

}

private static MouseListener getMouseListener(final Test t) {
    return new MouseAdapter() {

        @Override
        public void mousePressed(MouseEvent arg0) {
            xS = arg0.getPoint().x;
            yS = arg0.getPoint().y;
        }

        @Override
        public void mouseReleased(MouseEvent arg0) {
            xF = arg0.getPoint().x;
            yF = arg0.getPoint().y;
            t.repaint();
        }
    };
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawLine(xS,yS, xF, yF);
}
}

Upvotes: 1

StanislavL
StanislavL

Reputation: 57381

Override paintComponent() rather than paint() method of JPanel.

Create list of Point to be used in the paintComponent().

Iterate the list in a loop and For each pair of points from the list call

g.drawLine(currentPoint.x,currentPoint.y, nextPoint.x,nextPoint.y); 

Add drag listening to store drag points in the list.

Upvotes: 2

Related Questions