The SimpleDraw application : Mouse Draw « 2D Graphics « Java Tutorial

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Reflection
8. Regular Expressions
9. Collections
10. Thread
11. File
12. Generics
13. I18N
14. Swing
15. Swing Event
16. 2D Graphics
17. SWT
18. SWT 2D Graphics
19. Network
20. Database
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web Services SOA
27. EJB3
28. Spring
29. PDF
30. Email
31. J2ME
32. J2EE Application
33. XML
34. Design Pattern
35. Log
36. Security
37. Apache Common
38. Ant
39. JUnit
Java
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Tutorial » 2D Graphics » Mouse Draw 
16. 43. 2. The SimpleDraw application
The SimpleDraw application
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class SimpleDraw extends JFrame
        implements ActionListener, MouseListener {
    // (x1,y1) = coordinate of mouse pressed
    // (x2,y2) = coordinate of mouse released
    int x1;
    int y1;
    int x2;

    int y2;

    ArrayList<Shape> shapes = new ArrayList<Shape>();

    String shapeType = "Rectangle";

    public SimpleDraw() {
        this.setTitle("Simple DRAW");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // add check box group
        ButtonGroup cbg = new ButtonGroup();
        JRadioButton lineButton = new JRadioButton("Line");
        JRadioButton ovalButton = new JRadioButton("Oval");
        JRadioButton rectangleButton =
                new JRadioButton("Rectangle");
        cbg.add(lineButton);
        cbg.add(ovalButton);
        cbg.add(rectangleButton);
        lineButton.addActionListener(this);
        ovalButton.addActionListener(this);
        rectangleButton.addActionListener(this);
        rectangleButton.setSelected(true);
        JPanel radioPanel = new JPanel(new FlowLayout());
        radioPanel.add(lineButton);
        radioPanel.add(ovalButton);
        radioPanel.add(rectangleButton);
        this.addMouseListener(this);
        this.setLayout(new BorderLayout());
        this.add(radioPanel, BorderLayout.NORTH);
    }

    public void paint(Graphics g) {
        paintComponents(g);
        for (Shape shape : shapes) {
          Graphics2D g2 = (Graphics2D)g;  
          g2.draw(shape);
        }
    }

    public void actionPerformed(ActionEvent ae) {
        shapeType = ae.getActionCommand().toString();
    }

    public void mouseClicked(MouseEvent me) {
    }

    public void mouseEntered(MouseEvent me) {

    }

    public void mouseExited(MouseEvent me) {
    }

    public void mousePressed(MouseEvent me) {
        x1 = me.getX();
        y1 = me.getY();
    }

    public void mouseReleased(MouseEvent me) {
        x2 = me.getX();
        y2 = me.getY();
        Shape shape = null;
        if (shapeType.equals("Rectangle")) {
            // a Rectangle cannot have a zero width or height
            if (x1 != x2 || y1 != y2) {
                shape = new Rectangle(x1, y1, x2, y2);
            }
        
        if (shape != null) {
            this.shapes.add(shape);
            this.repaint();
        }
    }

    public static void main(String[] args) {
        JFrame.setDefaultLookAndFeelDecorated(true);
        SimpleDraw frame = new SimpleDraw();
        frame.pack();
        frame.setVisible(true);
    }
}
16. 43. Mouse Draw
16. 43. 1. Mouse drag and drawMouse drag and draw
16. 43. 2. The SimpleDraw applicationThe SimpleDraw application
16. 43. 3. Draws a small dot where the user clicks the mouseDraws a small dot where the user clicks the mouse
16. 43. 4. Save Your Drawing To a File
16. 43. 5. Select the Ellipse to Move It in the CanvasSelect the Ellipse to Move It in the Canvas
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.