Crenox

Java Game Programming Image Basics

Apr 24th, 2014
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. package com.zetcode.main;
  2.  
  3. import javax.swing.*;
  4.  
  5. public class Zetcode extends JFrame
  6. {
  7. private static final int WIDTH = 280;
  8. private static final int HEIGHT = 280;
  9.  
  10. public Zetcode()
  11. {
  12. add(new Board());
  13. setVisible(true);
  14. setTitle("Donut");
  15. setSize(WIDTH, HEIGHT);
  16. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  17. setLocationRelativeTo(null);
  18. setResizable(false);
  19. }
  20.  
  21. public static void main(String args[])
  22. {
  23. new Zetcode();
  24. }
  25. }
  26. -------------------------------------------------------------------------------------------------------------------------------------
  27. // The Board is a panel, where the game takes place.
  28.  
  29. package com.zetcode.main;
  30.  
  31. import java.awt.Graphics;
  32. import java.awt.Graphics2D;
  33. import java.awt.Image;
  34. import javax.swing.ImageIcon;
  35. import javax.swing.*;
  36.  
  37. @SuppressWarnings("serial")
  38. public class Board extends JPanel
  39. {
  40. Image img;
  41.  
  42. // We display an image of a town on the Board. The image is drawn inside the paint() method.
  43. public Board()
  44. {
  45. // We create an ImageIcon
  46. ImageIcon ii = new ImageIcon(this.getClass().getResource("house.jpeg"));
  47. // We get an Image out of the ImageIcon
  48. img = ii.getImage();
  49. }
  50.  
  51. public void paint(Graphics g)
  52. {
  53. Graphics2D g2d = (Graphics2D) g;
  54. // We draw the image on the window
  55. g2d.drawImage(img, 10, 10, null);
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment