Simple image handling and drawing interaction : Image « Advanced Graphics « Java

Simple image handling and drawing interaction

Simple image handling and drawing interaction
import java.awt.*;

import javax.swing.*;


 * G demo program. Demonstrates:
 * <ul>
 * <li>Simple image handling
 * <li>Simple drawing interaction
 * </ul>
 * @author <a href="">Jacob Dreyer</a>
public class Demo10 extends JFrame
  implements GInteraction
  private GScene    scene_;
  private GSegment  route_;
  private int[]     xy_;
  public Demo10 (String imageFileName)
    super ("G Graphics Library - Demo 10");    
    setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    getContentPane().setLayout (new BorderLayout());
    getContentPane().add (new JLabel ("Draw line on map using mouse button 1"),
    // Create the graphic canvas
    GWindow window = new GWindow();
    getContentPane().add (window.getCanvas(), BorderLayout.CENTER);
    // Create scane with default viewport and world extent settings
    scene_ = new GScene (window);

    // Create a graphic object
    GObject object = new TestObject (imageFileName);
    scene_.add (object);

    setSize (new Dimension (500, 500));
    setVisible (true);

    window.startInteraction (this);

  public void event (GScene scene, int event, int x, int y)
    switch (event) {
      case GWindow.BUTTON1_DOWN :
        xy_ = new int[] {x, y};
        route_.setGeometry (xy_);

      case GWindow.BUTTON1_DRAG :
        int[] a = new int[xy_.length + 2];
        System.arraycopy (xy_, 0, a, 0, xy_.length);
        a[a.length-2] = x;
        a[a.length-1] = y;
        xy_ = a;
        route_.setGeometry (xy_);
   * Defines the geometry and presentation for a sample
   * graphic object.
  private class TestObject extends GObject
    private GSegment segment_;
    TestObject (String imageFileName)
      segment_ = new GSegment();
      addSegment (segment_);

      GImage image = new GImage (new File (imageFileName));
      image.setPositionHint (GPosition.SOUTHEAST);
      segment_.setImage (image);

      route_ = new GSegment();
      addSegment (route_);

      GStyle routeStyle = new GStyle();
      routeStyle.setForegroundColor (new Color (255, 0, 0));
      routeStyle.setLineWidth (4);
      routeStyle.setAntialiased (true);
      route_.setStyle (routeStyle);

    public void draw()
      segment_.setGeometry (0, 0);
      route_.setGeometry (xy_);

  public static void main (String[] args)
    //if (args.length != 1) {
      //System.out.println ("Provide an image file name");
//      System.exit (0);
  //  }
    //new Demo10 (args[0]);
    new Demo10 ("logo.png");

  225 k)

Related examples in the same category

1.Create an image that does not support transparency
2.Create an image that supports transparent pixels
3.Create an image that supports arbitrary levels of transparency
4.Creating a buffered image using Component.createImage().
5.Creating a Buffered Image from an Image
6.Drawing on a Buffered Image
7.If the buffered image supports transparency
8.Converting a Buffered Image (BufferedImage) from an Image
9.Getting and Setting Pixels in a Buffered Image
10.Scaling a Buffered Image
11.Shearing a Buffered Image
12.Translating a Buffered Image
13.Rotating a Buffered Image
14.Flipping a Buffered Image
15.Flip the image horizontally
16.Flip the image vertically and horizontally, equivalent to rotating the image 180 degrees
17.2D Image Draw
18.Transform image
19.Creating a Image Zoomer using Graphics2D
20.Gaussian Blur DemoGaussian Blur Demo
21.Intermediate ImagesIntermediate Images
22.Image reflectionImage reflection
23.Bloom DemoBloom Demo
24.Box Blur DemoBox Blur Demo
25.Brightness Increase DemoBrightness Increase Demo
26.Blur an ImageBlur an Image
27.Blur our image: Blur means an unfocused image
28.A reflected image: effect makes an illusion as if the image was reflected in water
29.Enlarge Image With AnimationEnlarge Image With Animation
30.Image ZoomingImage Zooming
31.Unsharp Mask DemoUnsharp Mask Demo
32.Create a grayscale image with Java 2D tools
33.A 3x3 kernel that embosses an image.
34.A 3x3 kernel that blurs an image.
35.A 3x3 kernel that sharpens an image.
36.Embossing a Buffered Image
37.Brighten the image by 30%
38.Darken the image by 10%
39.Extend RGBImageFilter to create ColorFilter class
40.Extend RGBImageFilter to create AlphaFilter class
41.Enlarging an image by pixel replication
42.Shrinking an image by skipping pixels