import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
public class AnalyseExample extends JFrame {
private JQDrawPanel drawPanel = new JQDrawPanel();
private JButton bClear = new JButton("clear board");
private JToolBar tb = new JToolBar();
private JToggleButton b1 = new JToggleButton("horizontal parallels");
private JToggleButton b2 = new JToggleButton("vertical parallels");
private JToggleButton b3 = new JToggleButton("freehand");
private ButtonGroup bg = new ButtonGroup();
public static void main(String[] args) {
new AnalyseExample();
}
public AnalyseExample() {
super("Analyse Example!");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationByPlatform(true);
this.setAlwaysOnTop(true);
bClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
drawPanel.handPointsList = null;
drawPanel.handPoints = null;
drawPanel.handPointsList = new ArrayList<List<Point>>();
drawPanel.handPoints = new ArrayList<Point>();
drawPanel.repaint();
}
});
bg.add(b1);
bg.add(b2);
bg.add(b3);
b3.setSelected(true);
tb.add(b1);
tb.add(b2);
tb.add(Box.createGlue());
tb.add(b3);
this.add(drawPanel, BorderLayout.CENTER);
this.add(bClear, BorderLayout.SOUTH);
this.add(tb, BorderLayout.NORTH);
this.pack();
this.setVisible(true);
}
class JQDrawPanel extends JPanel {
private Point mousePos = new Point();
private List<Point> points = new ArrayList<Point>();
private List<Point> handPoints = new ArrayList<Point>();
private List<List<Point>> handPointsList = new ArrayList<List<Point>>();
public JQDrawPanel() {
this.setPreferredSize(new Dimension(400, 400));
this.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
JQDrawPanel.this.repaint();
}
public void mousePressed(MouseEvent e) {
JQDrawPanel.this.repaint();
}
public void mouseReleased(MouseEvent e) {
if (b3.isSelected()) {
handPointsList.add(handPoints);
handPoints = new ArrayList<Point>();
}
JQDrawPanel.this.repaint();
}
public void mouseEntered(MouseEvent e) {
JQDrawPanel.this.repaint();
}
public void mouseExited(MouseEvent e) {
JQDrawPanel.this.repaint();
}
});
this.addMouseMotionListener(new MouseMotionListener() {
public void mouseDragged(MouseEvent e) {
if (b3.isSelected())
handPoints.add(e.getPoint());
JQDrawPanel.this.repaint();
}
public void mouseMoved(MouseEvent e) {
mousePos.setLocation(e.getPoint());
JQDrawPanel.this.repaint();
}
});
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// Draw freehand
g.setColor(Color.RED);
if (handPointsList != null)
for (List<Point> hps : handPointsList) {
Point lasthp = null;
for (Point h : hps) {
if (lasthp != null) {
g.drawLine(lasthp.x, lasthp.y, h.x, h.y);
}
lasthp = h;
}
}
// Draw Horizontal vs. Vertical lines
if (b1.isSelected()) {
g.setColor(Color.BLUE);
g.drawLine(mousePos.x - 50, mousePos.y - 10, mousePos.x + 50,
mousePos.y - 10);
g.drawLine(mousePos.x - 50, mousePos.y + 10, mousePos.x + 50,
mousePos.y + 10);
} else if (b2.isSelected()) {
g.setColor(Color.GREEN);
g.drawLine(mousePos.x - 10, mousePos.y - 50, mousePos.x - 10,
mousePos.y + 50);
g.drawLine(mousePos.x + 10, mousePos.y - 50, mousePos.x + 10,
mousePos.y + 50);
}
}
}
}