Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*; import java.awt.image.BufferedImage;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Point;
- import java.awt.event.*;
- import javax.swing.*;
- @SuppressWarnings("serial")
- public class SimpleComponent extends JFrame {
- public BufferedImage image = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB);
- JPanel canvas = new JPanel();
- JPanel buttonPanel = new JPanel();
- Point rect=null;
- Point lastPos = null;
- Point startPos = null;
- Point finishPos = null;
- Graphics graph;
- JButton drawButton = new JButton("Free");
- private Graphics gr;
- public SimpleComponent () {
- setLocation(0,0);
- setSize(300,600);
- setTitle("Photoshop");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- canvas.setBackground(Color.WHITE);
- //add buttons here
- buttonPanel.add(drawButton);
- //set the look
- getContentPane().add(canvas, BorderLayout.CENTER);
- getContentPane().add(buttonPanel, BorderLayout.NORTH);
- setVisible(true);
- graph = canvas.getGraphics();
- canvas.addMouseMotionListener(new MouseMotionListener () {
- public void mouseDragged (MouseEvent m) {
- Point p = m.getPoint() ;
- graph.drawLine(lastPos.x, lastPos.y, p.x, p.y);
- lastPos = p ;
- }
- public void mouseMoved (MouseEvent m) {}
- });
- canvas.addMouseListener(new MouseListener () {
- public void mouseClicked(MouseEvent e) {startPos = e.getPoint();}
- public void mousePressed(MouseEvent e) {lastPos = e.getPoint();}
- public void mouseReleased(MouseEvent e) {
- lastPos = null;
- finishPos = e.getPoint();
- if (finishPos.x<startPos.x&&finishPos.y<startPos.y) {
- graph.drawLine(startPos.x, startPos.y, finishPos.x, startPos.y);
- }
- startPos = null;}
- public void mouseEntered(MouseEvent e) {}
- public void mouseExited(MouseEvent e) {}
- });
- }
- public void captureCanvasImage (){
- Graphics g = image.createGraphics();
- canvas.paint(g);
- }
- public static void main (String [] args) {
- SimpleComponent p = new SimpleComponent();
- p.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment