Skip to content

Instantly share code, notes, and snippets.

@DanielAndrews43
Created May 20, 2014 05:38
Show Gist options
  • Save DanielAndrews43/6f382351186efdd1deec to your computer and use it in GitHub Desktop.
Save DanielAndrews43/6f382351186efdd1deec to your computer and use it in GitHub Desktop.
public enum CurrentShape {
SQUARE,
CIRCLE,
RECTANGLE,
NONE;
}
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class DrawStuff extends JFrame implements MouseMotionListener, MouseListener, ActionListener{
//shape sizes
private static final int CANVAS_WIDTH = 800;
private static final int CANVAS_HEIGHT = 500;
private static final int CIRCLE_RADIUS = 150;
private static final int RECTANGLE_HEIGHT = 150;
private static final int RECTANGLE_WIDTH = 300;
private static final int SQUARE_SIZE = 200;
//shape numbers
private CurrentShape currentShape = CurrentShape.NONE;
private int currentX;
private int currentY;
//Canvas and panel declarations
JPanel btnPanel;
private BufferedImage canvasImage;
private CanvasDrawArea canvas;
public DrawStuff(){
//set up buttons for shapes
btnPanel = new JPanel(new FlowLayout());
createButtons(btnPanel);
//Create the drawing JPanel
canvas = new CanvasDrawArea();
canvas.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
//set up image area
canvasImage = new BufferedImage(CANVAS_WIDTH, CANVAS_HEIGHT, BufferedImage.TYPE_INT_ARGB);
//set up window
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
cp.add(canvas, BorderLayout.CENTER);
cp.add(btnPanel, BorderLayout.SOUTH);
//Sets up canvas (visible area)
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Draw With Shapes!");
pack();
setVisible(true);
requestFocus();
//MyListener myListener = new MyListener();
addMouseListener(this);
addMouseMotionListener(this);
}
/**
*
* Sets up the different buttons used
*
*/
private void createButtons(JPanel btnPanel){
JButton btnSquare = new JButton("Square");
btnPanel.add(btnSquare);
btnSquare.addActionListener(this);
JButton btnCircle = new JButton("Circle");
btnPanel.add(btnCircle);
btnCircle.addActionListener(this);
final JButton btnRectangle = new JButton("Rectangle");
btnPanel.add(btnRectangle);
btnRectangle.addActionListener(this);
final JButton btnClear = new JButton("Clear");
btnPanel.add(btnClear);
btnClear.addActionListener(this);
}
/**
* easy reset call
*/
public void reset(){
repaint();
requestFocus();
}
/**
* called every time a button is clicked
* sets up new shape for drawing
*/
@Override
public void actionPerformed(ActionEvent ae){
switch(ae.getActionCommand()){
case "Square":
currentShape = CurrentShape.SQUARE;
break;
case "Rectangle":
currentShape = CurrentShape.RECTANGLE;
break;
case "Circle":
currentShape = CurrentShape.CIRCLE;
break;
case "Clear":
//deletes old image and creates a new one
canvasImage = new BufferedImage(canvas.getWidth(), canvas.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
reset();
currentShape = CurrentShape.NONE;
break;
}
}
class CanvasDrawArea extends JPanel{
//maybe change to dynamic later?
//shape colors
private final Color CIRCLE_COLOR = Color.BLUE;
private final Color RECTANGLE_COLOR = Color.RED;
private final Color SQUARE_COLOR = Color.YELLOW;
private final Color CANVAS_COLOR = Color.GREEN;
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
canvas.setBackground(CANVAS_COLOR);
//uses a separate variable for graphics so they don't erase on each `paintComponent` call
Graphics2D image = (Graphics2D) canvasImage.getGraphics();
//larger border
image.setStroke(new BasicStroke(3));
//fills in the shape and then draws a border around it
switch(currentShape){
case RECTANGLE:
Rectangle2D rect = new Rectangle2D.Double(currentX - (RECTANGLE_WIDTH / 2),
currentY - (RECTANGLE_HEIGHT / 2), RECTANGLE_WIDTH, RECTANGLE_HEIGHT);
image.setColor(RECTANGLE_COLOR);
image.fill(rect);
image.setColor(Color.BLACK);
image.draw(rect);
break;
case SQUARE:
Rectangle2D rect1 = new Rectangle2D.Double(currentX - (SQUARE_SIZE / 2),
currentY - (SQUARE_SIZE / 2), SQUARE_SIZE, SQUARE_SIZE);
image.setColor(SQUARE_COLOR);
image.fill(rect1);
image.setColor(Color.BLACK);
image.draw(rect1);
break;
case CIRCLE:
image.setColor(CIRCLE_COLOR);
image.fillOval(currentX - (CIRCLE_RADIUS / 2), currentY - (CIRCLE_RADIUS / 2),
CIRCLE_RADIUS, CIRCLE_RADIUS);
image.setColor(Color.BLACK);
image.drawOval(currentX - (CIRCLE_RADIUS / 2), currentY - (CIRCLE_RADIUS / 2),
CIRCLE_RADIUS, CIRCLE_RADIUS);
break;
default:
}
g.drawImage(canvasImage, 0, 0, null);
}
}
/**
* called each time the mouse button is released
*/
@Override
public void mouseReleased(MouseEvent e) {
currentX = e.getX();
currentY = e.getY() - 22; //the 22 is to account for the little exit button on the top
reset();
}
@Override
public void mouseDragged(MouseEvent e) {}
@Override
public void mouseMoved(MouseEvent e) {}
@Override
public void mouseClicked(MouseEvent e) { }
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
}
import javax.swing.SwingUtilities;
public class Main {
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
new DrawStuff();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment