Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package kulki;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import javax.swing.Timer;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.util.ArrayList;
- import java.util.Random;
- import javax.swing.*;
- public class JavaApplication38
- {
- public static void main(String[] args)
- {
- Okno a1 = new Okno("KOT", 200, 200, 800, 600);
- }
- }
- //---------------------------------
- class Okno extends JFrame
- {
- private Container tlo;
- private JButton start;
- private JButton stop;
- private JButton koniec;
- private MojPanel cent = new MojPanel();
- public Okno(String atyt, int ax0, int ay0, int aszer, int awys) {
- super(atyt);
- setBounds(ax0, ay0, aszer, awys);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- tlo = getContentPane();
- budujUI();
- setVisible(true);
- }
- private void budujUI() {
- tlo.setLayout(new BorderLayout());
- JPanel gorny = new JPanel();
- gorny.setBackground(Color.GRAY);
- gorny.setLayout(new FlowLayout(FlowLayout.CENTER));
- tlo.add(BorderLayout.NORTH, gorny);
- // cent.setLayout(new FlowLayout(FlowLayout.CENTER));
- cent.setFocusable(true);
- cent.setRequestFocusEnabled(true);
- cent.addKeyListener(new KeyListener()
- {
- @Override
- public void keyTyped(KeyEvent ke)
- {
- }
- @Override
- public void keyPressed(KeyEvent ke)
- {
- int k = ke.getKeyCode();
- if(k == KeyEvent.VK_LEFT)
- {
- cent.p1.wLewo();
- cent.repaint();
- }
- if(k == KeyEvent.VK_RIGHT)
- {
- cent.p1.wPrawo();
- cent.repaint();
- }
- }
- @Override
- public void keyReleased(KeyEvent ke)
- {
- }
- });
- tlo.add(cent);
- koniec = new JButton("END");
- start = new JButton("START");
- stop = new JButton("STOP");
- koniec.addActionListener(new KlikKoniec());
- start.addActionListener(new KlikStart());
- stop.addActionListener(new KlikStop());
- gorny.add(koniec);
- gorny.add(start);
- gorny.add(stop);
- }
- class KlikKoniec implements ActionListener
- {
- @Override
- public void actionPerformed(ActionEvent ae) {
- cent.zegarStop();
- dispose();
- }
- }
- class KlikStart implements ActionListener
- {
- @Override
- public void actionPerformed(ActionEvent ae) {
- cent.zegarStart();
- cent.requestFocus();
- }
- }
- class KlikStop implements ActionListener
- {
- @Override
- public void actionPerformed(ActionEvent ae) {
- cent.zegarStop();
- }
- }
- }
- //------------------------------------------------------------------------------
- class MojPanel extends JPanel
- {
- private ArrayList <Figura> figury = new ArrayList <Figura>();
- private Figura fi = new Figura( Color.RED, Color.GREEN, 200, 300, 50, -1, 2) {};
- public Paletka p1 = new Paletka(400, 100, 10, Color.black);
- private Timer t;
- public MojPanel()
- {
- super();
- Random r = new Random();
- for(int i=0; i<5; i++)
- {
- figury.add(new Kulka(Color.RED,Color.WHITE,r.nextInt(400) + 100,r.nextInt(300) + 100,r.nextInt(50)+10,r.nextInt(21)-10,r.nextInt(21)-10));
- figury.add(new Kwadrat(Color.GREEN,Color.WHITE,r.nextInt(400) + 100,r.nextInt(300) + 100,r.nextInt(50)+10,r.nextInt(21)-10,r.nextInt(21)-10));
- // figury.add(new Prostokat(Color.BLUE,Color.WHITE,r.nextInt(400) + 100,r.nextInt(300) + 100,r.nextInt(50)+10,r.nextInt(21)-10,r.nextInt(21)-10));
- }
- t=new Timer(20,new ActionListener()
- {
- @Override
- public void actionPerformed(ActionEvent ae) {
- int szer = getWidth(), wys = getHeight();
- for(Figura f: figury)
- f.porusz( szer, wys);
- fi.porusz(szer, wys);
- repaint();
- }
- });
- }
- @Override
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- for(Figura f: figury)
- f.rysuj(g);
- fi.rysuj(g);
- p1.rysuj(g, getHeight());
- repaint();
- }
- public void zegarStart()
- {
- t.start();
- }
- public void zegarStop()
- {
- t.stop();
- }
- }
- //------------------------------------------------------------------------------
- abstract class Figura
- {
- Color a, b;
- int x, y, r, vx, vy;
- public Figura(Color aa, Color ab, int ax, int ay, int ar, int avx, int avy)
- {
- a = aa;
- b = ab;
- x=ax;
- y=ay;
- r=ar;
- vx=avx;
- vy=avy;
- }
- public void rysuj(Graphics g)
- {
- g.setColor(a);
- g.fillOval(x-r, y-r, 2*r, 2*r);
- g.setColor(b);
- g.drawOval(x-r, y-r, 2*r, 2*r);
- }
- public void porusz(int szer, int wys)
- {
- if(x >= szer-r || x <= r)
- {
- vx=-vx;
- }
- if(y >= wys-r || y <= r)
- {
- vy=-vy;
- }
- x+=vx;
- y+=vy;
- }
- }
- class Kulka extends Figura
- {
- public Kulka(Color aa, Color ab, int ax, int ay, int ar, int avx, int avy) {
- super(aa, ab, ax, ay, ar, avx, avy);
- }
- @Override
- public void rysuj(Graphics g)
- {
- g.setColor(a);
- g.fillOval(x-r, y-r, 2*r, 2*r);
- g.setColor(b);
- g.drawOval(x-r, y-r, 2*r, 2*r);
- }
- }
- class Kwadrat extends Figura
- {
- public Kwadrat(Color aa, Color ab, int ax, int ay, int ar, int avx, int avy) {
- super(aa, ab, ax, ay, ar, avx, avy);
- }
- @Override
- public void rysuj(Graphics g)
- {
- g.setColor(a);
- g.fillRect(x-r, y-r, 2*r, 2*r);
- g.setColor(b);
- g.drawRect(x-r, y-r, 2*r, 2*r);
- }
- }
- class Paletka
- {
- private int x, szer, wys, dx = 10;
- private Color k;
- public Paletka(int ax, int aszer, int awys, Color ak)
- {
- x= ax;
- szer = aszer;
- wys= awys;
- k=ak;
- }
- public void rysuj(Graphics g, int wysOkna)
- {
- g.setColor(k);
- g.fillRect(x - szer/2, wysOkna - wys, szer, wys);
- }
- public void wLewo()
- {
- x-=dx;
- }
- public void wPrawo()
- {
- x+=dx;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement