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. public class ImagePanel extends JComponent
  5. {
  6. // The current width and height of this panel
  7. private int width, height;
  8. // An internal image buffer that is used for painting. For
  9. // actual display, this image buffer is then copied to screen.
  10. private OFImage panelImage;
  11. /**
  12. * Create a new, empty ImagePanel.
  13. */
  14. public ImagePanel()
  15. {
  16. width = 360; // arbitrary size for empty panel
  17. height = 240;
  18. panelImage = null;
  19. }
  20. /**
  21. * Set the image that this panel should show.
  22. *
  23. * @param image The image to be displayed.
  24. */
  25. public void setImage(OFImage image)
  26. {
  27. if(image != null) {
  28. width = image.getWidth();
  29. height = image.getHeight();
  30. panelImage = image;
  31. repaint();
  32. }
  33. }
  34. /**
  35. * Clear the image on this panel.
  36. */
  37. public void clearImage()
  38. {
  39. Graphics imageGraphics = panelImage.getGraphics();
  40. imageGraphics.setColor(Color.LIGHT_GRAY);
  41. imageGraphics.fillRect(0, 0, width, height);
  42. repaint();
  43. }
  44. // The following methods are redefinitions of methods
  45. // inherited from superclasses.
  46. /**
  47. * Tell the layout manager how big we would like to be.
  48. * (This method gets called by layout managers for placing
  49. * the components.)
  50. *
  51. * @return The preferred dimension for this component.
  52. */
  53. public Dimension getPreferredSize()
  54. {
  55. return new Dimension(width, height);
  56. }
  57. /**
  58. * This component needs to be redisplayed. Copy the internal image
  59. * to screen. (This method gets called by the Swing screen painter
  60. * every time it want this component displayed.)
  61. *
  62. * @param g The graphics context that can be used to draw on this component.
  63. */
  64. public void paintComponent(Graphics g)
  65. {
  66. Dimension size = getSize();
  67. g.clearRect(0, 0, size.width, size.height);
  68. if(panelImage != null) {
  69. g.drawImage(panelImage, 0, 0, null);
  70. }
  71. }
  72. }
');