Advertisement
icebit

Java_01.06.2016 _SWINGWORKER

Jun 1st, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.49 KB | None | 0 0
  1.  
  2. public class Cordinates {
  3.  
  4.     private int x;
  5.     private int y;
  6.    
  7.     public Cordinates(int x, int y){
  8.         this.x = x;
  9.         this.y = y;
  10.     }
  11.    
  12.     public synchronized void increase(){
  13.         x ++;
  14.         y ++;
  15.     }
  16.    
  17.     public synchronized int[] getXY(){
  18.         return new int[]{x,y};
  19.     }
  20.    
  21.    
  22. }
  23. --------------------------------------------------------------------------------------------------------
  24. import java.awt.Color;
  25. import java.awt.Graphics;
  26. import java.awt.event.ActionEvent;
  27. import java.awt.event.ActionListener;
  28.  
  29. import javax.swing.JLabel;
  30.  
  31.  
  32.  
  33. public class MyLabel extends JLabel{
  34.  
  35.     Cordinates kordy = new Cordinates(100,100);
  36.     MySwingWorker sw = new MySwingWorker(kordy);
  37.  
  38.    
  39.     MyLabel(String title){
  40.         super(title);
  41.         sw.execute();
  42.  
  43.     }
  44.     protected void paintComponent(Graphics g){
  45.         super.paintComponent(g);
  46.        
  47.         g.setColor(Color.red);
  48.         int[] xy = kordy.getXY();
  49.         g.fillRect(xy[0],xy[1], 50,50);
  50.     }
  51.    
  52.     public void tick(){
  53.        
  54.     }
  55. }
  56. -----------------------------------------------------------------------
  57. import javax.swing.SwingWorker;
  58.  
  59.  
  60. public class MySwingWorker extends SwingWorker{
  61.  
  62.     Cordinates kordy;
  63.    
  64.     public MySwingWorker(Cordinates kordy){
  65.         super();
  66.         this.kordy = kordy;
  67.     }
  68.    
  69.     @Override
  70.     protected Object doInBackground() throws Exception {
  71.         for(int i =0; i<600 ; i++){
  72.             Thread.sleep(100);
  73.             kordy.increase();
  74.         }
  75.         return null;
  76.     }
  77.  
  78. }
  79.  
  80. ----------------------------------------------------------------------------------
  81. import java.awt.BorderLayout;
  82. import java.awt.HeadlessException;
  83. import java.awt.event.ActionEvent;
  84. import java.awt.event.ActionListener;
  85.  
  86. import javax.swing.JButton;
  87. import javax.swing.JFrame;
  88. import javax.swing.JPanel;
  89. import javax.swing.SwingUtilities;
  90. import javax.swing.Timer;
  91. import javax.swing.WindowConstants;
  92.  
  93.  
  94.  
  95. public class SWR extends JFrame{
  96.    
  97.  
  98.     public SWR(String title) throws HeadlessException{
  99.    
  100.         super(title);
  101.         setSize(720, 700);
  102.         JPanel panel = new JPanel();
  103.         add(panel);
  104.         panel.setLayout(new BorderLayout());
  105.         final MyLabel ml = new MyLabel("xD");
  106.         Timer timer;
  107.         add(ml, BorderLayout.CENTER);
  108.         setVisible(true);
  109.         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  110.        
  111.         timer = new Timer(100, new ActionListener(){
  112.             public void  actionPerformed(ActionEvent e){
  113.                 ml.repaint();
  114.             }
  115.         });
  116.         timer.start();
  117.     }
  118.    
  119.    
  120.     public static void main(String[] args){
  121.    
  122.         SwingUtilities.invokeLater(new Runnable() {
  123.        
  124.             public void run(){
  125.                 SWR main = new SWR("Okno");
  126.             }      
  127.         });
  128.        
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement