Advertisement
Guest User

Paint example

a guest
May 11th, 2013
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.73 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.Color;
  3. import java.awt.Graphics;
  4. import java.awt.Graphics2D;
  5. import java.awt.GridLayout;
  6. import java.awt.Point;
  7. import java.awt.Shape;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import java.awt.event.MouseEvent;
  11. import java.awt.event.MouseListener;
  12. import java.awt.event.MouseMotionListener;
  13. import java.awt.geom.Ellipse2D;
  14. import java.awt.geom.Line2D;
  15. import java.awt.geom.Rectangle2D;
  16. import java.awt.print.PageFormat;
  17. import java.awt.print.Printable;
  18. import java.awt.print.PrinterException;
  19. import java.awt.print.PrinterJob;
  20. import java.util.ArrayList;
  21. import java.util.Vector;
  22.  
  23. import javax.swing.JButton;
  24. import javax.swing.JFrame;
  25. import javax.swing.JPanel;
  26. import javax.swing.RepaintManager;
  27.  
  28. public class Paint extends JFrame {
  29.     //List of shapes
  30.     private Vector<Shape> shapes;
  31.    
  32.     private int type;
  33.     //Shapes color, with black as default
  34.     private Color color = Color.black;
  35.     private ArrayList<Color> colorList = new ArrayList<Color>();
  36.    
  37.     public Board board;
  38.     public ShapeSelect ss;
  39.     public ColorSelect colorSelect;
  40.    
  41.     //Shape select class' variables
  42.     private String[] labels = { "Line", "Sircle", "Square", "Clear", "Print" };
  43.     private JButton[] btns = new JButton[labels.length];
  44.    
  45.    
  46.     //Variables used in the color selection
  47.     private Color[] colors = { Color.black, Color.white, Color.gray, Color.blue,
  48.             Color.lightGray, Color.green, Color.pink, Color.red,
  49.             Color.magenta, Color.orange, Color.cyan, Color.yellow};
  50.     private JButton[] btnColor = new JButton[colors.length];
  51.  
  52.     public Paint() {
  53.         super("Paint");
  54.        
  55.         type = 0;
  56.        
  57.         board = new Board();
  58.         add(board, BorderLayout.CENTER);
  59.        
  60.         ss = new ShapeSelect();
  61.         add(ss, BorderLayout.SOUTH);
  62.        
  63.         colorSelect = new ColorSelect();
  64.         add(colorSelect, BorderLayout.WEST);
  65.        
  66.         setSize(700,700);
  67.         setVisible(true);
  68.         setResizable(false);
  69.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  70.         setLocationRelativeTo(null);
  71.         setAlwaysOnTop(true);
  72.     }
  73.    
  74.     private class Board extends JPanel implements MouseListener, MouseMotionListener,
  75.                                                         Printable {
  76.        
  77.         //Mouse points
  78.         private Point startPoint, endPoint;
  79.        
  80.         //Sets the number value of shapes
  81.         private final int CIRCLE = 1;
  82.         private final int LINE = 2;
  83.         private final int RECTANGLE = 3;
  84.        
  85.         //shape coordinates
  86.         int upperLeftX;
  87.         int upperLeftY;
  88.         int width;
  89.         int height;
  90.        
  91.         public Board() {
  92.             //Setup of the frame
  93.             setBackground(Color.white);
  94.            
  95.             shapes = new Vector<Shape>();
  96.            
  97.             //sets the mouse points to null
  98.             startPoint = null;
  99.             endPoint = null;
  100.             addMouseListener(this);
  101.             addMouseMotionListener(this);
  102.            
  103.         }
  104.        
  105.         public void paintComponent(Graphics g) {
  106.             super.paintComponent(g);
  107.            
  108.             Graphics2D g2 = (Graphics2D) g;
  109.             for (int i = 0; i < shapes.size(); i++) {
  110.                 Shape s = (Shape) shapes.get(i);
  111.                 if (s != null)
  112.                     g2.setColor(colorList.get(i));
  113.                     g2.fill(s);
  114.                     g2.draw(s);
  115.             }
  116.         }
  117.  
  118.         public void mousePressed(MouseEvent e) {
  119.             startPoint = e.getPoint();
  120.            
  121.         }
  122.  
  123.         public void mouseReleased(MouseEvent e) {
  124.             endPoint = e.getPoint();
  125.            
  126.             //creates an empty shape object to fill with spesified shape.
  127.             Shape s = null;
  128.             switch(type) {
  129.             case (CIRCLE):
  130.                 //s = new Ellipse2D.float(height, width, upperLeftX, upperLeftY);
  131.                 //Up left
  132.                 if(endPoint.x < startPoint.x && endPoint.y < startPoint.y) {
  133.                     height = startPoint.y - endPoint.y;
  134.                     width = startPoint.x - endPoint.x;
  135.                     upperLeftX = endPoint.x;
  136.                     upperLeftY = endPoint.y;
  137.                 }
  138.                 //Up right
  139.                 if(endPoint.x > startPoint.x && endPoint.y < startPoint.y) {
  140.                     height = startPoint.y - endPoint.y;
  141.                     width = endPoint.x - startPoint.x;
  142.                     upperLeftX = endPoint.x - width;
  143.                     upperLeftY = startPoint.y - height;
  144.                 }  
  145.                 //Down left
  146.                 if(endPoint.x < startPoint.x && endPoint.y > startPoint.y){
  147.                     height = endPoint.y - startPoint.y;
  148.                     width = startPoint.x - endPoint.x;
  149.                     upperLeftX = startPoint.x - width;
  150.                     upperLeftY = endPoint.y - height;
  151.                    
  152.                 }
  153.                 //Down right
  154.                 if(endPoint.x > startPoint.x && endPoint.y > startPoint.y){
  155.                     height = endPoint.y - startPoint.y;
  156.                     width = endPoint.x - startPoint.x;
  157.                     upperLeftX = startPoint.x;
  158.                     upperLeftY = startPoint.y;
  159.                 }
  160.            
  161.                 s = new Ellipse2D.Double(upperLeftX, upperLeftY, width, height);
  162.                 break;
  163.             case LINE:
  164.                 s = new Line2D.Double(startPoint.x, startPoint.y, endPoint.x, endPoint.y);
  165.                 break;
  166.             case (RECTANGLE):
  167.                 //s = new Ellipse2D.float(height, width, upperLeftX, upperLeftY);
  168.                 //Up left
  169.                 if(endPoint.x < startPoint.x && endPoint.y < startPoint.y) {
  170.                     height = startPoint.y - endPoint.y;
  171.                     width = startPoint.x - endPoint.x;
  172.                     upperLeftX = endPoint.x;
  173.                     upperLeftY = endPoint.y;
  174.                 }
  175.                 //Up right
  176.                 if(endPoint.x > startPoint.x && endPoint.y < startPoint.y) {
  177.                     height = startPoint.y - endPoint.y;
  178.                     width = endPoint.x - startPoint.x;
  179.                     upperLeftX = endPoint.x - width;
  180.                     upperLeftY = startPoint.y - height;
  181.                 }  
  182.                 //Down left
  183.                 if(endPoint.x < startPoint.x && endPoint.y > startPoint.y){
  184.                     height = endPoint.y - startPoint.y;
  185.                     width = startPoint.x - endPoint.x;
  186.                     upperLeftX = startPoint.x - width;
  187.                     upperLeftY = endPoint.y - height;
  188.                    
  189.                 }
  190.                 //Down right
  191.                 if(endPoint.x > startPoint.x && endPoint.y > startPoint.y){
  192.                     height = endPoint.y - startPoint.y;
  193.                     width = endPoint.x - startPoint.x;
  194.                     upperLeftX = startPoint.x;
  195.                     upperLeftY = startPoint.y;
  196.                 }
  197.            
  198.                 s = new Rectangle2D.Double(upperLeftX, upperLeftY, width, height);
  199.                 break;
  200.             }
  201.             shapes.add(s);
  202.             startPoint = null;
  203.             endPoint = null;
  204.             colorList.add(color);
  205.             repaint();
  206.         }
  207.  
  208.         public void mouseDragged(MouseEvent e) {
  209.             //Creates the shapes when mouse is draged
  210.             Graphics2D g = (Graphics2D) getGraphics();
  211.             g.setXORMode(Color.white);
  212.            
  213.             switch (type) {
  214.             case CIRCLE :
  215.                 //While you drag
  216.                 if(endPoint != null) {
  217.                     //g.drawOval(upperLeft, BottomRight, width, height);
  218.                     //Top
  219.                     if(endPoint.y < startPoint.y){
  220.                         upperLeftY = endPoint.y;
  221.                         height = startPoint.y - endPoint.y;
  222.                         width = endPoint.x - startPoint.x;
  223.                         g.drawOval(startPoint.x, upperLeftY, width, height);
  224.                     }
  225.                     if(endPoint.y < startPoint.y && endPoint.x < startPoint.x) {
  226.                         width = startPoint.x - endPoint.x;
  227.                         height = startPoint.y - endPoint.y;
  228.                         upperLeftX = startPoint.x - width;
  229.                         upperLeftY = startPoint.y - height;
  230.                         g.drawOval(upperLeftX, upperLeftY, width, height);
  231.                     }
  232.                     //Bottom
  233.                     if(endPoint.x < startPoint.x) {
  234.                         upperLeftX = endPoint.x;
  235.                         width = startPoint.x - endPoint.x;
  236.                         g.drawOval(upperLeftX, startPoint.y, width, endPoint.y - startPoint.y);
  237.                     }
  238.                     if(endPoint.x > startPoint.x && endPoint.y > startPoint.y) {
  239.                         g.drawOval(startPoint.x, startPoint.y, endPoint.x
  240.                                 - startPoint.x, endPoint.y - startPoint.y);
  241.                     }
  242.                 }
  243.                
  244.                 //First time you drag
  245.                 endPoint = e.getPoint();
  246.                 //g.drawOval(upperLeft, BottomRight, width, height);
  247.                 //Top
  248.                 if(endPoint.y < startPoint.y){
  249.                     upperLeftY = endPoint.y;
  250.                     height = startPoint.y - endPoint.y;
  251.                     width = endPoint.x - startPoint.x;
  252.                     g.drawOval(startPoint.x, upperLeftY, width, height);
  253.                 }
  254.                 if(endPoint.y < startPoint.y && endPoint.x < startPoint.x) {
  255.                     width = startPoint.x - endPoint.x;
  256.                     height = startPoint.y - endPoint.y;
  257.                     upperLeftX = startPoint.x - width;
  258.                     upperLeftY = startPoint.y - height;
  259.                     g.drawOval(upperLeftX, upperLeftY, width, height);
  260.                 }
  261.                 //Bottom
  262.                 if(endPoint.x < startPoint.x) {
  263.                     upperLeftX = endPoint.x;
  264.                     width = startPoint.x - endPoint.x;
  265.                     g.drawOval(upperLeftX, startPoint.y, width, endPoint.y - startPoint.y);
  266.                 }
  267.                 if(endPoint.x > startPoint.x && endPoint.y > startPoint.y) {
  268.                     g.drawOval(startPoint.x, startPoint.y, endPoint.x
  269.                             - startPoint.x, endPoint.y - startPoint.y);
  270.                 }
  271.                
  272.                 break;
  273.             case LINE :
  274.                 //While you drag
  275.                 if(endPoint != null) {
  276.                     g.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y);
  277.                 }
  278.                
  279.                 //First time you drag
  280.                 endPoint = e.getPoint();
  281.                 g.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y);
  282.                 break;
  283.             case RECTANGLE:
  284.                 //While you drag
  285.                 /*
  286.                     x - the x coordinate of the rectangle to be drawn.
  287.                     y - the y coordinate of the rectangle to be drawn.
  288.                     width - the width of the rectangle to be drawn.
  289.                     height - the height of the rectangle to be drawn.
  290.  
  291.                  */
  292.                 if(endPoint != null) {
  293.                     //g.drawOval(upperLeft, BottomRight, width, height);
  294.                     //Top
  295.                     if(endPoint.y < startPoint.y){
  296.                         upperLeftY = endPoint.y;
  297.                         height = startPoint.y - endPoint.y;
  298.                         width = endPoint.x - startPoint.x;
  299.                         g.drawRect(startPoint.x, upperLeftY, width, height);
  300.                     }
  301.                     if(endPoint.y < startPoint.y && endPoint.x < startPoint.x) {
  302.                         width = startPoint.x - endPoint.x;
  303.                         height = startPoint.y - endPoint.y;
  304.                         upperLeftX = startPoint.x - width;
  305.                         upperLeftY = startPoint.y - height;
  306.                         g.drawRect(upperLeftX, upperLeftY, width, height);
  307.                     }
  308.                     //Bottom
  309.                     if(endPoint.x < startPoint.x) {
  310.                         upperLeftX = endPoint.x;
  311.                         width = startPoint.x - endPoint.x;
  312.                         g.drawRect(upperLeftX, startPoint.y, width, endPoint.y - startPoint.y);
  313.                     }
  314.                     if(endPoint.x > startPoint.x && endPoint.y > startPoint.y) {
  315.                         g.drawRect(startPoint.x, startPoint.y, endPoint.x
  316.                                 - startPoint.x, endPoint.y - startPoint.y);
  317.                     }
  318.                 }
  319.                 //First time you drag
  320.                 endPoint = e.getPoint();
  321.                 if(endPoint.y < startPoint.y){
  322.                     upperLeftY = endPoint.y;
  323.                     height = startPoint.y - endPoint.y;
  324.                     width = endPoint.x - startPoint.x;
  325.                     g.drawRect(startPoint.x, upperLeftY, width, height);
  326.                 }
  327.                 if(endPoint.y < startPoint.y && endPoint.x < startPoint.x) {
  328.                     width = startPoint.x - endPoint.x;
  329.                     height = startPoint.y - endPoint.y;
  330.                     upperLeftX = startPoint.x - width;
  331.                     upperLeftY = startPoint.y - height;
  332.                     g.drawRect(upperLeftX, upperLeftY, width, height);
  333.                 }
  334.                 //Bottom
  335.                 if(endPoint.x < startPoint.x) {
  336.                     upperLeftX = endPoint.x;
  337.                     width = startPoint.x - endPoint.x;
  338.                     g.drawRect(upperLeftX, startPoint.y, width, endPoint.y - startPoint.y);
  339.                 }
  340.                 if(endPoint.x > startPoint.x && endPoint.y > startPoint.y) {
  341.                     g.drawRect(startPoint.x, startPoint.y, endPoint.x
  342.                             - startPoint.x, endPoint.y - startPoint.y);
  343.                 }
  344.             }
  345.         }
  346.  
  347.         public void mouseMoved(MouseEvent e) {
  348.         }
  349.        
  350.         public void mouseClicked(MouseEvent e) {
  351.         }
  352.  
  353.         public void mouseEntered(MouseEvent e) {
  354.         }
  355.  
  356.         public void mouseExited(MouseEvent e) {
  357.         }
  358.  
  359.         @Override
  360.         public int print(Graphics g, PageFormat pageFormat, int pageIndex)
  361.                 throws PrinterException {
  362.             if (pageIndex > 0) {
  363.                 return NO_SUCH_PAGE;
  364.             } else {
  365.  
  366.                 int x = (int) pageFormat.getImageableX() + 1;
  367.                 int y = (int) pageFormat.getImageableY() + 1;
  368.                 g.translate(x, y);
  369.                 RepaintManager rm = RepaintManager.currentManager(this);
  370.                 rm.setDoubleBufferingEnabled(false);
  371.                 paint(g);
  372.                 rm.setDoubleBufferingEnabled(true);
  373.                 return PAGE_EXISTS;
  374.             }
  375.         }
  376.  
  377.  
  378.     }
  379.    
  380.     private class ShapeSelect extends JPanel {
  381.        
  382.         public ShapeSelect() {
  383.             setLayout(new GridLayout(1,6));
  384.             JButton btn;
  385.             EventScanner scanner = new EventScanner();
  386.            
  387.             for (int i = 0; i < labels.length; i++) {
  388.                 btn = new JButton(labels[i]);
  389.                 btn.addActionListener(scanner);
  390.                 btns[i] = btn;
  391.                 add(btn);
  392.             }
  393.         }
  394.     }
  395.    
  396.     private class ColorSelect extends JPanel implements ActionListener {
  397.        
  398.         public ColorSelect() {
  399.             setLayout(new GridLayout(colors.length / 2, 2));
  400.            
  401.             JButton btn;
  402.             for (int i = 0; i < colors.length; i++) {
  403.                 btn = new JButton();
  404.                 if(i == 0) {
  405.                     btn.setText("X");
  406.                 }
  407.                 btn.setBackground(colors[i]);
  408.                 btn.addActionListener(this);
  409.                 btnColor[i] = btn;
  410.                 add(btn);
  411.             }
  412.         }
  413.  
  414.         public void actionPerformed(ActionEvent e) {
  415.             JButton btn = (JButton) e.getSource();
  416.            
  417.             for (int i = 0; i < btnColor.length; i++) {
  418.                 if(btn.equals(btnColor[i])) {
  419.                     clearBtnColor();
  420.                     btn.setText("X");
  421.                     color = btn.getBackground();
  422.                 }
  423.             }          
  424.         }
  425.     }
  426.    
  427.     private void clearBtnColor() {
  428.         for (int i = 0; i < btnColor.length; i++) {
  429.             btnColor[i].setText("");
  430.         }
  431.     }
  432.    
  433.     private class EventScanner implements ActionListener {
  434.        
  435.         public void actionPerformed(ActionEvent e) {
  436.             String data = e.getActionCommand();
  437.             JButton btn = (JButton) e.getSource();
  438.            
  439.             if(data.equals("Line")) {
  440.                 resetButtons();
  441.                 btn.setEnabled(false);
  442.                 type = 2;
  443.             }
  444.             if(data.equals("Oval")) {
  445.                 resetButtons();
  446.                 btn.setEnabled(false);
  447.                 System.out.println("oval");
  448.             }
  449.             if(data.equals("Sircle")) {
  450.                 resetButtons();
  451.                 btn.setEnabled(false);
  452.                 type = 1;
  453.             }
  454.             if(data.equals("Square")) {
  455.                 resetButtons();
  456.                 btn.setEnabled(false);
  457.                 type = 3;
  458.             }
  459.             if(data.equals("Clear")) {
  460.                 resetButtons();
  461.                 btn.setEnabled(false);
  462.                 color = btnColor[0].getBackground();
  463.                 clearBtnColor();
  464.                 btnColor[0].setText("X");
  465.                
  466.                 type = 0;
  467.                 shapes.clear();
  468.                 board.repaint();
  469.             }
  470.             if(data.equals("Print")) {
  471.                 PrinterJob printJob = PrinterJob.getPrinterJob();
  472.                 printJob.setPrintable(board);
  473.                 if (printJob.printDialog()) {
  474.                     try {
  475.                         printJob.print();
  476.                     } catch (PrinterException pe) {
  477.                         System.out.println("Error printing: " + pe);
  478.                     }
  479.                 }
  480.             }
  481.         }
  482.        
  483.     }
  484.    
  485.     //Helper method to reset buttons
  486.     private void resetButtons() {
  487.         for (int i = 0; i < btns.length; i++) {
  488.             btns[i].setEnabled(true);
  489.         }
  490.     }
  491.    
  492.     public static void main(String[] args) {
  493.         new Paint();
  494.     }
  495. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement