Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.28 KB | None | 0 0
  1. public class Estradi extends JFrame implements Runnable{
  2.  
  3.     //x max = 471
  4.     //x min = 9
  5.    
  6.     // y max = 471
  7.     // y min = 32
  8.    
  9.     // mistä pallo lähtee
  10.   int x = 9;
  11.   int y = 32;
  12.  
  13.     // maksimit enne ruudulta poistumista
  14.   int minX = 9;
  15.   int minY = 32;
  16.  
  17.   int maxX = 471;
  18.   int maxY = 471;
  19.  
  20.     private Thread t=null;
  21.  
  22.    
  23.    
  24.     //sleepmetodin tauko
  25.     int tauko=1;
  26.  
  27.    
  28.    
  29.  
  30.  
  31.  
  32.     public Estradi(){
  33.        
  34.      
  35.  
  36.        
  37.             // ikkuna
  38.         this.setSize(500,500);
  39.         this.setTitle("Ikkuna");
  40.        
  41.             // Pysäyttää suorituksen painettaessa raksia
  42.         this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  43.        
  44.            
  45.        
  46.        
  47.        
  48.        
  49.     }
  50.    
  51.    
  52.  
  53.  
  54.    
  55.  
  56.     public void run(){
  57.  
  58.             //suuntaliput
  59.         int suuntay = 0;
  60.         int suuntax = 0;
  61.         int tulostustoisto=0;
  62.        
  63.         while(tulostustoisto<1){
  64.  
  65.            
  66.            
  67.            if(x < minX)
  68.            {
  69.                suuntax = 1;
  70.            }
  71.            if(x > maxX)
  72.            {
  73.                suuntax = 0;
  74.            }
  75.            if(y < minY)
  76.            {
  77.                suuntay = 1;
  78.            }
  79.            if(y > maxY)
  80.            {
  81.                suuntay = 0;
  82.            }
  83.            
  84.            if (suuntay == 1)
  85.            {
  86.                y++;
  87.            }
  88.            else
  89.                --y;
  90.            if (suuntax == 1)
  91.            {
  92.                x++;
  93.            }
  94.            else
  95.                 --x;
  96.                    
  97.            
  98.            
  99.            
  100.            
  101.            
  102.            
  103.            
  104.            
  105.  
  106.             try{
  107.                 repaint();
  108.                 t.sleep(tauko);
  109.  
  110.                 --tulostustoisto;
  111.                
  112.                
  113.  
  114.             }
  115.             catch(InterruptedException e) {}
  116.  
  117.         }
  118.  
  119.        
  120.  
  121.        
  122.  
  123.     }
  124.                 // neliön piirto
  125.  
  126.         public void paint(Graphics g) {
  127.  
  128.  
  129.    
  130.                  super.paint(g);
  131.                  
  132.    
  133.            
  134.            
  135.             g.setColor(Color.MAGENTA);
  136.             g.fillRect(x, y, 30, 30);
  137.            
  138.            
  139.    
  140.  
  141.          
  142.     }
  143.        
  144.    
  145.        
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement