Advertisement
Guest User

Untitled

a guest
May 24th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. package ergastirio16;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.MouseEvent;
  8. import java.awt.event.MouseMotionListener;
  9. import java.awt.image.BufferedImage;
  10. import java.io.File;
  11. import java.io.IOException;
  12. import javax.imageio.ImageIO;
  13. import javax.swing.JFrame;
  14. import javax.swing.JPanel;
  15. import javax.swing.Timer;
  16.  
  17. /**
  18. *
  19. * @author aifragkou
  20. */
  21. public class Game extends JFrame implements ActionListener , MouseMotionListener{
  22. private MyPanel panel;
  23. private Timer timer;
  24. public Game(){
  25. this.setBounds(0,0,800,600);
  26. this.setTitle("Hello unicorn");
  27. this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  28. this.panel = new MyPanel(this);
  29. this.add(this.panel);
  30. this.setVisible(true);
  31. this.timer = new Timer(33,this);
  32. this.timer.start();
  33. }
  34.  
  35. @Override
  36. public void actionPerformed(ActionEvent e) {
  37. // this.panel.setUnicornX(this.panel.getUnicornX()+1);
  38. this.repaint();
  39. }
  40.  
  41. @Override
  42. public void mouseDragged(MouseEvent e) {
  43.  
  44. }
  45.  
  46. @Override
  47. public void mouseMoved(MouseEvent e) {
  48. this.panel.setUnicornX(e.getX());
  49. this.panel.setUnicornY(e.getY());
  50. }
  51. private class MyPanel extends JPanel{
  52.  
  53. private Game gameInstance;
  54. private BufferedImage image;
  55. private int unicornX,unicornY;
  56.  
  57. public int getUnicornX() {
  58. return unicornX;
  59. }
  60.  
  61. public void setUnicornX(int unicornX) {
  62. this.unicornX = unicornX;
  63. }
  64.  
  65. public int getUnicornY() {
  66. return unicornY;
  67. }
  68.  
  69. public void setUnicornY(int unicornY) {
  70. this.unicornY = unicornY;
  71. }
  72.  
  73. public MyPanel(Game game){
  74. this.gameInstance = game;
  75. try {
  76. this.image = (BufferedImage)ImageIO.read(new File("C:\\Users\\aifragkou\\Downloads\\unicorn1.jpg"));
  77. } catch (IOException ex) {
  78. }
  79. }
  80.  
  81. @Override
  82. public void paint(Graphics g){
  83. if(Math.random()>0.5){
  84. g.setColor(Color.BLACK);
  85. }else{
  86. g.setColor(Color.GREEN);
  87. }
  88. g.fillRect(0,0, this.gameInstance.getWidth(),this.gameInstance.getHeight());
  89. g.drawImage(image,this.getUnicornX(),this.getUnicornY(),this);
  90. g.setColor(Color.GREEN);
  91. g.drawString("Hello world", 50, 50);
  92.  
  93. }
  94. }
  95.  
  96.  
  97. public static void main(String[] args){
  98. Game g = new Game();
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement