Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.07 KB | None | 0 0
  1. package Zestawy;
  2.  
  3.  
  4. import javax.swing.*;
  5. import java.awt.event.*;
  6. import java.awt.*;
  7. import java.awt.geom.*;
  8. import java.awt.image.BufferedImage;
  9. import java.io.File;
  10. import java.io.IOException;
  11. import java.util.logging.Level;
  12. import java.util.logging.Logger;
  13. import javax.imageio.ImageIO;
  14. import javax.swing.ImageIcon;
  15.  
  16. class Kulka extends Ellipse2D.Float
  17. {
  18.    Planszad p;
  19.    int dx,dy;
  20.    Belka b;
  21.    Cegla[] cegly;
  22.      
  23.  
  24.    Kulka(Planszad p, Belka b, Cegla[] cegly, int x,int y,int dx,int dy)
  25.    {                                          
  26.       this.x=x;                              
  27.       this.y=y;                              
  28.       this.width=10;                          
  29.       this.height=10;
  30.       this.b=b;
  31.       this.cegly = cegly;
  32.       this.p=p;                              
  33.       this.dx=dx;                            
  34.       this.dy=dx;
  35.    }                                          
  36.  
  37.    void nextKrok()                                        
  38.    {                                                    
  39.       x+=dx;                                            
  40.       y+=dy;                                            
  41.       if(getMinX()<0 || getMaxX()>p.getWidth())  dx=-dx;
  42.       if( getMinY()>p.getHeight()) dy=-dy;
  43.       if(y==350) {
  44.           for(int i=0; i < cegly.length; i++)
  45.           {
  46.                   dy =- dy;
  47.                   cegly[i].x = 1000;
  48.                   cegly[i].y = 1000;}
  49.           x=200;
  50.           y=200;
  51.           SilnikKulki.licznik = 0;
  52.              
  53.       };
  54.       if(b.intersects(x, y, width, height))
  55.       {
  56.         dy=-dy;
  57.         }
  58.       for(int i=0; i < cegly.length; i++)
  59.       {
  60.           if(cegly[i].intersects(x, y, width, height))
  61.           {
  62.               dy =- dy;
  63.               cegly[i].x = 1000;
  64.               cegly[i].y = 1000;
  65.              
  66.           }
  67.       }
  68.       p.repaint();                                      
  69.    }
  70.    
  71.    
  72. }
  73. class SilnikKulki extends Thread
  74. {
  75.    public static int licznik=2;
  76.    Kulka a;
  77.  
  78.    SilnikKulki(Kulka a)
  79.    {              
  80.       this.a=a;        
  81.       start();          
  82.    }                    
  83.  
  84.    public void run()                  
  85.    {                                  
  86.       try                            
  87.       {                              
  88.          while(licznik > 0)                  
  89.          {                            
  90.             a.nextKrok();            
  91.             sleep(15);                
  92.          }
  93.          
  94.       }                              
  95.       catch(InterruptedException e){}
  96.    }                                  
  97. }
  98. class Cegla extends Rectangle2D.Float
  99. {
  100.     Cegla(int x, int y)
  101.     {
  102.         this.x = x;
  103.         this.y = y;
  104.         this.width =60;
  105.         this.height =10;
  106.     }
  107.     void setX(int x)
  108.     {
  109.         this.x=x;
  110.     }
  111.     void setY(int y)
  112.     {
  113.         this.y=y;
  114.     }
  115. }
  116. class Belka extends Rectangle2D.Float
  117. {
  118.    Belka(int x)      
  119.    {                  
  120.       this.x=x;      
  121.       this.y=330;    
  122.       this.width=60;  
  123.       this.height=10;
  124.    }                  
  125.  
  126.    void setX(int x)
  127.    {                
  128.       this.x=x;    
  129.    }                
  130. }
  131. class Planszad extends JPanel implements MouseMotionListener
  132. {
  133.    Belka b;
  134.    Kulka a;
  135.    SilnikKulki s;
  136.    Cegla[] cegly={new Cegla(10,5), new Cegla(80,5), new Cegla(150,5), new Cegla(220,5), new Cegla(290,5), new Cegla(360,5), new Cegla(10,20), new Cegla(80,20), new Cegla(150,20), new Cegla(220,20), new Cegla(290,20), new Cegla(360,20)};
  137.  
  138.    Planszad()                        
  139.    {                                
  140.       super();                      
  141.       addMouseMotionListener(this);  
  142.      
  143.       b=new Belka(100);              
  144.       a=new Kulka(this, b, cegly, 100,100,1,1);
  145.       s=new SilnikKulki(a);  
  146.    }                                
  147.  
  148.    public void paintComponent(Graphics g)
  149.    {                                      
  150.       super.paintComponent(g);            
  151.       Graphics2D g2d=(Graphics2D)g;      
  152.  
  153.       g2d.fill(a);                        
  154.       g2d.fill(b);
  155.       g2d.setColor(Color.blue);
  156.       for(int i=0; i<cegly.length; i++)
  157.       {
  158.           g2d.fill(cegly[i]);
  159.       }
  160.       if(SilnikKulki.licznik<=0)
  161.       {
  162.         g2d.clearRect(0,0,getWidth(),getHeight());
  163.         BufferedImage bsrc = null;
  164.           try {
  165.               bsrc = ImageIO.read(new File("wiadro.jpg"));
  166.           } catch (IOException ex) {
  167.               System.out.print(ex);
  168.           }
  169.         Image imgOut = bsrc.getScaledInstance(390,345,Image.SCALE_DEFAULT);
  170.         g2d.drawImage(imgOut, 0,0,null);
  171.        
  172.       }
  173.    }                                      
  174.  
  175.    public void mouseMoved(MouseEvent e)
  176.    {                                    
  177.       b.setX(e.getX()-50);              
  178.       repaint();                        
  179.    }                                    
  180.  
  181.    public void mouseDragged(MouseEvent e)
  182.    {
  183.        b.setX(e.getX()-50);              
  184.           repaint();
  185.    }                                      
  186. }
  187. public class Kulka_belka {
  188.  
  189.    public static void main(String[] args)                      
  190.    {                                                          
  191.       javax.swing.SwingUtilities.invokeLater(new Runnable()    
  192.       {                                                        
  193.          public void run()                                    
  194.          {                                                    
  195.             Planszad p;                                        
  196.             p=new Planszad();                                  
  197.  
  198.             JFrame jf=new JFrame();                            
  199.             jf.add(p);                                        
  200.             jf.setTitle("GRA");  
  201.             jf.setSize(450,400);
  202.             jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  203.             jf.setVisible(true);                              
  204.          }                                                    
  205.       });                                                      
  206.    }                                  
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement