Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package testquakea;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import javax.swing.Timer;
- import java.awt.event.ActionListener;
- import java.util.ArrayList;
- import javax.swing.*;
- import java.util.*;
- /**
- *
- * @author Student
- */
- public class Testquakea {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- Okno a1 = new Okno("Quake", 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.RED);
- 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 ActionListener() {
- @Override
- public void actionPerformed(ActionEvent ae) {
- t.stop();
- dispose();
- }
- });
- start.addActionListener(new ActionListener()
- {
- @Override
- public void actionPerformed(ActionEvent ae) {
- t.start();
- }
- });
- stop.addActionListener(new ActionListener()
- {
- @Override
- public void actionPerformed(ActionEvent ae) {
- t.stop();
- }
- });
- gorny.add(koniec);
- gorny.add(start);
- gorny.add(stop);
- t = new Timer(10, new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent ae) {
- cent.poruszkulki();
- }
- });
- }
- }
- //------------------------------------------------------------------------------
- class MojPanel extends JPanel {
- ArrayList<Kulka> lista1=new ArrayList<>();
- public MojPanel() {
- super();
- setBackground(Color.BLACK);
- Random r = new Random();
- for(int i=0;i<50;i++)
- {
- lista1.add( new Kulka( new Color( r.nextInt(256), r.nextInt(256), r.nextInt(256)),
- 100, 100, r.nextInt(30),
- r.nextInt(11) - 5, r.nextInt(11) - 5));
- }
- }
- @Override
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- for(Kulka x:lista1)
- x.rysuj(g);
- /*kulka1.rysuj(g);
- kulka2.rysuj(g);
- kulka3.rysuj(g);
- kulka4.rysuj(g);*/
- }
- public void poruszkulki() {
- for(Kulka x:lista1)
- {
- x.porusz(getWidth(),getHeight());
- }
- repaint();
- /*kulka1.porusz(getWidth(),getHeight());
- kulka2.porusz(getWidth(),getHeight());
- kulka3.porusz(getWidth(),getHeight());
- kulka4.porusz(getWidth(),getHeight());
- repaint();*/
- }
- }
- //------------------------------------------------------------------------------
- class Kulka {
- private int x, y, r, vx, vy;
- Color a, b;
- 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