ArthurDn

Untitled

Oct 9th, 2012
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.54 KB | None | 0 0
  1.  
  2.         import java.awt.*;  import java.awt.image.BufferedImage;
  3.     import java.awt.Color;
  4.     import java.awt.Graphics;
  5.  
  6.     import java.awt.Point;
  7.     import java.awt.event.*;
  8.     import javax.swing.*;
  9.  
  10.      
  11.      
  12.     @SuppressWarnings("serial")
  13.     public class SimpleComponent extends JFrame {
  14.      
  15.         public BufferedImage image = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB);
  16.         JPanel canvas = new JPanel();
  17.         JPanel buttonPanel = new JPanel();
  18. Point rect=null;
  19.         Point lastPos = null;
  20.         Point startPos = null;
  21.         Point finishPos = null;
  22.         Graphics graph;
  23.      
  24.         JButton drawButton = new JButton("Free");
  25.         private Graphics gr;
  26.      
  27.      
  28.          
  29.         public SimpleComponent () {
  30.      
  31.             setLocation(0,0);
  32.             setSize(300,600);
  33.             setTitle("Photoshop");
  34.             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  35.             canvas.setBackground(Color.WHITE);
  36.              
  37.             //add buttons here
  38.             buttonPanel.add(drawButton);
  39.                      
  40.             //set the look
  41.             getContentPane().add(canvas, BorderLayout.CENTER);
  42.             getContentPane().add(buttonPanel, BorderLayout.NORTH);
  43.          
  44.             setVisible(true);
  45.          
  46.             graph = canvas.getGraphics();
  47.          
  48.  
  49.      
  50.         canvas.addMouseMotionListener(new MouseMotionListener () {                      
  51.                 public void mouseDragged (MouseEvent m) {
  52.                     Point p = m.getPoint() ;
  53.                  
  54.                      graph.drawLine(lastPos.x, lastPos.y, p.x, p.y);
  55.                     lastPos = p ;
  56.  
  57.                 }  
  58.                 public void mouseMoved (MouseEvent m) {}
  59.             });
  60.              
  61.                  
  62.          
  63.     canvas.addMouseListener(new MouseListener () {
  64.             public void mouseClicked(MouseEvent e) {startPos = e.getPoint();}
  65.             public void mousePressed(MouseEvent e) {lastPos = e.getPoint();}
  66.         public void mouseReleased(MouseEvent e) {
  67.         lastPos = null;
  68.  
  69.                 finishPos = e.getPoint();
  70.                   if (finishPos.x<startPos.x&&finishPos.y<startPos.y) {
  71.                         graph.drawLine(startPos.x, startPos.y, finishPos.x, startPos.y);
  72.                     }
  73.     startPos = null;}
  74.             public void mouseEntered(MouseEvent e) {}
  75.         public void mouseExited(MouseEvent e) {}
  76.             });
  77.              
  78.         }
  79.          
  80.      
  81.         public void captureCanvasImage (){
  82.             Graphics g = image.createGraphics();
  83.             canvas.paint(g);
  84.         }
  85.    
  86.        
  87.      
  88.          
  89.          
  90.         public static void main (String [] args) {
  91.             SimpleComponent p = new SimpleComponent();
  92.             p.setVisible(true);
  93.         }
  94.     }
Advertisement
Add Comment
Please, Sign In to add comment