document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.awt.image.*;
  4. /**
  5. * hana
  6. * An ImagePanel is a Swing component that can display an OFImage.
  7. * It is constructed as a subclass of JComponent with the added functionality
  8. * of setting an OFImage that will be displayed on the surface of this
  9. * component.
  10. *
  11. * @author Michael Kolling and David J. Barnes
  12. * @version 1.0
  13. */
  14. public class ImagePanel extends JComponent
  15. {
  16. // The current width and height of this panel
  17. private int width, height;
  18. // An internal image buffer that is used for painting. For
  19. // actual display, this image buffer is then copied to screen.
  20. private OFImage panelImage;
  21. /**
  22. * Create a new, empty ImagePanel.
  23. */
  24. public ImagePanel()
  25. {
  26. width = 360; // arbitrary size for empty panel
  27. height = 240;
  28. panelImage = null;
  29. }
  30. /**
  31. * Set the image that this panel should show.
  32. *
  33. * @param image The image to be displayed.
  34. */
  35. public void setImage(OFImage image)
  36. {
  37. if(image != null) {
  38. width = image.getWidth();
  39. height = image.getHeight();
  40. panelImage = image;
  41. repaint();
  42. }
  43. }
  44. /**
  45. * Clear the image on this panel.
  46. */
  47. public void clearImage()
  48. {
  49. Graphics imageGraphics = panelImage.getGraphics();
  50. imageGraphics.setColor(Color.LIGHT_GRAY);
  51. imageGraphics.fillRect(0, 0, width, height);
  52. repaint();
  53. }
  54. // The following methods are redefinitions of methods
  55. // inherited from superclasses.
  56. /**
  57. * Tell the layout manager how big we would like to be.
  58. * (This method gets called by layout managers for placing
  59. * the components.)
  60. *
  61. * @return The preferred dimension for this component.
  62. */
  63. public Dimension getPreferredSize()
  64. {
  65. return new Dimension(width, height);
  66. }
  67. /**
  68. * This component needs to be redisplayed. Copy the internal image
  69. * to screen. (This method gets called by the Swing screen painter
  70. * every time it want this component displayed.)
  71. *
  72. * @param g The graphics context that can be used to draw on this component.
  73. */
  74. public void paintComponent(Graphics g)
  75. {
  76. Dimension size = getSize();
  77. g.clearRect(0, 0, size.width, size.height);
  78. if(panelImage != null) {
  79. g.drawImage(panelImage, 0, 0, null);
  80. }
  81. }
  82. }
');