Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.GridLayout;
- import java.awt.Point;
- import java.awt.Shape;
- 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.Ellipse2D;
- import java.awt.geom.Line2D;
- import java.awt.geom.Rectangle2D;
- import java.awt.print.PageFormat;
- import java.awt.print.Printable;
- import java.awt.print.PrinterException;
- import java.awt.print.PrinterJob;
- import java.util.ArrayList;
- import java.util.Vector;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.RepaintManager;
- public class Paint extends JFrame {
- //List of shapes
- private Vector<Shape> shapes;
- private int type;
- //Shapes color, with black as default
- private Color color = Color.black;
- private ArrayList<Color> colorList = new ArrayList<Color>();
- public Board board;
- public ShapeSelect ss;
- public ColorSelect colorSelect;
- //Shape select class' variables
- private String[] labels = { "Line", "Sircle", "Square", "Clear", "Print" };
- private JButton[] btns = new JButton[labels.length];
- //Variables used in the color selection
- private Color[] colors = { Color.black, Color.white, Color.gray, Color.blue,
- Color.lightGray, Color.green, Color.pink, Color.red,
- Color.magenta, Color.orange, Color.cyan, Color.yellow};
- private JButton[] btnColor = new JButton[colors.length];
- public Paint() {
- super("Paint");
- type = 0;
- board = new Board();
- add(board, BorderLayout.CENTER);
- ss = new ShapeSelect();
- add(ss, BorderLayout.SOUTH);
- colorSelect = new ColorSelect();
- add(colorSelect, BorderLayout.WEST);
- setSize(700,700);
- setVisible(true);
- setResizable(false);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- setLocationRelativeTo(null);
- setAlwaysOnTop(true);
- }
- private class Board extends JPanel implements MouseListener, MouseMotionListener,
- Printable {
- //Mouse points
- private Point startPoint, endPoint;
- //Sets the number value of shapes
- private final int CIRCLE = 1;
- private final int LINE = 2;
- private final int RECTANGLE = 3;
- //shape coordinates
- int upperLeftX;
- int upperLeftY;
- int width;
- int height;
- public Board() {
- //Setup of the frame
- setBackground(Color.white);
- shapes = new Vector<Shape>();
- //sets the mouse points to null
- startPoint = null;
- endPoint = null;
- addMouseListener(this);
- addMouseMotionListener(this);
- }
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- Graphics2D g2 = (Graphics2D) g;
- for (int i = 0; i < shapes.size(); i++) {
- Shape s = (Shape) shapes.get(i);
- if (s != null)
- g2.setColor(colorList.get(i));
- g2.fill(s);
- g2.draw(s);
- }
- }
- public void mousePressed(MouseEvent e) {
- startPoint = e.getPoint();
- }
- public void mouseReleased(MouseEvent e) {
- endPoint = e.getPoint();
- //creates an empty shape object to fill with spesified shape.
- Shape s = null;
- switch(type) {
- case (CIRCLE):
- //s = new Ellipse2D.float(height, width, upperLeftX, upperLeftY);
- //Up left
- if(endPoint.x < startPoint.x && endPoint.y < startPoint.y) {
- height = startPoint.y - endPoint.y;
- width = startPoint.x - endPoint.x;
- upperLeftX = endPoint.x;
- upperLeftY = endPoint.y;
- }
- //Up right
- if(endPoint.x > startPoint.x && endPoint.y < startPoint.y) {
- height = startPoint.y - endPoint.y;
- width = endPoint.x - startPoint.x;
- upperLeftX = endPoint.x - width;
- upperLeftY = startPoint.y - height;
- }
- //Down left
- if(endPoint.x < startPoint.x && endPoint.y > startPoint.y){
- height = endPoint.y - startPoint.y;
- width = startPoint.x - endPoint.x;
- upperLeftX = startPoint.x - width;
- upperLeftY = endPoint.y - height;
- }
- //Down right
- if(endPoint.x > startPoint.x && endPoint.y > startPoint.y){
- height = endPoint.y - startPoint.y;
- width = endPoint.x - startPoint.x;
- upperLeftX = startPoint.x;
- upperLeftY = startPoint.y;
- }
- s = new Ellipse2D.Double(upperLeftX, upperLeftY, width, height);
- break;
- case LINE:
- s = new Line2D.Double(startPoint.x, startPoint.y, endPoint.x, endPoint.y);
- break;
- case (RECTANGLE):
- //s = new Ellipse2D.float(height, width, upperLeftX, upperLeftY);
- //Up left
- if(endPoint.x < startPoint.x && endPoint.y < startPoint.y) {
- height = startPoint.y - endPoint.y;
- width = startPoint.x - endPoint.x;
- upperLeftX = endPoint.x;
- upperLeftY = endPoint.y;
- }
- //Up right
- if(endPoint.x > startPoint.x && endPoint.y < startPoint.y) {
- height = startPoint.y - endPoint.y;
- width = endPoint.x - startPoint.x;
- upperLeftX = endPoint.x - width;
- upperLeftY = startPoint.y - height;
- }
- //Down left
- if(endPoint.x < startPoint.x && endPoint.y > startPoint.y){
- height = endPoint.y - startPoint.y;
- width = startPoint.x - endPoint.x;
- upperLeftX = startPoint.x - width;
- upperLeftY = endPoint.y - height;
- }
- //Down right
- if(endPoint.x > startPoint.x && endPoint.y > startPoint.y){
- height = endPoint.y - startPoint.y;
- width = endPoint.x - startPoint.x;
- upperLeftX = startPoint.x;
- upperLeftY = startPoint.y;
- }
- s = new Rectangle2D.Double(upperLeftX, upperLeftY, width, height);
- break;
- }
- shapes.add(s);
- startPoint = null;
- endPoint = null;
- colorList.add(color);
- repaint();
- }
- public void mouseDragged(MouseEvent e) {
- //Creates the shapes when mouse is draged
- Graphics2D g = (Graphics2D) getGraphics();
- g.setXORMode(Color.white);
- switch (type) {
- case CIRCLE :
- //While you drag
- if(endPoint != null) {
- //g.drawOval(upperLeft, BottomRight, width, height);
- //Top
- if(endPoint.y < startPoint.y){
- upperLeftY = endPoint.y;
- height = startPoint.y - endPoint.y;
- width = endPoint.x - startPoint.x;
- g.drawOval(startPoint.x, upperLeftY, width, height);
- }
- if(endPoint.y < startPoint.y && endPoint.x < startPoint.x) {
- width = startPoint.x - endPoint.x;
- height = startPoint.y - endPoint.y;
- upperLeftX = startPoint.x - width;
- upperLeftY = startPoint.y - height;
- g.drawOval(upperLeftX, upperLeftY, width, height);
- }
- //Bottom
- if(endPoint.x < startPoint.x) {
- upperLeftX = endPoint.x;
- width = startPoint.x - endPoint.x;
- g.drawOval(upperLeftX, startPoint.y, width, endPoint.y - startPoint.y);
- }
- if(endPoint.x > startPoint.x && endPoint.y > startPoint.y) {
- g.drawOval(startPoint.x, startPoint.y, endPoint.x
- - startPoint.x, endPoint.y - startPoint.y);
- }
- }
- //First time you drag
- endPoint = e.getPoint();
- //g.drawOval(upperLeft, BottomRight, width, height);
- //Top
- if(endPoint.y < startPoint.y){
- upperLeftY = endPoint.y;
- height = startPoint.y - endPoint.y;
- width = endPoint.x - startPoint.x;
- g.drawOval(startPoint.x, upperLeftY, width, height);
- }
- if(endPoint.y < startPoint.y && endPoint.x < startPoint.x) {
- width = startPoint.x - endPoint.x;
- height = startPoint.y - endPoint.y;
- upperLeftX = startPoint.x - width;
- upperLeftY = startPoint.y - height;
- g.drawOval(upperLeftX, upperLeftY, width, height);
- }
- //Bottom
- if(endPoint.x < startPoint.x) {
- upperLeftX = endPoint.x;
- width = startPoint.x - endPoint.x;
- g.drawOval(upperLeftX, startPoint.y, width, endPoint.y - startPoint.y);
- }
- if(endPoint.x > startPoint.x && endPoint.y > startPoint.y) {
- g.drawOval(startPoint.x, startPoint.y, endPoint.x
- - startPoint.x, endPoint.y - startPoint.y);
- }
- break;
- case LINE :
- //While you drag
- if(endPoint != null) {
- g.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y);
- }
- //First time you drag
- endPoint = e.getPoint();
- g.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y);
- break;
- case RECTANGLE:
- //While you drag
- /*
- x - the x coordinate of the rectangle to be drawn.
- y - the y coordinate of the rectangle to be drawn.
- width - the width of the rectangle to be drawn.
- height - the height of the rectangle to be drawn.
- */
- if(endPoint != null) {
- //g.drawOval(upperLeft, BottomRight, width, height);
- //Top
- if(endPoint.y < startPoint.y){
- upperLeftY = endPoint.y;
- height = startPoint.y - endPoint.y;
- width = endPoint.x - startPoint.x;
- g.drawRect(startPoint.x, upperLeftY, width, height);
- }
- if(endPoint.y < startPoint.y && endPoint.x < startPoint.x) {
- width = startPoint.x - endPoint.x;
- height = startPoint.y - endPoint.y;
- upperLeftX = startPoint.x - width;
- upperLeftY = startPoint.y - height;
- g.drawRect(upperLeftX, upperLeftY, width, height);
- }
- //Bottom
- if(endPoint.x < startPoint.x) {
- upperLeftX = endPoint.x;
- width = startPoint.x - endPoint.x;
- g.drawRect(upperLeftX, startPoint.y, width, endPoint.y - startPoint.y);
- }
- if(endPoint.x > startPoint.x && endPoint.y > startPoint.y) {
- g.drawRect(startPoint.x, startPoint.y, endPoint.x
- - startPoint.x, endPoint.y - startPoint.y);
- }
- }
- //First time you drag
- endPoint = e.getPoint();
- if(endPoint.y < startPoint.y){
- upperLeftY = endPoint.y;
- height = startPoint.y - endPoint.y;
- width = endPoint.x - startPoint.x;
- g.drawRect(startPoint.x, upperLeftY, width, height);
- }
- if(endPoint.y < startPoint.y && endPoint.x < startPoint.x) {
- width = startPoint.x - endPoint.x;
- height = startPoint.y - endPoint.y;
- upperLeftX = startPoint.x - width;
- upperLeftY = startPoint.y - height;
- g.drawRect(upperLeftX, upperLeftY, width, height);
- }
- //Bottom
- if(endPoint.x < startPoint.x) {
- upperLeftX = endPoint.x;
- width = startPoint.x - endPoint.x;
- g.drawRect(upperLeftX, startPoint.y, width, endPoint.y - startPoint.y);
- }
- if(endPoint.x > startPoint.x && endPoint.y > startPoint.y) {
- g.drawRect(startPoint.x, startPoint.y, endPoint.x
- - startPoint.x, endPoint.y - startPoint.y);
- }
- }
- }
- public void mouseMoved(MouseEvent e) {
- }
- public void mouseClicked(MouseEvent e) {
- }
- public void mouseEntered(MouseEvent e) {
- }
- public void mouseExited(MouseEvent e) {
- }
- @Override
- public int print(Graphics g, PageFormat pageFormat, int pageIndex)
- throws PrinterException {
- if (pageIndex > 0) {
- return NO_SUCH_PAGE;
- } else {
- int x = (int) pageFormat.getImageableX() + 1;
- int y = (int) pageFormat.getImageableY() + 1;
- g.translate(x, y);
- RepaintManager rm = RepaintManager.currentManager(this);
- rm.setDoubleBufferingEnabled(false);
- paint(g);
- rm.setDoubleBufferingEnabled(true);
- return PAGE_EXISTS;
- }
- }
- }
- private class ShapeSelect extends JPanel {
- public ShapeSelect() {
- setLayout(new GridLayout(1,6));
- JButton btn;
- EventScanner scanner = new EventScanner();
- for (int i = 0; i < labels.length; i++) {
- btn = new JButton(labels[i]);
- btn.addActionListener(scanner);
- btns[i] = btn;
- add(btn);
- }
- }
- }
- private class ColorSelect extends JPanel implements ActionListener {
- public ColorSelect() {
- setLayout(new GridLayout(colors.length / 2, 2));
- JButton btn;
- for (int i = 0; i < colors.length; i++) {
- btn = new JButton();
- if(i == 0) {
- btn.setText("X");
- }
- btn.setBackground(colors[i]);
- btn.addActionListener(this);
- btnColor[i] = btn;
- add(btn);
- }
- }
- public void actionPerformed(ActionEvent e) {
- JButton btn = (JButton) e.getSource();
- for (int i = 0; i < btnColor.length; i++) {
- if(btn.equals(btnColor[i])) {
- clearBtnColor();
- btn.setText("X");
- color = btn.getBackground();
- }
- }
- }
- }
- private void clearBtnColor() {
- for (int i = 0; i < btnColor.length; i++) {
- btnColor[i].setText("");
- }
- }
- private class EventScanner implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- String data = e.getActionCommand();
- JButton btn = (JButton) e.getSource();
- if(data.equals("Line")) {
- resetButtons();
- btn.setEnabled(false);
- type = 2;
- }
- if(data.equals("Oval")) {
- resetButtons();
- btn.setEnabled(false);
- System.out.println("oval");
- }
- if(data.equals("Sircle")) {
- resetButtons();
- btn.setEnabled(false);
- type = 1;
- }
- if(data.equals("Square")) {
- resetButtons();
- btn.setEnabled(false);
- type = 3;
- }
- if(data.equals("Clear")) {
- resetButtons();
- btn.setEnabled(false);
- color = btnColor[0].getBackground();
- clearBtnColor();
- btnColor[0].setText("X");
- type = 0;
- shapes.clear();
- board.repaint();
- }
- if(data.equals("Print")) {
- PrinterJob printJob = PrinterJob.getPrinterJob();
- printJob.setPrintable(board);
- if (printJob.printDialog()) {
- try {
- printJob.print();
- } catch (PrinterException pe) {
- System.out.println("Error printing: " + pe);
- }
- }
- }
- }
- }
- //Helper method to reset buttons
- private void resetButtons() {
- for (int i = 0; i < btns.length; i++) {
- btns[i].setEnabled(true);
- }
- }
- public static void main(String[] args) {
- new Paint();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement