Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Lab9;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.awt.event.MouseMotionListener;
- import java.util.ArrayList;
- import javax.swing.JPanel;
- public class PaintPanel extends JPanel implements MouseListener, MouseMotionListener{
- private static final long serialVersionUID = 1L;
- public int x0,y0,x1,y1; //?0-mouse click position ?1- mouse release position
- private int mx, my; //mouse position at any given time
- private Color c= Color.BLACK;
- Outline outline;
- private int mode=0; //1-pencil mode 2-shape mode
- private int panelHeight=100;
- private int cwidth; //tool panel cell width
- ArrayList<Circle> shapes = new ArrayList<Circle>();
- ArrayList<PencilDot> pdots= new ArrayList<PencilDot>();
- public PaintPanel(){
- setPreferredSize(new Dimension(1200,1200));
- setBackground(Color.white);
- addMouseListener(this);
- addMouseMotionListener(this);
- }
- public void paintComponent(Graphics g){
- super.paintComponent(g);
- cwidth = this.getWidth()/10;
- //draw the empty tool panel
- g.setColor(Color.black);
- g.drawRect(0, 0, this.getWidth()-1, panelHeight+1);
- g.setColor(Color.lightGray);
- g.fillRect(1, 1, this.getWidth()-1, panelHeight);
- /*
- * Draw Color Picker
- */
- g.setColor(Color.BLACK);
- g.fillRect(1,1,cwidth,panelHeight);
- g.setColor(Color.RED);
- g.fillRect((cwidth*1),1,cwidth,panelHeight);
- g.setColor(Color.GREEN);
- g.fillRect((cwidth*2),1,cwidth,panelHeight);
- g.setColor(Color.BLUE);
- g.fillRect((cwidth*3),1,cwidth,panelHeight);
- /*
- * Draw Tools picker
- */
- //Pencil
- g.setColor(Color.BLACK);
- g.drawRect((cwidth*4),0,cwidth,panelHeight+1);
- g.drawString("PENCIL", (cwidth*4), 15);
- //Circle
- g.drawRect(cwidth*5,0, cwidth,panelHeight+1);
- if(cwidth*6-cwidth*5>panelHeight)
- g.fillOval(cwidth*5, 1, panelHeight,panelHeight+1);
- else if(cwidth*6-cwidth*5<panelHeight)
- g.fillOval(cwidth*5, 1, cwidth,cwidth);
- g.drawRect(cwidth*9, 0, cwidth, panelHeight+1);
- g.drawString("CLEAR", (cwidth*9), 15);
- //draw canvas
- for(Circle shape:shapes){
- shape.draw(g);
- }
- for(PencilDot pdot:pdots){
- pdot.draw(g);
- }
- //draw outline if it is not null
- if(outline!=null)
- outline.draw(g);
- //mouse position and selected color for debugging
- g.setColor(Color.BLACK);
- g.drawString("X: "+mx+"Y: "+my+" Color: "+c,mx,my);
- }
- @Override
- public void mouseClicked(MouseEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mouseEntered(MouseEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mouseExited(MouseEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mousePressed(MouseEvent e) {
- /*
- * Selecting color
- */
- if(e.getX()>0 && e.getX()<(cwidth) && e.getY()>1 && e.getY()<panelHeight){
- c=Color.BLACK;
- System.out.println("BLACK");
- }
- if(e.getX()>(cwidth) && x1< (cwidth*2) && e.getY()>1 && e.getY()<panelHeight){
- c=Color.RED;
- System.out.println("RED");
- }
- if(e.getX()>(cwidth*2) && x1< (cwidth*3) && e.getY()>1 && e.getY()< panelHeight){
- c=Color.GREEN;
- System.out.println("GREEN");
- }
- if(e.getX()>(cwidth*3) && x1< (cwidth*4) && e.getY()>1 && e.getY()<panelHeight){
- c=Color.BLUE;
- System.out.println("BLUE");
- }
- /*
- * Selecting mode
- */
- if(e.getX()>(cwidth*4)&& x1<(cwidth*5)&& e.getY()>1&&e.getY()<panelHeight){
- mode=0;
- }
- if(e.getX()>(cwidth*5)&& e.getX()<(cwidth*6)&& e.getY()>1&&e.getY()<panelHeight){
- mode=1;
- }
- /*
- * Clear button
- */
- if(e.getX() > cwidth*9 && e.getX() < cwidth*10 && e.getY() > 1 && e.getY() < panelHeight){
- shapes.clear();
- pdots.clear();
- }
- /*
- * if mode set to shapes> get starting position and start the outline of the shape
- */
- if(mode==1){
- x0=e.getX();
- if(e.getY()<100)
- y0=100;
- else
- y0=e.getY();
- outline = new Outline();
- outline.setX(x0);
- outline.setY(y0);
- }
- repaint();
- }
- @Override
- public void mouseReleased(MouseEvent e) {
- //get mouse position on release
- x1=e.getX();
- y1=e.getY();
- //If shapes mode set draw shape and set outline to null
- if(mode==1){
- if((x1-x0>0))
- shapes.add(new Circle(x1-x0, c,x0,y0));
- else
- shapes.add(new Circle(x0-x1,c,x0,y0));
- outline=null;
- repaint();
- }
- }
- @Override
- public void mouseDragged(MouseEvent e) {
- //grab mouse position when dragging
- mx=e.getX();
- my=e.getY();
- //Limit mouse release position within current canvas area
- if(e.getX()>this.getWidth()){
- x1=this.getWidth()-pdots.get(0).getWidth();
- }else if(e.getX()<0){
- x1=0;
- }else{
- x1=e.getX();
- }
- if(e.getY()<panelHeight+1){
- y1=panelHeight;
- }else if(e.getY()>this.getHeight()){
- y1=this.getHeight()-pdots.get(0).getWidth();
- }else{
- y1=e.getY();
- }
- //If pencil mode> add a dot to current mouse position
- if(mode==0){
- pdots.add(new PencilDot(x1,y1,c));
- }else if (mode==1){ //if shapes mode make outline to current mouse position
- outline.setWidth(x1-x0);
- outline.setHeight(y1-y0);
- }
- repaint();
- }
- @Override
- public void mouseMoved(MouseEvent e) {
- //grab mouse position when mouse if moved
- mx=e.getX();
- my=e.getY();
- repaint();
- }
- public void resetValues(){
- //Reset position variables.
- //not used but made it just in case
- x0=0;
- x1=0;
- y0=0;
- y1=0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement