Paint Test : Paint « 2D Graphics GUI « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
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 » 2D Graphics GUI » PaintScreenshots 
Paint Test
Paint Test
   
/**
 @version 1.00 1999-09-11
 @author Cay Horstmann
 */

import java.awt.Color;
import java.awt.Container;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Paint;
import java.awt.TexturePaint;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

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

public class PaintTest {
  public static void main(String[] args) {
    JFrame frame = new PaintTestFrame();
    frame.show();
  }
}

class PaintTestFrame extends JFrame implements ActionListener {
  public PaintTestFrame() {
    setTitle("PaintTest");
    setSize(400400);
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });

    Container contentPane = getContentPane();
    canvas = new PaintPanel();
    contentPane.add(canvas, "Center");

    JPanel buttonPanel = new JPanel();
    ButtonGroup group = new ButtonGroup();

    colorButton = new JRadioButton("Color"true);
    buttonPanel.add(colorButton);
    group.add(colorButton);
    colorButton.addActionListener(this);

    gradientPaintButton = new JRadioButton("Gradient Paint"false);
    buttonPanel.add(gradientPaintButton);
    group.add(gradientPaintButton);
    gradientPaintButton.addActionListener(this);

    texturePaintButton = new JRadioButton("Texture Paint"false);
    buttonPanel.add(texturePaintButton);
    group.add(texturePaintButton);
    texturePaintButton.addActionListener(this);

    contentPane.add(buttonPanel, "North");
  }

  public void actionPerformed(ActionEvent event) {
    Object source = event.getSource();
    if (source == colorButton)
      canvas.setColor();
    else if (source == gradientPaintButton)
      canvas.setGradientPaint();
    else if (source == texturePaintButton)
      canvas.setTexturePaint();
  }

  private PaintPanel canvas;

  private JRadioButton colorButton;

  private JRadioButton gradientPaintButton;

  private JRadioButton texturePaintButton;
}

class PaintPanel extends JPanel {
  public PaintPanel() {
    Image image = Toolkit.getDefaultToolkit().getImage("java2s.gif");
    MediaTracker tracker = new MediaTracker(this);
    tracker.addImage(image, 0);
    try {
      tracker.waitForID(0);
    catch (InterruptedException e) {
    }
    bufferedImage = new BufferedImage(image.getWidth(null), image
        .getHeight(null), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = bufferedImage.createGraphics();
    g2.drawImage(image, 00null);

    setColor();
  }

  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2Dg;
    g2.setPaint(paint);
    Ellipse2D circle = new Ellipse2D.Double(00, getWidth(), getHeight());
    g2.fill(circle);
  }

  public void setColor() {
    paint = Color.red; // Color implements Paint
    repaint();
  }

  public void setGradientPaint() {
    paint = new GradientPaint(00, Color.red, (floatgetWidth(),
        (floatgetHeight(), Color.blue);
    repaint();
  }

  public void setTexturePaint() {
    Rectangle2D anchor = new Rectangle2D.Double(00* bufferedImage
        .getWidth()* bufferedImage.getHeight());
    paint = new TexturePaint(bufferedImage, anchor);
    repaint();
  }

  private Paint paint;

  private BufferedImage bufferedImage;
}

           
         
    
    
  
Related examples in the same category
1. Draw stringDraw string
2. Draw canvas with color and textDraw canvas with color and text
3. Anti AliasAnti Alias
4. PaintsPaints
5. Program to draw gridsProgram to draw grids
6. Render Quality Test Render Quality Test
7. PlotPlot
8. Picture Scaler
9. Radial GradientRadial Gradient
10. Rotation About Center
11. Safe Repaint
12. Scale Test
13. Scaling Methods
14. Simple Attributes of painting
15. Two Stops GradientTwo Stops Gradient
16. Bad vs. Good Primitive Rendering
17. Static methods for some common painting functions
18. Set Text Anti Aliasing
19. Graphics Util: draw shapes and text
20. Utilties for painting visual effects
21. Your own Graphics2D
22. Reads a Paint object that has been serialised
23. A panel that displays a paint sample.
w___w___w__.___j__a_v_a__2___s_.__c___o_m___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.