Advertisement
Guest User

PaintPanel

a guest
Nov 4th, 2016
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.56 KB | None | 0 0
  1. package Lab9;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Dimension;
  5. import java.awt.Graphics;
  6. import java.awt.event.MouseEvent;
  7. import java.awt.event.MouseListener;
  8. import java.awt.event.MouseMotionListener;
  9. import java.util.ArrayList;
  10.  
  11. import javax.swing.JPanel;
  12.  
  13. public class PaintPanel extends JPanel implements MouseListener, MouseMotionListener{
  14.     private static final long serialVersionUID = 1L;
  15.    
  16.     public int x0,y0,x1,y1; //?0-mouse click position ?1- mouse release position
  17.     private int mx, my; //mouse position at any given time
  18.     private Color c= Color.BLACK;
  19.     Outline outline;
  20.     private int mode=0; //1-pencil mode  2-shape mode
  21.     private int panelHeight=100;
  22.     private int cwidth; //tool panel cell width
  23.  
  24.     ArrayList<Circle> shapes = new ArrayList<Circle>();
  25.     ArrayList<PencilDot> pdots= new ArrayList<PencilDot>();
  26.    
  27.     public PaintPanel(){
  28.        
  29.         setPreferredSize(new Dimension(1200,1200));
  30.         setBackground(Color.white);
  31.        
  32.         addMouseListener(this);
  33.         addMouseMotionListener(this);
  34.        
  35.     }
  36.    
  37.     public void paintComponent(Graphics g){
  38.         super.paintComponent(g);
  39.         cwidth = this.getWidth()/10;
  40.        
  41.         //draw the empty tool panel
  42.         g.setColor(Color.black);
  43.         g.drawRect(0, 0, this.getWidth()-1, panelHeight+1);
  44.         g.setColor(Color.lightGray);
  45.         g.fillRect(1, 1, this.getWidth()-1, panelHeight);
  46.        
  47.         /*
  48.          * Draw Color Picker
  49.          */
  50.         g.setColor(Color.BLACK);
  51.         g.fillRect(1,1,cwidth,panelHeight);
  52.        
  53.         g.setColor(Color.RED);
  54.         g.fillRect((cwidth*1),1,cwidth,panelHeight);
  55.        
  56.         g.setColor(Color.GREEN);
  57.         g.fillRect((cwidth*2),1,cwidth,panelHeight);
  58.                
  59.         g.setColor(Color.BLUE);
  60.         g.fillRect((cwidth*3),1,cwidth,panelHeight);
  61.        
  62.         /*
  63.          * Draw Tools picker
  64.          */
  65.         //Pencil
  66.         g.setColor(Color.BLACK);
  67.         g.drawRect((cwidth*4),0,cwidth,panelHeight+1);
  68.         g.drawString("PENCIL", (cwidth*4), 15);
  69.        
  70.         //Circle
  71.         g.drawRect(cwidth*5,0, cwidth,panelHeight+1);
  72.         if(cwidth*6-cwidth*5>panelHeight)
  73.             g.fillOval(cwidth*5, 1, panelHeight,panelHeight+1);
  74.         else if(cwidth*6-cwidth*5<panelHeight)
  75.             g.fillOval(cwidth*5, 1, cwidth,cwidth);
  76.        
  77.         g.drawRect(cwidth*9, 0, cwidth, panelHeight+1);
  78.         g.drawString("CLEAR", (cwidth*9), 15);
  79.        
  80.        
  81.         //draw canvas
  82.         for(Circle shape:shapes){
  83.             shape.draw(g);
  84.         }
  85.         for(PencilDot pdot:pdots){
  86.             pdot.draw(g);
  87.         }
  88.         //draw outline if it is not null
  89.         if(outline!=null)
  90.             outline.draw(g);
  91.        
  92.        
  93.         //mouse position and selected color for debugging
  94.         g.setColor(Color.BLACK);
  95.         g.drawString("X: "+mx+"Y: "+my+" Color: "+c,mx,my);
  96.     }
  97.  
  98.     @Override
  99.     public void mouseClicked(MouseEvent e) {
  100.         // TODO Auto-generated method stub
  101.        
  102.     }
  103.  
  104.     @Override
  105.     public void mouseEntered(MouseEvent e) {
  106.         // TODO Auto-generated method stub
  107.        
  108.     }
  109.  
  110.     @Override
  111.     public void mouseExited(MouseEvent e) {
  112.         // TODO Auto-generated method stub
  113.        
  114.     }
  115.  
  116.     @Override
  117.     public void mousePressed(MouseEvent e) {
  118.         /*
  119.          * Selecting color
  120.          */
  121.         if(e.getX()>0 && e.getX()<(cwidth) && e.getY()>1 && e.getY()<panelHeight){
  122.             c=Color.BLACK;
  123.             System.out.println("BLACK");
  124.  
  125.         }
  126.         if(e.getX()>(cwidth) && x1< (cwidth*2) && e.getY()>1 && e.getY()<panelHeight){
  127.             c=Color.RED;
  128.             System.out.println("RED");
  129.         }
  130.         if(e.getX()>(cwidth*2) && x1< (cwidth*3) && e.getY()>1 && e.getY()< panelHeight){
  131.             c=Color.GREEN;
  132.             System.out.println("GREEN");
  133.  
  134.         }
  135.         if(e.getX()>(cwidth*3) && x1< (cwidth*4) && e.getY()>1 && e.getY()<panelHeight){
  136.             c=Color.BLUE;
  137.             System.out.println("BLUE");
  138.  
  139.         }
  140.         /*
  141.          * Selecting mode
  142.          */
  143.         if(e.getX()>(cwidth*4)&& x1<(cwidth*5)&& e.getY()>1&&e.getY()<panelHeight){
  144.             mode=0;
  145.         }
  146.         if(e.getX()>(cwidth*5)&& e.getX()<(cwidth*6)&& e.getY()>1&&e.getY()<panelHeight){
  147.             mode=1;
  148.         }
  149.         /*
  150.          * Clear button
  151.          */
  152.         if(e.getX() > cwidth*9 && e.getX() < cwidth*10 && e.getY() > 1 && e.getY() < panelHeight){
  153.             shapes.clear();
  154.             pdots.clear();
  155.         }
  156.        
  157.        
  158.         /*
  159.          * if mode set to shapes> get starting position and start the outline of the shape
  160.          */
  161.         if(mode==1){
  162.             x0=e.getX();
  163.             if(e.getY()<100)
  164.                 y0=100;
  165.             else
  166.                 y0=e.getY();
  167.            
  168.             outline = new Outline();
  169.             outline.setX(x0);
  170.             outline.setY(y0);
  171.         }
  172.        
  173.         repaint();
  174.     }
  175.        
  176.  
  177.     @Override
  178.    
  179.     public void mouseReleased(MouseEvent e) {
  180.         //get mouse position on release
  181.         x1=e.getX();
  182.         y1=e.getY();
  183.        
  184.         //If shapes mode set draw shape and set outline to null
  185.         if(mode==1){
  186.             if((x1-x0>0))
  187.                 shapes.add(new Circle(x1-x0, c,x0,y0));
  188.             else
  189.                 shapes.add(new Circle(x0-x1,c,x0,y0));
  190.             outline=null;
  191.            
  192.             repaint();
  193.             }
  194.     }
  195.  
  196.     @Override
  197.     public void mouseDragged(MouseEvent e) {
  198.         //grab mouse position when dragging
  199.         mx=e.getX();
  200.         my=e.getY();
  201.        
  202.        
  203.        
  204.         //Limit mouse release position within current canvas area
  205.         if(e.getX()>this.getWidth()){
  206.             x1=this.getWidth()-pdots.get(0).getWidth();
  207.         }else if(e.getX()<0){
  208.             x1=0;
  209.         }else{
  210.             x1=e.getX();
  211.         }
  212.         if(e.getY()<panelHeight+1){
  213.             y1=panelHeight;
  214.         }else if(e.getY()>this.getHeight()){
  215.             y1=this.getHeight()-pdots.get(0).getWidth();
  216.         }else{
  217.             y1=e.getY();
  218.         }
  219.        
  220.         //If pencil mode> add a dot to current mouse position
  221.         if(mode==0){
  222.             pdots.add(new PencilDot(x1,y1,c));
  223.         }else if (mode==1){ //if shapes mode make outline to current mouse position
  224.         outline.setWidth(x1-x0);
  225.         outline.setHeight(y1-y0);
  226.        
  227.         }
  228.         repaint();             
  229.     }
  230.  
  231.     @Override
  232.     public void mouseMoved(MouseEvent e) {
  233.         //grab mouse position when mouse if moved
  234.         mx=e.getX();
  235.         my=e.getY();
  236.         repaint();
  237.     }
  238.    
  239.    
  240.     public void resetValues(){
  241.         //Reset position variables.
  242.         //not used but made it just in case
  243.         x0=0;
  244.         x1=0;
  245.         y0=0;
  246.         y1=0;
  247.     }
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement