import java.awt.*; import java.util.*; import java.awt.event.*; import java.awt.Dimension; import javax.swing.*; import javax.swing.event.*; public class Plotter extends JPanel implements ActionListener, MouseListener, MouseMotionListener { String[] sBNames = {"Line", "Square", "Rectangle", "Circle", "Oval", "toRobot","Clear"}; String[] sColors = {"BLACK", "RED", "ORANGE", "YELLOW", "GREEN", "BLUE","MAGENTA"}; JButton[] buttons = new JButton[sBNames.length]; JButton[] colors = new JButton[sColors.length]; String mode = "Line"; int colorMode = 0; Vector drawing = new Vector(); PLCanvas canvas = new PLCanvas(); int pressedX, pressedY, releasedX, releasedY; public Plotter() { super(new BorderLayout()); setPreferredSize(new Dimension(951, 620)); Dimension buttonSize = new Dimension(100,25); JPanel toolPanel = new JPanel(); toolPanel.setLayout(new BoxLayout(toolPanel, BoxLayout.Y_AXIS)); toolPanel.setPreferredSize(new Dimension(120,570)); JPanel optionPanel = new JPanel(); optionPanel.setLayout(new BoxLayout(optionPanel, BoxLayout.X_AXIS)); optionPanel.setPreferredSize(new Dimension(931,50)); canvas.addMouseListener(this); canvas.addMouseMotionListener(this); for(int i = 0; i < buttons.length; i++) { buttons[i] = new JButton(sBNames[i]); buttons[i].setSize(buttonSize); toolPanel.add(buttons[i]); buttons[i].setActionCommand(sBNames[i]); buttons[i].addActionListener(this); } for(int j = 0; j < colors.length; j++) { colors[j] = new JButton(sColors[j]); colors[j].setSize(buttonSize); toolPanel.add(colors[j]); colors[j].setActionCommand(sColors[j]); colors[j].addActionListener(this); } toolPanel.setBorder(BorderFactory.createTitledBorder("Draw Tools")); optionPanel.setBorder(BorderFactory.createTitledBorder("Options")); add(canvas, BorderLayout.CENTER); add(toolPanel, BorderLayout.WEST); add(optionPanel, BorderLayout.SOUTH); } public void actionPerformed(ActionEvent e) { for(int i = 0; i < sBNames.length-2; i++) { if (sBNames[i].equals(e.getActionCommand())) { mode = sBNames[i]; System.out.println(sBNames[i]); } } if (sBNames[5].equals(e.getActionCommand())) { Convert.toRobot(drawing); System.out.println(sBNames[5]); } if (sBNames[6].equals(e.getActionCommand())) { clear(); System.out.println(sBNames[6]); } for(int j = 0; j < sColors.length; j++){ if (sColors[j].equals(e.getActionCommand())) { Vector colorVector = new Vector(); colorVector.add(new java.awt.Point(-1, colorMode)); colorMode = j; colorVector.add(new java.awt.Point(-1, colorMode)); drawing.add(colorVector); System.out.println(sColors[j]); } } } private void clear() { drawing.clear(); this.repaint(); } //Handle mouse events. public void mouseReleased(MouseEvent e) { Tools tools = new Tools(); releasedX = e.getX(); releasedY = e.getY(); drawing.add(tools.draw(new java.awt.Point(pressedX,pressedY), new java.awt.Point(releasedX,releasedY), mode, 1)); canvas.setVector(drawing); this.repaint(); canvas.clear(); } public void mousePressed(MouseEvent e){ pressedX = e.getX(); pressedY = e.getY(); } public void mouseDragged(MouseEvent e){ int x = e.getX(); int y = e.getY(); PLCanvas.liveDraw(x, y, pressedX, pressedY, canvas, mode); this.repaint(); } public void mouseMoved(MouseEvent e){} public void mouseClicked(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} private static void createAndShowGUI() { JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("[Plotter] by B.Stoeller & F.Huizinga"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JComponent newContentPane = new Plotter(); newContentPane.setOpaque(true); //content panes must be opaque frame.setContentPane(newContentPane); frame.setResizable(false); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }