Switch to full style
Graphics and animation Java code examples
Post a reply

draw any number of lines on mouse clicks

Tue Apr 16, 2013 9:14 pm

Drawing N lines when use mouse clicks on JFrame.
java code
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package testgraphices;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.event.MouseInputListener;

public class WindowFrame extends JFrame implements MouseInputListener {

private Point p1 = new Point();
private Point p2 = new Point();
private List list = new ArrayList();
private Line line;
private int clicks = 0;

public WindowFrame() {

setTitle("Drawing ");
addMouseListener(this);
setLocation(100, 100);
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.white);
g.fillRect(0, 0, 600, 600);
g.setColor(Color.red);
Line currLine;
for (int i = 0; i < list.size(); i++) {
currLine = (Line) (list.get(i));
g.drawLine(currLine.getP1().getX(), currLine.getP1().getY(),
currLine.getP2().getX(), currLine.getP2().getY());
}
}

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

@Override
public void mouseClicked(MouseEvent e) {

}

@Override
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();

if (clicks == 0) {
line = new Line();
line.setP1(new Point(x, y));

clicks++;
} else {
line.setP2(new Point(x, y));
list.add(line);
clicks = 0;
}
repaint();
}

@Override
public void mouseReleased(MouseEvent e) {
}

@Override
public void mouseEntered(MouseEvent e) {
}

@Override
public void mouseExited(MouseEvent e) {
}

@Override
public void mouseDragged(MouseEvent e) {
}

@Override
public void mouseMoved(MouseEvent e) {
}
}

class Point {

private int x;
private int y;

public Point() {
}

public Point(int x, int y) {
this.x = x;
this.y = y;
}

public int getX() {
return x;
}

public int getY() {
return y;
}

public void setX(int x) {
this.x = x;
}

public void setY(int y) {
this.y = y;
}
}

class Line {

private Point p1;
private Point p2;

public Line() {
}

public Line(Point p1, Point p2) {
this.p1 = p1;
this.p2 = p2;
}

public Point getP1() {
return p1;
}

public Point getP2() {
return p2;
}

public void setP1(Point p1) {
this.p1 = p1;
}

public void setP2(Point p2) {
this.p2 = p2;
}
}



Attachments
Line.gif
Line.gif (8.79 KiB) Viewed 6612 times

Post a reply
  Related Posts  to : draw any number of lines on mouse clicks
 draw any number of circles using mouse clicks     -  
 Applet get mouse clicks and draw circle     -  
 Draw Cardinal Spline With Mouse Clicks Points     -  
 Draw curves using Hermite Curve equations with mouse clicks     -  
 get time difference between to mouse-clicks-double clicks     -  
 Draw Triangle using Lines     -  
 increase the number of bank lines by <br/>     -  
 Draw Circles using Mid-Point Algorithm Equations- On Mouse     -  
 Draw Bezier Curve and selecting the points with mouse click     -  
 Draw Circles using using Polar form equations-On mouse Click     -  

Topic Tags

Java Graphics