/* * ======================================================================= * DemoTexturePaint.java : This applet demonstrates how texture patterns * are created from "*.jpg" files. The applet window displays a sequence * of textures as thumbnails. Clicking on a thumbnail will fill the canvas * at the center of the window with the corresponding texture. * * Note. Pantham's implementation of this program is as a standalone * Java program. The applet version is a bit more complicated because the * texture patterns need to be downloaded over the net. * * Adapted from : Pantham S., Pure JFC Swing, 1999. * Modified by : Mark Austin March, 2001 * ======================================================================= */ import javax.swing.*; import javax.swing.border.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.awt.image.*; import java.util.*; import java.net.URL; public class DemoTexturePaint extends JApplet { DemoTexturePaintCanvas canvas; URL codeBase; // URL for applet codebase public void init() { // 1. Get the content pane and assign layout Container container = getContentPane(); // 2. Add the canvas with rectangles canvas = new DemoTexturePaintCanvas(); container.add(canvas); // 3. Create a panel to display various textures (canvases) JPanel panel = new JPanel(); panel.setLayout(new GridLayout(1,10)); panel.setBorder(new TitledBorder( "Select a Texture by Clicking the Mouse...")); // 4. Get codebase for applet .... codeBase = getCodeBase(); // 5. Create canvas objects displaying thumb nails of textures, // and add them to the panel. for (int i=0 ; i < 10; i++) { panel.add( new DemoTextureSelectableCanvas( codeBase, "textures/texture"+(i+1)+".jpg", canvas ) ); } // 6. Add the panel to the content pane .... container.add(BorderLayout.SOUTH, panel); } } // 7. Canvas to display the selected texture class DemoTexturePaintCanvas extends Canvas { Image displayImage; // Constructor DemoTexturePaintCanvas() { // Add the mouse listener to receive a rectangle selection setBackground(Color.white); // For canvas background color } public void setImage(Image image) { displayImage = image; } public void paint(Graphics g) { // Create the graphics context object Graphics2D g2D = (Graphics2D) g; // Create a texture paint object and display it in a rectangle // by filling it. if (displayImage != null) { BufferedImage bi = new BufferedImage( displayImage.getWidth(this), displayImage.getHeight(this), BufferedImage.TYPE_INT_RGB); bi.createGraphics().drawImage(displayImage, 0, 0, this); Rectangle2D rectangle = new Rectangle2D.Float(0, 0, displayImage.getWidth(this), displayImage.getHeight(this)); TexturePaint tp = new TexturePaint(bi, rectangle); g2D.setPaint(tp); g2D.fill(new Rectangle2D.Float(0, 0, getWidth(), getHeight())); } } } // 8. Class to define thumb nail objects of textures. The // user can select a texture by clicking the mouse on the // corresponding thumb nail. class DemoTextureSelectableCanvas extends Canvas { Image image; DemoTexturePaintCanvas canvas; URL textureURL; // URL for texture image ... DemoTextureSelectableCanvas( URL codeBase, String imageFile, DemoTexturePaintCanvas canvas ) { // Construct URL for texture .... try { textureURL = new URL( codeBase, imageFile ); } catch ( java.net.MalformedURLException e ) { System.out.println("Badly specified URL!!"); } // Download and save image icon .... this.image = new ImageIcon( textureURL ).getImage(); this.canvas = canvas; addMouseListener(new MouseEventHandler()); setSize( 35, 35); setBackground(Color.white); } public void paint(Graphics g) { g.drawImage( image, 0, 0, this); } // 9. Class to handle mouse events that are generated // when the mouse is clicked on the canvas. class MouseEventHandler extends MouseAdapter { public void mouseClicked(MouseEvent evt) { canvas.setImage( image); canvas.repaint(); } } }