Advertisement
Guest User

anal

a guest
Dec 7th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.55 KB | None | 0 0
  1. package a1;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import javax.swing.Timer;
  5. import java.awt.event.ActionListener;
  6. import javax.swing.*;
  7. public class A1 {
  8.     public static void main(String[] args) {
  9.         Okno a1 = new Okno("Quake V 0.01", 200, 200, 800, 600);
  10.     }
  11. }
  12. //---------------------------------
  13. class Okno extends JFrame {
  14.     private Container tlo;
  15.     private JButton start;
  16.     private JButton stop;
  17.     private JButton koniec;
  18.     private MojPanel cent;
  19.     private Timer t;
  20.  
  21.     public Okno(String atyt, int ax0, int ay0, int aszer, int awys) {
  22.         super(atyt);
  23.         setBounds(ax0, ay0, aszer, awys);
  24.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  25.         tlo = getContentPane();
  26.         budujUI();
  27.         setVisible(true);      
  28.     }
  29.     private void budujUI() {
  30.         tlo.setLayout(new BorderLayout());
  31.         JPanel gorny = new JPanel();
  32.         gorny.setBackground(Color.GRAY);
  33.         gorny.setLayout(new FlowLayout(FlowLayout.CENTER));
  34.         cent = new MojPanel();
  35.         cent.setLayout(new FlowLayout(FlowLayout.CENTER));
  36.         tlo.add(BorderLayout.NORTH, gorny);
  37.         tlo.add(cent);
  38.         koniec = new JButton("Koniec");
  39.         start = new JButton("Start");
  40.         stop = new JButton("Stop");
  41.         koniec.addActionListener(new KlikKoniec());
  42.         start.addActionListener(new KlikStart());
  43.         stop.addActionListener(new KlikStop());
  44.        
  45.         gorny.add(koniec);
  46.         gorny.add(start);
  47.         gorny.add(stop);  
  48.        
  49.         t = new Timer(10, new ActionListener() {
  50.  
  51.             @Override
  52.             public void actionPerformed(ActionEvent ae) {
  53.                 cent.poruszkulki();
  54.             }
  55.         });
  56.     }
  57.     class KlikKoniec implements ActionListener
  58.     {
  59.         @Override
  60.         public void actionPerformed(ActionEvent ae) {
  61.             t.stop();
  62.             dispose();
  63.         }
  64.     }
  65.     class KlikStart implements ActionListener
  66.     {
  67.         @Override
  68.         public void actionPerformed(ActionEvent ae) {
  69.             t.start();
  70.         }
  71.     }
  72.     class KlikStop implements ActionListener
  73.     {
  74.         @Override
  75.         public void actionPerformed(ActionEvent ae) {
  76.             t.stop();
  77.         }
  78.     }
  79.     class MojPanel extends JPanel {
  80.        Kulka kula = new Kulka(Color.WHITE, 300, 300, 100, 10, -1);
  81.        Kulka kula2 = new Kulka(Color.RED, 300, 300, 50, 5, -6);
  82.         public MojPanel() {
  83.             super();
  84.             setBackground(Color.BLACK);
  85.         }
  86.         @Override
  87.         public void paintComponent(Graphics g) {
  88.             super.paintComponent(g);
  89.             kula2.rysuj(g);
  90.             kula.rysuj(g);      
  91.         }
  92.         public void poruszkulki() {
  93.             kula2.porusz(getWidth(),getHeight());
  94.             kula.porusz(getWidth(),getHeight());
  95.             repaint();
  96.         }    
  97.     }}
  98.    
  99. class Kulka {
  100.         private int x, y, r, vx, vy;
  101.         Color a;
  102.         public Kulka(Color aa, int ax, int ay, int ar, int avx, int avy) {
  103.             a = aa;
  104.             x=ax;
  105.             y=ay;
  106.             r=ar;
  107.             vx=avx;
  108.             vy=avy;
  109.         }
  110.         public void rysuj(Graphics g) {
  111.                 g.setColor(a);
  112.                 g.fillOval(x-r, y-r, 2*r, 2*r);
  113.         }
  114.         public void porusz(int szer, int wys) {
  115.             if(x >= szer-r || x <= r) {
  116.                 vx=-vx;
  117.             }
  118.             if(y >= wys-r || y <= r) {
  119.                 vy=-vy;
  120.             }
  121.             x+=vx;
  122.             y+=vy;
  123.         }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement