Skip to content

Instantly share code, notes, and snippets.

@allisons
Created January 29, 2011 03:28
Show Gist options
  • Save allisons/801478 to your computer and use it in GitHub Desktop.
Save allisons/801478 to your computer and use it in GitHub Desktop.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class ShapeControl extends JPanel {
private ButtonListener buttonListener;
private ShapeMouseListener mouseListener;
private DrawingBoard drawingboard;
private GraphicView graphicview;
public ShapeControl(DrawingBoard db) {
drawingboard = db;
setLayout(new BorderLayout());
graphicview = new GraphicView(db);
graphicview.setPreferredSize(new Dimension(DrawingBoard.viewSize, DrawingBoard.viewSize));
add(graphicview, BorderLayout.SOUTH);
mouseListener = new ShapeMouseListener(db);
graphicview.addMouseListener(mouseListener);
drawingboard.addShapeListener(graphicview);
JButton triangle = new JButton("Draw Triangle");
JButton circle = new JButton("Draw Circle");
JPanel buttons = new JPanel();
buttons.add(triangle);
buttons.add(circle);
triangle.setBackground(Color.darkGray);
circle.setBackground(Color.darkGray);
this.add(buttons, BorderLayout.NORTH);
buttonListener = new ButtonListener(db);
triangle.addActionListener(buttonListener);
circle.addActionListener(buttonListener);
}
}
class ButtonListener implements ActionListener {
private DrawingBoard drawingboard;
public ButtonListener(DrawingBoard db) {
drawingboard = db;
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Draw Triangle")){
drawingboard.setCirClicked(false);
drawingboard.setRTClicked(true);
}
if(e.getActionCommand().equals("Draw Circle")){
drawingboard.setRTClicked(false);
drawingboard.setCirClicked(true);
}
}
}
class ShapeMouseListener implements MouseListener {
private DrawingBoard drawingboard;
public ShapeMouseListener(DrawingBoard db) {
drawingboard = db;
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
if(drawingboard.RTClicked){
drawingboard.addShape(new RightTriangle(new Point(e.getX(), e.getY()),80));
}
if(drawingboard.cirClicked){
drawingboard.addShape(new Circle(new Point(e.getX(), e.getY()),80));
}
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment