Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package a1;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import javax.swing.Timer;
- import java.awt.event.ActionListener;
- import javax.swing.*;
- public class A1 {
- public static void main(String[] args) {
- Okno a1 = new Okno("Quake V 0.01", 200, 200, 800, 600);
- }
- }
- //---------------------------------
- class Okno extends JFrame {
- private Container tlo;
- private JButton start;
- private JButton stop;
- private JButton koniec;
- private MojPanel cent;
- private Timer t;
- 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));
- cent = new MojPanel();
- cent.setLayout(new FlowLayout(FlowLayout.CENTER));
- tlo.add(BorderLayout.NORTH, gorny);
- tlo.add(cent);
- koniec = new JButton("Koniec");
- 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);
- t = new Timer(10, new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent ae) {
- cent.poruszkulki();
- }
- });
- }
- class KlikKoniec implements ActionListener
- {
- @Override
- public void actionPerformed(ActionEvent ae) {
- t.stop();
- dispose();
- }
- }
- class KlikStart implements ActionListener
- {
- @Override
- public void actionPerformed(ActionEvent ae) {
- t.start();
- }
- }
- class KlikStop implements ActionListener
- {
- @Override
- public void actionPerformed(ActionEvent ae) {
- t.stop();
- }
- }
- class MojPanel extends JPanel {
- Kulka kula = new Kulka(Color.WHITE, 300, 300, 100, 10, -1);
- Kulka kula2 = new Kulka(Color.RED, 300, 300, 50, 5, -6);
- public MojPanel() {
- super();
- setBackground(Color.BLACK);
- }
- @Override
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- kula2.rysuj(g);
- kula.rysuj(g);
- }
- public void poruszkulki() {
- kula2.porusz(getWidth(),getHeight());
- kula.porusz(getWidth(),getHeight());
- repaint();
- }
- }}
- class Kulka {
- private int x, y, r, vx, vy;
- Color a;
- public Kulka(Color aa, int ax, int ay, int ar, int avx, int avy) {
- a = aa;
- 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);
- }
- 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;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement