Advertisement
Guest User

forMrKott

a guest
Jun 26th, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.95 KB | None | 0 0
  1. import java.io.IOException;
  2. import javax.swing.JFrame;
  3.  
  4. public class Game { // ãëàâíûé êëàññ
  5.  
  6.  
  7.     public static void main(String[] args) throws IOException {
  8.         JFrame frame = new JFrame("GetFruit");
  9.  
  10.         frame.setLayout(null);
  11.        
  12.         int standX = 250;
  13.         int standY = 350;        
  14.         Stand stand = new Stand();
  15.         stand.setBounds(standX,standY,100,100);
  16.        
  17.         int potatoX = (int)(Math.random() * 486); // ãåíåðèðóåì ñëó÷àéíûé x
  18.         int potatoY = 100;        
  19.         Potato potato = new Potato();
  20.         potato.setBounds(potatoX,potatoY,50,50);
  21.        
  22.         frame.add(stand).setVisible(true);
  23.         frame.add(potato).setVisible(true);
  24.        
  25.         KeyboardObserver observer = new KeyboardObserver(stand);
  26.         frame.addKeyListener(observer);
  27.        
  28.         frame.setSize(700,500);
  29.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  30.         frame.setResizable(false);
  31.         frame.setVisible(true);
  32.     }
  33. }
  34.  
  35. =================================================
  36. import java.awt.event.KeyAdapter;
  37. import java.awt.event.KeyEvent;
  38.  
  39. public class KeyboardObserver extends KeyAdapter { // Îòñëåæèâàåò íàæàòèÿ êëàâèø
  40.     private Stand stand;
  41.  
  42.     public KeyboardObserver(Stand stand) {
  43.         this.stand = stand;
  44.     }
  45.  
  46.  
  47.     @Override
  48.     public void keyPressed(KeyEvent e) {
  49.         if(e.getKeyCode() == KeyEvent.VK_LEFT){
  50.             stand.setDirection(StandDirection.LEFT);
  51.             stand.setLocation(stand.getX()-10, stand.getY());
  52.         }else if(e.getKeyCode() == KeyEvent.VK_RIGHT){
  53.             stand.setDirection(StandDirection.RIGHT);
  54.             stand.setLocation(stand.getX()+10, stand.getY());
  55.         }
  56.     }
  57. }
  58. =================================================
  59. import java.awt.Graphics;
  60. import java.awt.image.BufferedImage;
  61. import java.io.File;
  62. import java.io.IOException;
  63.  
  64. import javax.imageio.ImageIO;
  65. import javax.swing.JPanel;
  66.  
  67. public class Potato extends JPanel{ // Êàðòîøêà
  68.     private BufferedImage image;
  69.  
  70.     public Potato(){
  71.         try {
  72.             image = ImageIO.read(new File("src\\images\\potato.jpg"));
  73.         } catch (IOException e) {
  74.             System.out.println("Íåâåðíûé ïóòü ê èçîáðàæåíèþ.");
  75.         }
  76.     }
  77.    
  78.     @Override
  79.     public void paintComponent(Graphics g) {
  80.         super.paintComponent(g);
  81.         g.drawImage(image,0,0,null);
  82.     }
  83. }
  84. =================================================
  85. import java.awt.Graphics;
  86. import java.awt.image.BufferedImage;
  87. import java.io.File;
  88. import java.io.IOException;
  89.  
  90. import javax.imageio.ImageIO;
  91. import javax.swing.JPanel;
  92.  
  93. public class Stand extends JPanel { // Ïîäñòàâêà
  94.     private StandDirection direction;
  95.  
  96.     private BufferedImage image;
  97.  
  98.  
  99.     public Stand(){
  100.         try {
  101.             image = ImageIO.read(new File("src\\images\\p.jpg"));
  102.         } catch (IOException e) {
  103.             System.out.println("Íåâåðíûé ïóòü ê èçîáðàæåíèþ.");
  104.         }
  105.     }
  106.  
  107.     public void setDirection(StandDirection direction) {
  108.         this.direction = direction;
  109.     }
  110.  
  111.     public StandDirection getDirection() {
  112.         return direction;
  113.     }
  114.    
  115. //    public void draw(){
  116. //        if (this.getDirection() == StandDirection.LEFT) {
  117. //            x -= 25;
  118. //            if(this.x <0 ){
  119. //                x+= 25;
  120. //            }
  121. //        } else if (this.getDirection() == StandDirection.RIGHT) {
  122. //            x += 25;
  123. //            if(this.x > 495){
  124. //                x -= 25;
  125. //            }
  126. //        }
  127. //    }
  128.    
  129.     @Override
  130.     public void paintComponent(Graphics g) {
  131.         super.paintComponent(g);
  132. //        draw();
  133.         g.drawImage(image, 0, 0, null);
  134.     }
  135. }
  136. =================================================
  137. public enum StandDirection { // enum ñî çíà÷åíèÿìè ïîäñòàâêè
  138.     LEFT,
  139.     RIGHT
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement