home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / javafile / ch07 / LayerApp.java < prev    next >
Encoding:
Java Source  |  1998-12-14  |  5.7 KB  |  282 lines

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.applet.Applet;
  4. import java.awt.image.*;
  5.  
  6. /*
  7.  * class for handling one image
  8.  */
  9. class Picture {
  10.  
  11. /*
  12.  * position of the image
  13.  */
  14. int xpos, ypos;
  15.  
  16. /*
  17.  * width and height of the image
  18.  */
  19. int width, height;
  20.  
  21. /*
  22.  * the image itself
  23.  */
  24. Image image;
  25. ImageObserver obs;
  26.  
  27. /**
  28.  * constructor saves arguments
  29.  * @param img - the image
  30.  * @param x, y - initial position
  31.  * @param o - imageObserver of parent
  32.  */
  33. public Picture (Image img, int x, int y, ImageObserver o) {
  34.  
  35.     image = img;
  36.     xpos = x;
  37.     ypos = y;
  38.     obs = o;
  39.     width = image.getWidth (obs);
  40.     height = image.getHeight (obs);
  41. }
  42.  
  43. /**
  44.  * determine whether the point is inside this image
  45.  * @param x, y - coordinate of point
  46.  */
  47. boolean inside (int x, int y) {
  48.  
  49.     if (x < xpos || x > (xpos+width)) return false;
  50.     if (y < ypos || y > (ypos+height)) return false;
  51.     return true; 
  52. }
  53.  
  54. /**
  55.  * set the current position of the image
  56.  * @param x, y - position to set
  57.  */
  58. void setPosition (int x, int y) {
  59.  
  60.     xpos = x;
  61.     ypos = y;
  62. }
  63.  
  64. /**
  65.  * draw the image
  66.  * draw a green border around the image if
  67.  * highlight is true
  68.  * @param g - destination graphics object
  69.  * @param highlight - draw border
  70.  */
  71. void paint (Graphics g, boolean highlight) {
  72.  
  73.     if (highlight) {
  74.         g.setColor (Color.green);
  75.         g.fillRect (xpos-5, ypos-5, width+10, height+10);
  76.     }
  77.     g.drawImage (image, xpos, ypos, obs);
  78. }
  79. }
  80.  
  81. /*
  82.  * the applet
  83.  */
  84. public class LayerApp extends Applet implements MouseListener,
  85.         MouseMotionListener, ActionListener
  86. {
  87.  
  88. /*
  89.  * the number of picture objects
  90.  */
  91. final int NPictures = 4;
  92.  
  93. /*
  94.  * an array containing the picture objects
  95.  */
  96. Picture pictures[] = new Picture[NPictures];
  97.  
  98. /*
  99.  * the user-selected picture
  100.  */
  101. int selectedPic = -1;
  102.  
  103. /*
  104.  * offsets from mouse to image origin
  105.  */
  106. int dx, dy; 
  107.  
  108. /*
  109.  * offscreen image for double-buffering
  110.  */
  111. Image offimg;
  112. Button btnBring;
  113. Button btnSend;
  114.  
  115. /*
  116.  * offscreen graphics context associated with
  117.  * offscreen image
  118.  */
  119. Graphics offg;
  120.  
  121. /*
  122.  * dimension of offscreen image
  123.  */
  124. Dimension offsize;
  125.  
  126. /*
  127.  * called when the applet is loaded
  128.  */
  129. public void init() {
  130.  
  131.     setLayout(new BorderLayout());
  132.  
  133.     Panel p = new Panel ();
  134.     add ("South", p);
  135.     btnBring = new Button("Bring to front");
  136.     btnSend = new Button("Send to back");
  137.     p.add(btnBring);
  138.     p.add(btnSend);
  139.     addMouseListener(this);
  140.     addMouseMotionListener(this);
  141.     btnBring.addActionListener(this);
  142.     btnSend.addActionListener(this);
  143.  
  144.     int i;
  145.     Image img;
  146.     String name;
  147.     MediaTracker tracker = new MediaTracker (this);
  148.  
  149.     offsize = getSize();
  150.     offimg = createImage (offsize.width, offsize.height);
  151.     offg = offimg.getGraphics();
  152.  
  153.     for (i=0; i<NPictures; i+=1) {
  154.                 if (i < 2) name = "T"+(i+1)+".jpg";
  155.                 else name = "T"+(i+1)+".gif";
  156.         img = getImage (getDocumentBase(), name);
  157.         tracker.addImage (img, i);
  158.         showStatus ("Getting image: "+name);
  159.         try {
  160.             tracker.waitForID (i);
  161.         } catch (InterruptedException e) { }
  162.         pictures[i] = new Picture (img, i*10, i*20, this);
  163.     }
  164. }
  165.  
  166. /**
  167.  * reverse the order of update for efficiency
  168.  * @param g - destination graphics object
  169.  */
  170. public void paint (Graphics g) {
  171.  
  172.     update (g);
  173. }
  174.  
  175. /**
  176.  * override update to avoid erase flicker
  177.  * @param g - destination graphics object
  178.  */
  179. public void update (Graphics g) {
  180.  
  181.     int i;
  182.  
  183.     offg.setColor (Color.black);
  184.     offg.fillRect (0, 0, offsize.width, offsize.height);
  185.     for (i=0; i<NPictures; i+=1) {
  186.         if (i == selectedPic) pictures[i].paint (offg, true);
  187.         else pictures[i].paint (offg, false);
  188.     }
  189.     g.drawImage(offimg, 0, 0, this);
  190. }
  191.  
  192. /**
  193.  * determine which image the user clicked 
  194.  * @param evt - event object
  195.  * @param x, y - mouse position
  196.  */
  197. public void mouseClicked(MouseEvent e){}
  198. public void mouseEntered(MouseEvent e){}
  199. public void mouseExited(MouseEvent e){} 
  200.  
  201. public void mouseReleased(MouseEvent e)
  202. {
  203.     repaint ();
  204. }
  205.  
  206. public void mouseMoved(MouseEvent e){}
  207.  
  208. public void mousePressed(MouseEvent e)
  209. {
  210.     int x =e.getX();
  211.     int y =e.getY();
  212.  
  213.     int i;
  214.  
  215.     selectedPic = -1;
  216.     for (i=NPictures-1; i>=0; i-=1)
  217.     {
  218.         if (pictures[i].inside (x, y))
  219.         {
  220.             selectedPic = i;
  221.             dx = x - pictures[i].xpos;
  222.             dy = y - pictures[i].ypos;
  223.             break;
  224.         }
  225.     }
  226. }
  227.  
  228. public void mouseDragged(MouseEvent e)
  229. {
  230.     int x =e.getX();
  231.     int y =e.getY();
  232.     int i;
  233.  
  234.     if (selectedPic < 0) return;
  235.     /* for (i=NPictures-1; i>=0; i-=1)
  236.     {
  237.  
  238.         pictures[selectedPic].setPosition (x - dx, y - dy);
  239.         repaint ();
  240.     } */
  241.  
  242. }
  243. /**
  244.  * reorder the images depending on which button is pressed
  245.  * @param evt - event object
  246.  * @param arg - target object
  247.  */
  248. public void actionPerformed(ActionEvent evt)
  249. {
  250.     Object object1 = evt.getSource();
  251.     int i;
  252.     Picture temp;
  253.  
  254.  
  255.     if (object1 == btnBring) {
  256.         if (selectedPic < 0) return;
  257.         temp = pictures[selectedPic];
  258.         for (i=selectedPic; i<NPictures-1; i+=1)
  259.         {
  260.             pictures[i] = pictures[i+1];
  261.         }
  262.         pictures[NPictures-1] = temp;
  263.         selectedPic = NPictures - 1;
  264.         repaint ();
  265.         return;
  266.     }
  267.     if (object1 == btnSend)
  268.     {
  269.         if (selectedPic < 0) return;
  270.         temp = pictures[selectedPic];
  271.         for (i=selectedPic; i>0; i-=1) 
  272.         {
  273.             pictures[i] = pictures[i-1];
  274.         }
  275.         pictures[0] = temp;
  276.         selectedPic = 0;
  277.         repaint ();
  278.         return;
  279.     }
  280. }
  281. }
  282.