home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-12-14 | 5.7 KB | 282 lines |
- import java.awt.*;
- import java.awt.event.*;
- import java.applet.Applet;
- import java.awt.image.*;
-
- /*
- * class for handling one image
- */
- class Picture {
-
- /*
- * position of the image
- */
- int xpos, ypos;
-
- /*
- * width and height of the image
- */
- int width, height;
-
- /*
- * the image itself
- */
- Image image;
- ImageObserver obs;
-
- /**
- * constructor saves arguments
- * @param img - the image
- * @param x, y - initial position
- * @param o - imageObserver of parent
- */
- public Picture (Image img, int x, int y, ImageObserver o) {
-
- image = img;
- xpos = x;
- ypos = y;
- obs = o;
- width = image.getWidth (obs);
- height = image.getHeight (obs);
- }
-
- /**
- * determine whether the point is inside this image
- * @param x, y - coordinate of point
- */
- boolean inside (int x, int y) {
-
- if (x < xpos || x > (xpos+width)) return false;
- if (y < ypos || y > (ypos+height)) return false;
- return true;
- }
-
- /**
- * set the current position of the image
- * @param x, y - position to set
- */
- void setPosition (int x, int y) {
-
- xpos = x;
- ypos = y;
- }
-
- /**
- * draw the image
- * draw a green border around the image if
- * highlight is true
- * @param g - destination graphics object
- * @param highlight - draw border
- */
- void paint (Graphics g, boolean highlight) {
-
- if (highlight) {
- g.setColor (Color.green);
- g.fillRect (xpos-5, ypos-5, width+10, height+10);
- }
- g.drawImage (image, xpos, ypos, obs);
- }
- }
-
- /*
- * the applet
- */
- public class LayerApp extends Applet implements MouseListener,
- MouseMotionListener, ActionListener
- {
-
- /*
- * the number of picture objects
- */
- final int NPictures = 4;
-
- /*
- * an array containing the picture objects
- */
- Picture pictures[] = new Picture[NPictures];
-
- /*
- * the user-selected picture
- */
- int selectedPic = -1;
-
- /*
- * offsets from mouse to image origin
- */
- int dx, dy;
-
- /*
- * offscreen image for double-buffering
- */
- Image offimg;
- Button btnBring;
- Button btnSend;
-
- /*
- * offscreen graphics context associated with
- * offscreen image
- */
- Graphics offg;
-
- /*
- * dimension of offscreen image
- */
- Dimension offsize;
-
- /*
- * called when the applet is loaded
- */
- public void init() {
-
- setLayout(new BorderLayout());
-
- Panel p = new Panel ();
- add ("South", p);
- btnBring = new Button("Bring to front");
- btnSend = new Button("Send to back");
- p.add(btnBring);
- p.add(btnSend);
- addMouseListener(this);
- addMouseMotionListener(this);
- btnBring.addActionListener(this);
- btnSend.addActionListener(this);
-
- int i;
- Image img;
- String name;
- MediaTracker tracker = new MediaTracker (this);
-
- offsize = getSize();
- offimg = createImage (offsize.width, offsize.height);
- offg = offimg.getGraphics();
-
- for (i=0; i<NPictures; i+=1) {
- if (i < 2) name = "T"+(i+1)+".jpg";
- else name = "T"+(i+1)+".gif";
- img = getImage (getDocumentBase(), name);
- tracker.addImage (img, i);
- showStatus ("Getting image: "+name);
- try {
- tracker.waitForID (i);
- } catch (InterruptedException e) { }
- pictures[i] = new Picture (img, i*10, i*20, this);
- }
- }
-
- /**
- * reverse the order of update for efficiency
- * @param g - destination graphics object
- */
- public void paint (Graphics g) {
-
- update (g);
- }
-
- /**
- * override update to avoid erase flicker
- * @param g - destination graphics object
- */
- public void update (Graphics g) {
-
- int i;
-
- offg.setColor (Color.black);
- offg.fillRect (0, 0, offsize.width, offsize.height);
- for (i=0; i<NPictures; i+=1) {
- if (i == selectedPic) pictures[i].paint (offg, true);
- else pictures[i].paint (offg, false);
- }
- g.drawImage(offimg, 0, 0, this);
- }
-
- /**
- * determine which image the user clicked
- * @param evt - event object
- * @param x, y - mouse position
- */
- public void mouseClicked(MouseEvent e){}
- public void mouseEntered(MouseEvent e){}
- public void mouseExited(MouseEvent e){}
-
- public void mouseReleased(MouseEvent e)
- {
- repaint ();
- }
-
- public void mouseMoved(MouseEvent e){}
-
- public void mousePressed(MouseEvent e)
- {
- int x =e.getX();
- int y =e.getY();
-
- int i;
-
- selectedPic = -1;
- for (i=NPictures-1; i>=0; i-=1)
- {
- if (pictures[i].inside (x, y))
- {
- selectedPic = i;
- dx = x - pictures[i].xpos;
- dy = y - pictures[i].ypos;
- break;
- }
- }
- }
-
- public void mouseDragged(MouseEvent e)
- {
- int x =e.getX();
- int y =e.getY();
- int i;
-
- if (selectedPic < 0) return;
- /* for (i=NPictures-1; i>=0; i-=1)
- {
-
- pictures[selectedPic].setPosition (x - dx, y - dy);
- repaint ();
- } */
-
- }
- /**
- * reorder the images depending on which button is pressed
- * @param evt - event object
- * @param arg - target object
- */
- public void actionPerformed(ActionEvent evt)
- {
- Object object1 = evt.getSource();
- int i;
- Picture temp;
-
-
- if (object1 == btnBring) {
- if (selectedPic < 0) return;
- temp = pictures[selectedPic];
- for (i=selectedPic; i<NPictures-1; i+=1)
- {
- pictures[i] = pictures[i+1];
- }
- pictures[NPictures-1] = temp;
- selectedPic = NPictures - 1;
- repaint ();
- return;
- }
- if (object1 == btnSend)
- {
- if (selectedPic < 0) return;
- temp = pictures[selectedPic];
- for (i=selectedPic; i>0; i-=1)
- {
- pictures[i] = pictures[i-1];
- }
- pictures[0] = temp;
- selectedPic = 0;
- repaint ();
- return;
- }
- }
- }
-