Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Cursor;
  3. import java.awt.Font;
  4. import java.awt.FontFormatException;
  5. import java.awt.Graphics;
  6. import java.awt.Graphics2D;
  7. import java.awt.GraphicsEnvironment;
  8. import java.awt.Point;
  9. import java.awt.Toolkit;
  10. import java.awt.event.KeyEvent;
  11. import java.awt.event.KeyListener;
  12. import java.io.File;
  13. import java.io.FileInputStream;
  14. import java.io.IOException;
  15. import java.io.InputStream;
  16. import java.text.DecimalFormat;
  17.  
  18. import javax.swing.ImageIcon;
  19. import javax.swing.JFrame;
  20. import javax.swing.JOptionPane;
  21. import javax.swing.JPanel;
  22. import javafx.scene.media.Media;
  23. import javafx.scene.media.MediaPlayer;
  24. import java.io.File;
  25.  
  26. public class DuckHunt extends JPanel {
  27.  
  28. private ImageIcon imgBackground, imgForeground, imgCursor;
  29. private Cursor cursor;
  30. private int score, hits;
  31. private double accuracy;
  32. private DecimalFormat df;
  33. private Font f;
  34. private static final int PANEL_WIDTH = 640;
  35. private static final int PANEL_HEIGHT = 480;
  36. private boolean start = false;
  37.  
  38. public static void main(String[] args) {
  39.  
  40. new DuckHunt();
  41. }
  42.  
  43. public DuckHunt() {
  44.  
  45.  
  46.  
  47. df = new DecimalFormat("#%");
  48. f = new Font("m29", Font.PLAIN, 18);
  49.  
  50. imgBackground = new ImageIcon("images\\background.png");
  51. imgForeground = new ImageIcon("images\\foreground.png");
  52.  
  53. imgCursor = new ImageIcon("images\\cursor.png");
  54.  
  55. cursor = Toolkit.getDefaultToolkit().createCustomCursor(imgCursor.getImage(),
  56. new Point(imgCursor.getIconWidth() / 2, imgCursor.getIconHeight() / 2), "");
  57.  
  58. String musicFile = "mainscreen.mp3";
  59.  
  60. Media sound = new Media(new File(musicFile).toURI().toString());
  61. MediaPlayer mainscreen = new MediaPlayer(sound);
  62. mainscreen.play();
  63.  
  64. setLayout(null);
  65. setCursor(cursor);
  66. setFocusable(true);
  67. requestFocus();
  68. this.addKeyListener(new KeyListener() {
  69.  
  70. public void keyPressed(KeyEvent e) {
  71. if (e.getKeyCode() == KeyEvent.VK_SPACE) {
  72. start = true;
  73. }
  74. repaint();
  75. }
  76.  
  77. public void keyReleased(KeyEvent e) {
  78. }
  79.  
  80. public void keyTyped(KeyEvent e) {
  81. }
  82. });
  83.  
  84. JFrame frame = new JFrame();
  85. frame.setContentPane(this);
  86. frame.setTitle("Duck Hunt © Nintendo 1985");
  87. frame.setSize(PANEL_WIDTH, PANEL_HEIGHT);
  88. frame.setResizable(false);
  89. frame.setLocationRelativeTo(null);
  90. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  91. frame.setFocusable(false);
  92. frame.setVisible(true);
  93. }
  94.  
  95. public void paintComponent(Graphics p) {
  96.  
  97. super.paintComponent(p);
  98. Graphics2D p2 = (Graphics2D) p;
  99. Font customFont = null;
  100. try {
  101. // Create the font to use
  102. customFont = Font.createFont(Font.TRUETYPE_FONT, new File("m29.TTF")).deriveFont(18f);
  103. GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  104. // Register the font
  105. ge.registerFont(customFont);
  106. } catch (IOException e) {
  107. e.printStackTrace();
  108. } catch (FontFormatException e) {
  109. e.printStackTrace();
  110. }
  111. p2.setFont(customFont);
  112. p2.drawImage(imgBackground.getImage(), 0, 0, this);
  113. p2.setColor(new Color(128, 208, 16));
  114. p2.drawImage(imgForeground.getImage(), 0, 0, this);
  115. p2.drawString("PRESS SPACE TO START "
  116. + " " + score, PANEL_WIDTH - 480, PANEL_HEIGHT - 50);
  117. if (start == true) {
  118.  
  119. p2.drawImage(imgBackground.getImage(), 0, 0, this);
  120. p2.setFont(customFont);
  121. p2.setColor(new Color(128, 208, 16));
  122. p2.drawImage(imgForeground.getImage(), 0, 0, this);
  123. p2.drawString("SCORE: " + score, 20, PANEL_HEIGHT - 50);
  124. p2.drawString("HITS: " + hits, 250, PANEL_HEIGHT - 50);
  125. p2.drawString("ACCURACY: " + df.format(accuracy), 430, PANEL_HEIGHT - 50);
  126. }
  127. }
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement