Advertisement
VinaDR

Untitled

Dec 12th, 2019
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. /**
  2. *
  3. * @author RyanR
  4. */
  5. import java.awt.*;
  6. import javax.swing.*;
  7. import java.awt.image.*;
  8. public class ImgPanel extends JComponent {
  9. private int width, height;
  10. private JImg panelImg;
  11. public ImgPanel() {
  12. width = 360;
  13. height = 240;
  14. panelImg = null;
  15. }
  16. public void setImage(JImg image) {
  17. if (image!=null) {
  18. width = image.getWidth();
  19. height = image.getHeight();
  20. panelImg = image;
  21. repaint();
  22. }
  23. }
  24. public void clearImage() {
  25. Graphics imageGraphics = panelImg.getGraphics();
  26. imageGraphics.setColor(Color.LIGHT_GRAY);
  27. imageGraphics.fillRect(0, 0, width, height);
  28. repaint();
  29. }
  30. public Dimension getPreferredSize() {
  31. return new Dimension(width, height);
  32. }
  33. public void paintComponent(Graphics g) {
  34. Dimension size = getSize();
  35. g.clearRect(0, 0, size.width, size.height);
  36. if(panelImg != null) {
  37. g.drawImage(panelImg, 0, 0, null);
  38. }
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement