The attempt is to enable drawing of figures(a line for now) with mouse on the awt canvas . Iam trying out java graphics for the first time . So not sure how to go about it . This is my first attempt :
package def.grafi;
import java.awt.Canvas;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Dra {
Frame f = new Frame();
public void disp() {
f.setBounds(100, 100, 200, 200);
MosL ml = new MosL();
Can c = new Can();
f.add(c);
c.addMouseListener(ml);
c.addMouseMotionListener(ml);
f.setVisible(true);
}
public static void main(String[] args) {
Dra d = new Dra();
d.disp();
}
public class MosL extends MouseAdapter {
int sx = 0;
int sy = 0;
boolean onDrag = false;
@Override
public void mouseDragged(MouseEvent e) {
if (onDrag) {
int x = e.getX();
int y = e.getY();
Canvas comp = (Canvas) e.getSource();
Graphics g = comp.getGraphics();
// comp.repaint(); << for cleaning up the intermediate lines : doesnt work :(
g.drawLine(sx, sy, x, y);
return;
}
onDrag = true;
sx = e.getX();
sy = e.getY();
System.out.println("Draggg");
}
@Override
public void mousePressed(MouseEvent e) {
System.out.println("Pressed");
}
@Override
public void mouseReleased(MouseEvent e) {
System.out.println("Released");
if (onDrag)
onDrag = false;
}
}
public class Can extends Canvas {
@Override
public void paint(Graphics g) {
}
}
}
Problems : 1) When the windows is minimized and restored , the drawn line is gone (due to repaint) 2) What i want is the line should follow the mouse (when it is dragged) . the final line should extend from the point of pressing to the point of release of the mouse . Rite now , when the mouse moves , new lines are getting drawn . I am not sure how to clean up the intermediate lines from the canvas .
Can someone help me out on these problems ?
To draw these intersecting lines you need to first extend the view class that provides the onDraw() method where you will write code to draw lines. You will call the drawline() method of the Canvas class to draw the lines. The setColor() method will be provided by the Paint class to set the colors of both the lines.
Java Applet | Draw a line using drawLine() method x1 – It takes the first point's x coordinate. y1 – It takes first point's y coordinate. x2 – It takes second point's x coordinate. y2 – It takes second point's y coordinate.
drawLine. Draws a line, using the current color, between the points (x1, y1) and (x2, y2) in this graphics context's coordinate system. Parameters: x1 - the first point's x coordinate.
Here is a simple example of such "painting":
public static void main ( String[] args )
{
JFrame paint = new JFrame ();
paint.add ( new JComponent ()
{
private List<Shape> shapes = new ArrayList<Shape> ();
private Shape currentShape = null;
{
MouseAdapter mouseAdapter = new MouseAdapter ()
{
public void mousePressed ( MouseEvent e )
{
currentShape = new Line2D.Double ( e.getPoint (), e.getPoint () );
shapes.add ( currentShape );
repaint ();
}
public void mouseDragged ( MouseEvent e )
{
Line2D shape = ( Line2D ) currentShape;
shape.setLine ( shape.getP1 (), e.getPoint () );
repaint ();
}
public void mouseReleased ( MouseEvent e )
{
currentShape = null;
repaint ();
}
};
addMouseListener ( mouseAdapter );
addMouseMotionListener ( mouseAdapter );
}
protected void paintComponent ( Graphics g )
{
Graphics2D g2d = ( Graphics2D ) g;
g2d.setPaint ( Color.BLACK );
for ( Shape shape : shapes )
{
g2d.draw ( shape );
}
}
} );
paint.setSize ( 500, 500 );
paint.setLocationRelativeTo ( null );
paint.setVisible ( true );
}
it will remember all of the drawn shapes and with a small effort you can extend it to draw any other shapes you like.
Make use of Line2D object in the AWT package and do the following steps:
boolean
variable
to check if the click is the first or the
secondList
container to contain your Line2D
objectsCan
objectThe step number 5 could be achieved through:
e.getX()
e.getY()
Where e is the mouse event and could be accesed through the parameter of the mouse listener method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With