daily pastebin goal
33%
SHARE
TWEET

simple paint

a guest Jan 16th, 2018 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.util.ArrayList;
  4.  
  5. import javax.swing.*;
  6.  
  7.  
  8. public class SimplePaint extends JPanel implements MouseListener, MouseMotionListener {
  9.  
  10.     /**
  11.      * This main routine allows this class to be run as a program.
  12.      */
  13.     public static void main(String[] args) {
  14.         JFrame window = new JFrame("Simple Paint");
  15.         SimplePaint content = new SimplePaint();
  16.         window.setContentPane(content);
  17.         window.setSize(700,380);
  18.         window.setLocation(100,100);
  19.         window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  20.         window.setVisible(true);
  21.  
  22.     }
  23.  
  24.     /**
  25.      * Some constants to represent the color selected by the user.
  26.      */
  27.     private final static int BLACK = 0,
  28.             RED = 1,    
  29.             GREEN = 2,  
  30.             BLUE = 3,
  31.             CYAN = 4,  
  32.             MAGENTA = 5,
  33.             YELLOW = 6;
  34.  
  35.     private int currentColor = BLACK;  // The currently selected drawing color,
  36.                                        //   coded as one of the above constants.
  37.  
  38.     /* The following variables are used when the user is sketching a
  39.          curve while dragging a mouse. */
  40.  
  41.     private int prevX, prevY;     // The previous location of the mouse.
  42.     private boolean dragging;      // This is set to true while the user is drawing.
  43.  
  44.     // *** Let's make a nested class to define a new data type that will be stored in a data structure
  45.     // e.g. (not a Rectangle[] rectangles)
  46.     private class Line{
  47.             private int x1, x2, y1, y2;
  48.             private int colorCode;
  49.  
  50.             public Line(int x1, int x2, int y1, int y2, int colorCode) {
  51.                 super();
  52.                 this.x1 = x1;
  53.                 this.x2 = x2;
  54.                 this.y1 = y1;
  55.                 this.y2 = y2;
  56.                 this.colorCode = colorCode;
  57.             }
  58.     }
  59.  
  60.     private ArrayList<Line> lines = new ArrayList<Line>();
  61.     /**
  62.      * Constructor for SimplePaintPanel class sets the background color to be
  63.      * white and sets it to listen for mouse events on itself.
  64.      */
  65.     SimplePaint() {
  66.         this.setBackground(Color.WHITE);
  67.         //*** Since the SimplePaint JPanel is also a Listener, register appropriately
  68.         this.addMouseListener(this);
  69.         this.addMouseMotionListener(this);
  70.        
  71.     }
  72.  
  73.     public void paintComponent(Graphics g) {
  74.         super.paintComponent(g);  // Fill with background color (white).
  75.  
  76.         int width = getWidth();    // Width of the panel.
  77.         int height = getHeight();  // Height of the panel.
  78.  
  79.         int colorSpacing = (height - 56) / 7;
  80.  
  81.         g.setColor(Color.GRAY);
  82.         g.drawRect(0, 0, width-1, height-1);  // one rectangle for each pixel
  83.         g.drawRect(1, 1, width-3, height-3);
  84.         g.drawRect(2, 2, width-5, height-5);
  85.  
  86.         /* Draw a 56-pixel wide gray rectangle along the right edge of the panel.
  87.              The color palette and Clear button will be drawn on top of this.
  88.              (This covers some of the same area as the border I just drew. */
  89.  
  90.         g.fillRect(width - 56, 0, 56, height);
  91.  
  92.         /* Draw the "Clear button" as a 50-by-50 white rectangle in the lower right
  93.              corner of the panel, allowing for a 3-pixel border. */
  94.  
  95.         g.setColor(Color.WHITE);
  96.         g.fillRect(width-53,  height-53, 50, 50);
  97.         g.setColor(Color.BLACK);
  98.         g.drawRect(width-53, height-53, 49, 49);
  99.         g.drawString("CLEAR", width-48, height-23);
  100.  
  101.         /* Draw the seven color rectangles. */
  102.  
  103.         g.setColor(Color.BLACK);
  104.         g.fillRect(width-53, 3 + 0*colorSpacing, 50, colorSpacing-3);
  105.         g.setColor(Color.RED);
  106.         g.fillRect(width-53, 3 + 1*colorSpacing, 50, colorSpacing-3);
  107.         g.setColor(Color.GREEN);
  108.         g.fillRect(width-53, 3 + 2*colorSpacing, 50, colorSpacing-3);
  109.         g.setColor(Color.BLUE);
  110.         g.fillRect(width-53, 3 + 3*colorSpacing, 50, colorSpacing-3);
  111.         g.setColor(Color.CYAN);
  112.         g.fillRect(width-53, 3 + 4*colorSpacing, 50, colorSpacing-3);
  113.         g.setColor(Color.MAGENTA);
  114.         g.fillRect(width-53, 3 + 5*colorSpacing, 50, colorSpacing-3);
  115.         g.setColor(Color.YELLOW);
  116.         g.fillRect(width-53, 3 + 6*colorSpacing, 50, colorSpacing-3);
  117.  
  118.         /* Draw a 2-pixel white border around the color rectangle
  119.              of the current drawing color. */
  120.  
  121.         g.setColor(Color.WHITE);
  122.         g.drawRect(width-55, 1 + currentColor*colorSpacing, 53, colorSpacing);
  123.         g.drawRect(width-54, 2 + currentColor*colorSpacing, 51, colorSpacing-2);
  124.  
  125.        
  126.         // *** Re-draw all of the information, based on the state of our data structure
  127.  
  128.         for (int i = 0; i < lines.size(); i++) {
  129.                 switch(lines.get(i).colorCode) {
  130.                 case BLACK:
  131.                     g.setColor(Color.BLACK);
  132.                     break;
  133.                 case RED:
  134.                     g.setColor(Color.RED);
  135.                     break;
  136.                 case GREEN:
  137.                     g.setColor(Color.GREEN);
  138.                     break;
  139.                 case BLUE:
  140.                     g.setColor(Color.BLUE);
  141.                     break;
  142.                 case CYAN:
  143.                     g.setColor(Color.CYAN);
  144.                     break;
  145.                 case MAGENTA:
  146.                     g.setColor(Color.MAGENTA);
  147.                     break;
  148.                 case YELLOW:
  149.                     g.setColor(Color.YELLOW);
  150.                     break;
  151.                 }
  152.                 g.drawLine(lines.get(i).x1, lines.get(i).x2, lines.get(i).y1, lines.get(i).y2);
  153.         }
  154.     } // end paintComponent()
  155.  
  156.  
  157.     private void changeColor(int y) {
  158.         int width = getWidth();           // Width of panel.
  159.         int height = getHeight();         // Height of panel.
  160.         int colorSpacing = (height - 56) / 7;  // Space for one color rectangle.
  161.         int newColor = y / colorSpacing;       // Which color number was clicked?
  162.         System.out.println("y: " + y + " colorSpacing: " + colorSpacing + " newColor: " + newColor);
  163.  
  164.         if (newColor < 0 || newColor > 6)      // Make sure the color number is valid.
  165.             return;
  166.  
  167.         currentColor = newColor;
  168.     }
  169.  
  170.  
  171.     /**
  172.      * This is called when the user presses the mouse anywhere in the panel.  
  173.      * There are three possible responses, depending on where the user clicked:  
  174.      * Change the current color, clear the drawing, or start drawing a curve.  
  175.      * (Or do nothing if user clicks on the border.)
  176.      */
  177.     public void mousePressed(MouseEvent evt) {
  178.         int x = evt.getX();   // x-coordinate where the user clicked.
  179.         int y = evt.getY();   // y-coordinate where the user clicked.
  180.  
  181.         int width = getWidth();    // Width of the panel.
  182.         int height = getHeight();  // Height of the panel.
  183.  
  184.         if (dragging == true)  // Ignore mouse presses that occur
  185.             return;            //    when user is already drawing a curve.
  186.                                //    (This can happen if the user presses
  187.                                //    two mouse buttons at the same time.)
  188.                                     //***like left button is down+dragging but you click the right button
  189.         if (x > width - 53) {
  190.             if (y > height - 53) {
  191.                         //  ***Clicked on "CLEAR button".
  192.                       lines.clear();
  193.                       repaint();
  194.             }
  195.             else {
  196.                 changeColor(y);  // Clicked on the color palette.
  197.                 repaint();              // ***update the highlighted square of color
  198.             }
  199.         }
  200.         else if (x > 3 && x < width - 56 && y > 3 && y < height - 3) {
  201.                 // The user has clicked on the white drawing area.
  202.                 // Start drawing a curve from the point (x,y).
  203.             prevX = x;
  204.             prevY = y;
  205.                 dragging = true;
  206.         }
  207.  
  208.     } // end mousePressed()
  209.  
  210.  
  211.     /**
  212.      * Called whenever the user releases the mouse button. If the user was drawing
  213.      * a curve, the curve is done, so we should set dragging to false
  214.      */
  215.     public void mouseReleased(MouseEvent evt) {
  216.         if (dragging == false)
  217.                 return; // Nothing to do because the user isn't drawing.
  218.         dragging = false;
  219.     }
  220.  
  221.  
  222.     /**
  223.      * Called whenever the user moves the mouse while a mouse button is held down.  
  224.      * If the user is drawing, draw a line segment from the previous mouse location
  225.      * to the current mouse location, and set up prevX and prevY for the next call.  
  226.      * Note that in case the user drags outside of the drawing area, the values of
  227.      * x and y are "clamped" to lie within this area.  This avoids drawing on the color
  228.      * palette or clear button.
  229.      */
  230.     public void mouseDragged(MouseEvent evt) {
  231.         if (dragging == false)
  232.             return;  // Nothing to do because the user isn't drawing.
  233.  
  234.         int x = evt.getX();   // x-coordinate of mouse.
  235.         int y = evt.getY();   // y-coordinate of mouse.
  236.  
  237.         if (x < 3)                          // Adjust the value of x,
  238.             x = 3;                           //   to make sure it's in
  239.         if (x > getWidth() - 57)       //   the drawing area.
  240.             x = getWidth() - 57;
  241.  
  242.         if (y < 3)                          // Adjust the value of y,
  243.             y = 3;                           //   to make sure it's in
  244.         if (y > getHeight() - 4)       //   the drawing area.
  245.             y = getHeight() - 4;
  246.  
  247.         lines.add(new Line (prevX, prevY, x, y, currentColor));
  248.         repaint();
  249.         prevX = x;
  250.             prevY = y;// *** update our data structure to reflect the new state as the user is dragging
  251.         // Remember, NO DRAWING here!
  252.        
  253.        
  254.        
  255.  
  256.     } // end mouseDragged()
  257.  
  258.  
  259.     public void mouseEntered(MouseEvent evt) { }   // Some empty routines.
  260.     public void mouseExited(MouseEvent evt) { }    //    (Required by the MouseListener
  261.     public void mouseClicked(MouseEvent evt) { }   //    and MouseMotionListener
  262.     public void mouseMoved(MouseEvent evt) { }     //    interfaces).
  263.  
  264.  
  265.  
  266. } // end class SimplePaint
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top