Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Cordinates {
- private int x;
- private int y;
- public Cordinates(int x, int y){
- this.x = x;
- this.y = y;
- }
- public synchronized void increase(){
- x ++;
- y ++;
- }
- public synchronized int[] getXY(){
- return new int[]{x,y};
- }
- }
- --------------------------------------------------------------------------------------------------------
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JLabel;
- public class MyLabel extends JLabel{
- Cordinates kordy = new Cordinates(100,100);
- MySwingWorker sw = new MySwingWorker(kordy);
- MyLabel(String title){
- super(title);
- sw.execute();
- }
- protected void paintComponent(Graphics g){
- super.paintComponent(g);
- g.setColor(Color.red);
- int[] xy = kordy.getXY();
- g.fillRect(xy[0],xy[1], 50,50);
- }
- public void tick(){
- }
- }
- -----------------------------------------------------------------------
- import javax.swing.SwingWorker;
- public class MySwingWorker extends SwingWorker{
- Cordinates kordy;
- public MySwingWorker(Cordinates kordy){
- super();
- this.kordy = kordy;
- }
- @Override
- protected Object doInBackground() throws Exception {
- for(int i =0; i<600 ; i++){
- Thread.sleep(100);
- kordy.increase();
- }
- return null;
- }
- }
- ----------------------------------------------------------------------------------
- import java.awt.BorderLayout;
- import java.awt.HeadlessException;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.SwingUtilities;
- import javax.swing.Timer;
- import javax.swing.WindowConstants;
- public class SWR extends JFrame{
- public SWR(String title) throws HeadlessException{
- super(title);
- setSize(720, 700);
- JPanel panel = new JPanel();
- add(panel);
- panel.setLayout(new BorderLayout());
- final MyLabel ml = new MyLabel("xD");
- Timer timer;
- add(ml, BorderLayout.CENTER);
- setVisible(true);
- setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- timer = new Timer(100, new ActionListener(){
- public void actionPerformed(ActionEvent e){
- ml.repaint();
- }
- });
- timer.start();
- }
- public static void main(String[] args){
- SwingUtilities.invokeLater(new Runnable() {
- public void run(){
- SWR main = new SWR("Okno");
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement