Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package paint;
- import javax.swing.*;
- import java.awt.event.*;
- import java.awt.*;
- import java.awt.geom.*;
- import java.util.*;
- @SuppressWarnings("serial")
- /**
- *
- * @author ramytamer
- */
- public final class Paint extends JFrame {
- JButton lineBtn, circleBtn, ellipseBtn, rectangleBtn, squareBtn, triangleBtn, colorBtn;
- public JButton getSquareBtn() {
- return squareBtn;
- }
- public void setSquareBtn(JButton squareBtn) {
- this.squareBtn = squareBtn;
- }
- public JButton getLineBtn() {
- return lineBtn;
- }
- public void setLineBtn(JButton lineBtn) {
- this.lineBtn = lineBtn;
- }
- public JButton getCircleBtn() {
- return circleBtn;
- }
- public void setCircleBtn(JButton circleBtn) {
- this.circleBtn = circleBtn;
- }
- public JButton getEllipseBtn() {
- return ellipseBtn;
- }
- public void setEllipseBtn(JButton ellipseBtn) {
- this.ellipseBtn = ellipseBtn;
- }
- public JButton getRectangleBtn() {
- return rectangleBtn;
- }
- public void setRectangleBtn(JButton rectangleBtn) {
- this.rectangleBtn = rectangleBtn;
- }
- public JButton getTriangleBtn() {
- return triangleBtn;
- }
- public void setTriangleBtn(JButton triangleBtn) {
- this.triangleBtn = triangleBtn;
- }
- public JButton getColorBtn() {
- return colorBtn;
- }
- public void setColorBtn(JButton colorBtn) {
- this.colorBtn = colorBtn;
- }
- public Graphics2D getGraphSettings() {
- return graphSettings;
- }
- public void setGraphSettings(Graphics2D graphSettings) {
- this.graphSettings = graphSettings;
- }
- public int getDrawAction() {
- return drawAction;
- }
- public void setDrawAction(int drawAction) {
- this.drawAction = drawAction;
- }
- public Color getColour() {
- return colour;
- }
- public void setColour(Color colour) {
- this.colour = colour;
- }
- Graphics2D graphSettings;
- int drawAction = 1;
- Color colour = Color.BLACK;
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- Paint paint = new Paint();
- }
- public Paint() {
- this.setSize(800, 670);
- this.setResizable(false);
- this.setTitle("The awesome paint program");
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- JPanel btnPanel = new JPanel();
- JPanel colorBtnPanel = new JPanel();
- Box btnsBox = Box.createVerticalBox();
- Box colorBtnBox = Box.createVerticalBox();
- setLineBtn(addBtn("./src/line.png", 1, false));
- setCircleBtn(addBtn("./src/circle.png", 2, false));
- setEllipseBtn(addBtn("./src/ellipse.png", 3, false));
- setRectangleBtn(addBtn("./src/rectangle.png", 4, false));
- setSquareBtn(addBtn("./src/square.png", 5, false));
- setTriangleBtn(addBtn("./src/triangle.png", 6, false));
- setColorBtn(addBtn("./src/color.png", 7, true));
- btnsBox.add(getLineBtn());
- btnsBox.add(getCircleBtn());
- btnsBox.add(getEllipseBtn());
- btnsBox.add(getRectangleBtn());
- btnsBox.add(getSquareBtn());
- btnsBox.add(getTriangleBtn());
- colorBtnBox.add(getColorBtn());
- btnPanel.add(btnsBox);
- colorBtnPanel.add(colorBtnBox);
- this.add(btnPanel, BorderLayout.WEST);
- this.add(colorBtnPanel, BorderLayout.EAST);
- this.add(new DrawingBoard(), BorderLayout.CENTER);
- this.setVisible(true);
- }
- public JButton addBtn(String path, int dAction, boolean isColor) {
- JButton btn = new JButton();
- Icon btnIcon = new ImageIcon(path);
- btn.setIcon(btnIcon);
- btn.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- if (isColor) {
- setColour(JColorChooser.showDialog(null, "Pick a Color", Color.BLACK));
- } else {
- setDrawAction(dAction);
- System.out.println(getDrawAction());
- }
- }
- });
- return btn;
- }
- private class DrawingBoard extends JComponent {
- ArrayList<Shape> shapes = new ArrayList<>();
- ArrayList<Color> shapeColor = new ArrayList<>();
- Point startPoint, endPoint;
- public Point getStartPoint() {
- return startPoint;
- }
- public void setStartPoint(Point startPoint) {
- this.startPoint = startPoint;
- }
- public Point getEndPoint() {
- return endPoint;
- }
- public void setEndPoint(Point endPoint) {
- this.endPoint = endPoint;
- }
- public DrawingBoard() {
- this.addMouseListener(new MouseAdapter() {
- @Override
- public void mousePressed(MouseEvent e) {
- // When the mouse is pressed get x & y position
- setStartPoint(new Point(e.getX(), e.getY()));
- System.out.println("Setting start & end point: " + e.getX() + "," + e.getY());
- setEndPoint(getStartPoint());
- repaint();
- }
- @Override
- public void mouseReleased(MouseEvent e) {
- // Create a shape using the starting x & y
- // and finishing x & y positions
- Shape theShape = null;
- if (getDrawAction() == 1) {
- // Line
- System.out.println("(x1,y1)->(" + getStartPoint().x + "," + getStartPoint().y + ")");
- System.out.println("(x2,y2)->(" + e.getX() + "," + e.getY() + ")");
- theShape = drawLine(getStartPoint().x, getStartPoint().y, e.getX(), e.getY());
- } else if (getDrawAction() == 2) {
- // Circle
- // theShape = drawCircle(getStartPoint().x, getEndPoint().y, e.getX(), e.getY());
- } else if (getDrawAction() == 3) {
- // Ellipse
- // theShape = drawEllipse(getStartPoint().x, getEndPoint().y, e.getX(), e.getY());
- } else if (getDrawAction() == 4) {
- // Rectangle
- theShape = drawRectangle(getStartPoint().x, getStartPoint().y, e.getX(), e.getY());
- } else if (getDrawAction() == 5) {
- // Square
- // theShape = drawSquare(getStartPoint().x, getEndPoint().y, e.getX(), e.getY());
- } else if (getDrawAction() == 6) {
- // Triangle
- // theShape = drawTriangle(getStartPoint().x, getEndPoint().y, e.getX(), e.getY());
- }
- // Add shapes, fills and colors to there ArrayLists
- shapes.add(theShape);
- System.out.println("Adding Shape");
- System.out.println(theShape);
- shapeColor.add(getColour());
- setStartPoint(null);
- setEndPoint(null);
- repaint();
- }
- });
- this.addMouseMotionListener(new MouseMotionAdapter() {
- @Override
- public void mouseDragged(MouseEvent e) {
- // Get the final x & y position after the mouse is dragged
- setEndPoint(new Point(e.getX(), e.getY()));
- System.out.println("Setting end point: " + e.getX() + "," + e.getY());
- repaint();
- }
- });
- }
- private Line2D.Float drawLine(int x1, int y1, int x2, int y2) {
- return new Line2D.Float(x1, y1, x2, y2);
- }
- private Ellipse2D.Float drawEllipse(
- int x1, int y1, int x2, int y2) {
- int x = Math.min(x1, x2);
- int y = Math.min(y1, y2);
- int width = Math.abs(x1 - x2);
- int height = Math.abs(y1 - y2);
- return new Ellipse2D.Float(
- x, y, width, height);
- }
- private Ellipse2D.Float drawCircle(
- int x1, int y1, int x2, int y2) {
- int x = Math.min(x1, x2);
- int y = Math.min(y1, y2);
- int width = Math.abs(x1 - x2);
- int height = width;
- return new Ellipse2D.Float(
- x, y, width, height);
- }
- private Rectangle2D.Float drawRectangle(int x1, int y1, int x2, int y2) {
- int x = Math.min(x1, x2);
- int y = Math.min(y1, y2);
- int width = Math.abs(x1 - x2);
- int height = Math.abs(y1 - y2);
- return new Rectangle2D.Float(x, y, width, height);
- }
- private Rectangle2D.Float drawSquare(int x1, int y1, int x2, int y2) {
- int x = Math.min(x1, x2);
- int y = Math.min(y1, y2);
- int width = Math.abs(x1 - x2);
- int height = width;
- return new Rectangle2D.Float(x, y, width, height);
- }
- @Override
- public void paint(Graphics graphics) {
- setGraphSettings((Graphics2D) graphics);
- getGraphSettings().setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
- getGraphSettings().setStroke(new BasicStroke(4));
- Iterator<Color> colourCounter = shapeColor.iterator();
- for (Shape shape : shapes) {
- // actually i don't know what is 0.40f :D
- getGraphSettings().setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
- getGraphSettings().draw(shape);
- getGraphSettings().setPaint(colourCounter.next());
- getGraphSettings().fill(shape);
- }
- Shape theShape = null;
- if (getStartPoint() != null && getEndPoint() != null) {
- // Color.LIGHT_GRAY
- // getGraphSettings().setPaint(Color.BLUE);
- if (getDrawAction() == 1) {
- // Line
- theShape = drawLine(getStartPoint().x, getStartPoint().y, getEndPoint().x, getEndPoint().y);
- } else if (getDrawAction() == 2) {
- // Circle
- theShape = drawCircle(getStartPoint().x, getStartPoint().y, getEndPoint().x, getEndPoint().y);
- } else if (getDrawAction() == 3) {
- // Ellipse
- theShape = drawEllipse(getStartPoint().x, getStartPoint().y, getEndPoint().x, getEndPoint().y);
- } else if (getDrawAction() == 4) {
- // Rectangle
- theShape = drawRectangle(getStartPoint().x, getStartPoint().y, getEndPoint().x, getEndPoint().y);
- } else if (getDrawAction() == 5) {
- // Square
- theShape = drawSquare(getStartPoint().x, getStartPoint().y, getEndPoint().x, getEndPoint().y);
- } else if (getDrawAction() == 6) {
- // Triangle
- // theShape = new Triangle().draw(startpoint.x,startpoint.y,e.getX(),e.getY());
- }
- }
- if (theShape != null) {
- graphSettings.draw(theShape);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement