summaryrefslogtreecommitdiff
path: root/java/ImageProcessing/framework/ImageCanvas.java
blob: 182bd54bf9bd80425b87ae75e3a4fec8989202d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package imaging.framework;

import java.awt.*;
import java.awt.image.*;
import gjt.Util;
import JACE.OS.*;

class ImageCanvas extends Canvas
{
  public static final double MAX_ZOOM = 4.0;
  public static final double MIN_ZOOM = 0.5;

  public int setImage (Image image)
  {
    int flags = 0;
    if (image != null)
      {
	// Load the image
	this.tracker_.addImage(image, 0);
	try { this.tracker_.waitForID(0); } catch(InterruptedException excp) {}

	// Check if the image was actually loaded. Note that we have
	// to wait for the potential image to finish loading before we
	// know if it is a valid image.
	flags = this.checkImage (image, this);
	if (ACE.BIT_ENABLED (flags, ImageObserver.ERROR))
	  return -1;

	// If we reached here, it means image was loaded successfully so cache it
	this.image_ = image;
	this.originalImage_ = this.image_;
	
	this.x_ = (this.size ().width - this.image_.getWidth (this))/2;
	this.y_ = (this.size ().height - this.image_.getHeight (this))/2;
	this.original_x_ = this.x_;
	this.original_y_ = this.y_;
	repaint ();
      }
    return 0;
  }

  public Image getImage ()
  {
    return this.image_;
  }

  public void paint (Graphics g)
  {
    this.setBackground (Color.white);
    if (this.image_ != null)
      g.drawImage(this.image_, 
		  this.x_, this.y_,
		  (int) (this.image_.getWidth (this) * this.zoom_),
		  (int) (this.image_.getHeight (this) * this.zoom_),
		  this);
  }

  public void applyFilter (ImageFilter filter)
  {
    if (this.image_ != null)
      {
	Image temp;
	if (filter == null)
	  {
	    temp = this.originalImage_;
	    this.x_ = this.original_x_;
	    this.y_ = this.original_y_;
	    this.zoom_ = 1.0;
	  }
	else
	  temp = this.ip_.processImage(this.image_, filter, this);
	
	this.tracker_.addImage(temp, 0);
	try { this.tracker_.waitForID(0); } catch(InterruptedException excp) {}
	
	this.image_ = temp;

	// Originally I needed to flush the pixel data for the image to be
	// drawn properly. When running the applet in appletviewer, the
	// image used to jump around, but running in a browser seems to be
	// ok.  
	//this.image_.flush();
	repaint ();
      }
  }

  public void zoomFactor (double zoom)
  {
    this.zoom_ *= zoom;
    if (this.zoom_ > ImageCanvas.MAX_ZOOM)
      this.zoom_ = ImageCanvas.MAX_ZOOM;
    else if (this.zoom_ < ImageCanvas.MIN_ZOOM)
      this.zoom_ = ImageCanvas.MIN_ZOOM;

    repaint ();
  }

  public boolean mouseDown (Event evt, int x, int y)
  {
    if (inBounds (x, y))
      {
	this.selected_ = true;
	this.last_x_ = x;
	this.last_y_ = y;
      }
    return true;
  }

  public boolean mouseUp (Event evt, int x, int y)
  {
    this.selected_ = false;
    return true;
  }

  public boolean mouseDrag (Event evt, int x, int y)
  {
    if (this.selected_)
      {
	this.x_ = x - (this.last_x_ - this.x_);
	this.y_ = y - (this.last_y_ - this.y_);
	this.last_x_ = x;
	this.last_y_ = y;
	repaint ();
      }
    return true;
  }

  public boolean mouseMove (Event evt, int x, int y)
  {
    if (this.image_ != null && inBounds (x, y))
      Util.getFrame (this).setCursor (Frame.HAND_CURSOR);
    else
      Util.getFrame (this).setCursor (Frame.DEFAULT_CURSOR);
    return true;	
  }

  public boolean mouseExit (Event evt, int x, int y)
  {
    Util.getFrame (this).setCursor (Frame.DEFAULT_CURSOR);
    return true;
  }

  // Check if  mouse is within the bounds of the image
  private boolean inBounds (int x, int y)
  {
    if (this.image_ == null)
      return false;
    else
      return (x >= this.x_) &&
	     (y >= this.y_) &&
             (x <= (this.x_ + this.zoom_ * this.image_.getWidth (this))) &&
             (y <= (this.y_ + this.zoom_ * this.image_.getHeight (this)));
  }

  private MediaTracker tracker_ = new MediaTracker(this);
  private Image image_, originalImage_;
  private int x_ = 0, y_ = 0;
  private int original_x_ = 0, original_y_ = 0;
  private int width_ = 0, height_ = 0;
  private ImageProcessor ip_ = new ImageProcessor ();
  private boolean selected_ = false;
  private int last_x_ = 0, last_y_ = 0;
  private double zoom_ = 1.0;

}