Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import java.awt.geom.*;
- import java.util.ArrayList;
- import javax.swing.*;
- class Draw extends JFrame implements ActionListener, ItemListener {
- static final ArrayList<MyShape> shapes = new ArrayList<>();
- // Initial Frame size
- static final int FWIDTH = 600; // frame width
- static final int FHEIGHT = 450; // frame height
- // Color choices
- static final String COLOR_NAMES[] = {"None", "Red", "Blue", "Green"};
- static final Color COLORS[] = {null, Color.red, Color.blue, Color.green};
- // Button control
- JButton circle;
- JButton roundRec;
- JButton threeDRec;
- // Color choice box
- JComboBox<String> colorChoice;
- JList<String> jList1;
- JScrollPane jScrollPane2;
- // the canvas
- DrawCanvas canvas;
- /**
- * Constructor
- */
- public
- Draw() {
- super("Java Draw");
- setLayout(new BorderLayout());
- // create panel for controls
- JPanel topPanel = new JPanel(new GridLayout(2, 1));
- add(topPanel, BorderLayout.NORTH);
- // create button control
- JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
- topPanel.add(buttonPanel);
- circle = new JButton("Circle");
- buttonPanel.add(circle);
- roundRec = new JButton("Rounded Rectangle");
- buttonPanel.add(roundRec);
- threeDRec = new JButton("3D Rectangle");
- buttonPanel.add(threeDRec);
- // add button listener
- circle.addActionListener(this);
- roundRec.addActionListener(this);
- threeDRec.addActionListener(this);
- // create panel for color choices
- JPanel colorPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
- topPanel.add(colorPanel);
- JLabel label = new JLabel("Filled Color:");
- colorPanel.add(label);
- colorChoice = new JComboBox();
- for (int i = 0; i < COLOR_NAMES.length; i++) {
- colorChoice.addItem(COLOR_NAMES[i]);
- }
- colorPanel.add(colorChoice);
- colorChoice.addItemListener(this);
- JPanel listPanel = getListPanel();
- add(listPanel, BorderLayout.WEST);
- // create the canvas
- canvas = new DrawCanvas();
- add(canvas, BorderLayout.CENTER);
- } // end of constructor
- /**
- * Implementing ActionListener
- */
- @Override public void actionPerformed(ActionEvent event) {
- if (event.getSource() == circle) { // circle button
- canvas.setShape(DrawCanvas.CIRCLE);
- } else if (event.getSource() == roundRec) { // rounded rectangle button
- canvas.setShape(DrawCanvas.ROUNDED_RECTANGLE);
- } else if (event.getSource() == threeDRec) { // 3D rectangle button
- canvas.setShape(DrawCanvas.RECTANGLE_3D);
- } else if (event.getActionCommand().equals("Clear All")) {
- for (int i = 0; i < shapes.size(); i++) {
- shapes.remove(i);
- }
- updateShapeList();
- canvas.repaint();
- } else if (event.getActionCommand().equals("Remove Shape")) {
- String name = jList1.getSelectedValue();
- for (int i = 0; i < shapes.size(); i++) {
- if (shapes.get(i).toString().equals(name)) {
- shapes.remove(i);
- break;
- }
- }
- updateShapeList();
- canvas.repaint();
- }
- }
- /**
- * Implementing ItemListener
- * @param event
- */
- @Override public void itemStateChanged(ItemEvent event) {
- Color color = COLORS[colorChoice.getSelectedIndex()];
- canvas.setFilledColor(color);
- }
- /**
- * the main method
- */
- public
- static void main(String[] argv) {
- // Create a frame
- Draw frame = new Draw();
- frame.setSize(FWIDTH, FHEIGHT);
- frame.setLocation(150, 100);
- // add window closing listener
- frame.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent event) {
- System.exit(0);
- }
- });
- // Show the frame
- frame.setVisible(true);
- }
- private
- JPanel getListPanel() {
- JPanel p = new JPanel(new GridLayout(3, 1));
- p.setSize(100, 300);
- jScrollPane2 = new JScrollPane();
- jList1 = new JList<>();
- updateShapeList();
- jScrollPane2.setViewportView(jList1);
- p.add(jScrollPane2);
- JButton removeShape = new JButton("Remove Shape");
- removeShape.addActionListener(this);
- p.add(removeShape);
- JButton Clearall = new JButton("Clear All");
- Clearall.addActionListener(this);
- p.add(Clearall);
- return p;
- }
- /**
- * When ever a new item added or removed from the list
- * then we need to update the model of the list.
- */
- public
- void updateShapeList() {
- jList1.setModel(new javax.swing.AbstractListModel<String>() {
- String[] shapenames = getMenuNames();
- private
- String[] getMenuNames() {
- int i = 0;
- String[] names = new String[shapes.size() + 1];
- for (MyShape m : shapes) {
- names[i++] = m.toString();
- }
- return names;
- }
- public
- int getSize() { return shapenames.length; }
- @Override public String getElementAt(int i) {
- return shapenames[i];
- }
- });
- }
- class DrawCanvas extends Canvas implements MouseListener, MouseMotionListener {
- // Constants for shapes
- public
- static final int CIRCLE = 1;
- public
- static final int ROUNDED_RECTANGLE = 2;
- public
- static final int RECTANGLE_3D = 3;
- // Coordinates of points to draw
- private
- int x1, y1, x2, y2;
- // shape to draw
- private
- int shape = CIRCLE;
- private
- final int PAINT = 0; // do we need to draw current component
- private
- final int NOPAINT = 1; // we dont need to draw current componet
- private
- int paintOrRepaint = NOPAINT; // at the beginning we dont need to draw
- /**
- *Method to set the shape
- *@param shape
- */
- public
- void setShape(int shape) { this.shape = shape; }
- // filled color
- private
- Color filledColor = null;
- /**
- *Method to set filled color
- *@param color
- */
- public
- void setFilledColor(Color color) { filledColor = color; }
- /**
- *Constructor
- */
- public
- DrawCanvas() {
- addMouseListener(this);
- addMouseMotionListener(this);
- } // end of constructor
- /**
- *painting the component
- */
- public
- void paint(Graphics g) {
- // the drawing area
- int x, y, width, height;
- // determine the upper-left corner of bounding rectangle
- x = Math.min(x1, x2);
- y = Math.min(y1, y2);
- // determine the width and height of bounding rectangle
- width = Math.abs(x1 - x2);
- height = Math.abs(y1 - y2);
- // current shape : by default it is a line after using a switch we update
- // this to
- // circle of rectangle
- Shape cur_shape = new Line2D.Double(x1, y1, x2, y2);
- switch (shape) {
- case ROUNDED_RECTANGLE:
- cur_shape = new RoundRectangle2D.Double(x, y, width, height, width / 4,
- height / 4);
- break;
- case CIRCLE:
- int diameter = Math.max(width, height);
- cur_shape = new Ellipse2D.Double(x, y, diameter, diameter);
- break;
- case RECTANGLE_3D:
- cur_shape = new Rectangle2D.Double(x, y, width, height);
- break;
- }
- // if we need to paint this component
- // first add it to the shapes list in the Draw class
- if (paintOrRepaint == PAINT) {
- MyShape sh = new MyShape(cur_shape, filledColor, shape);
- Draw.shapes.add(sh);
- }
- Graphics2D graphics = (Graphics2D)g;
- for (MyShape s : Draw.shapes) {
- if (s.fill != null)
- g.setColor(s.fill);
- else
- g.setColor(Color.BLACK);
- if (s.fill != null)
- graphics.fill(s.shape);
- else
- graphics.draw(s.shape);
- }
- updateShapeList(); // update the Jlist in the GUI
- setPaintOrRepaint(
- NOPAINT); // make the current componet not to draw next time
- }
- /**
- *Implementing MouseListener
- *@param event
- */
- public
- void mousePressed(MouseEvent event) {
- x1 = event.getX();
- y1 = event.getY();
- }
- public
- void mouseReleased(MouseEvent event) {
- x2 = event.getX();
- y2 = event.getY();
- // we have a new shape(componet) to draw
- // so set the paintOrRepaint to PAINT
- // so that repaint method can draw this
- // shape on the canvas
- setPaintOrRepaint(PAINT);
- repaint();
- }
- public
- void mouseClicked(MouseEvent e) {}
- public
- void mouseEntered(MouseEvent e) {}
- public
- void mouseExited(MouseEvent e) {}
- /**
- *Implementing MouseMotionListener
- *@param event
- */
- @Override public void mouseDragged(MouseEvent event) {
- x2 = event.getX();
- y2 = event.getY();
- }
- public
- void mouseMoved(MouseEvent e) {}
- private
- void setPaintOrRepaint(int option) { paintOrRepaint = option; }
- }
- }
- // class shape will store the details about the
- // shape that we want to draw and do we need to
- // fill it or not
- class MyShape {
- Shape shape;
- Color fill;
- String shapeName;
- MyShape(Shape shape, Color filledColor, int shapeName) {
- this.shape = shape;
- this.fill = filledColor;
- this.shapeName = getName(shapeName);
- }
- private
- String getName(int shapeName) {
- switch (shapeName) {
- case 1:
- return "Circle";
- case 2:
- return "Round Rectangle";
- case 3:
- return "Rectangle 3D";
- default:
- return "NoshapeName";
- }
- }
- public
- String toString() {
- return shapeName + "(" + shape.getBounds().x + "," + shape.getBounds().y +
- ")";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement