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 niewiemco1;
- import javax.swing.*;
- import java.util.*;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.Timer;
- import javax.swing.event.*;
- public class NieWiemCo1 {
- public static void main(String[] args) {
- // TODO code application logic here
- Okno o = new Okno("Test", 200, 100, 800, 800, Color.gray);
- }
- }
- class Okno extends JFrame
- {
- private Container tlo;
- public Okno(String tyt, int x, int y, int szer, int wys, Color t)
- {
- //budujemy okno
- super(tyt);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(x,y,szer,wys);
- tlo = getContentPane();
- tlo.setBackground(t);
- BudujUI(); //buduje user interface
- setVisible(true);
- }
- private void BudujUI()
- {
- //tworzymy panel
- final MojPanel mp = new MojPanel(Color.yellow, Color.black);
- tlo.setLayout(new BorderLayout());
- JPanel gorny = new JPanel();
- gorny.setBackground(Color.blue);
- //robimy przyciski i dodajemy listenery
- JButton Start = new JButton("Start");
- Start.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e)
- {
- mp.zegarOn();
- }
- });
- JButton Stop = new JButton("Stop");
- Stop.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e)
- {
- mp.zegarOff();
- }
- });
- JButton Koniec = new JButton("Koniec");
- Koniec.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e)
- {
- mp.zegarOff();
- dispose();
- }
- });
- JButton Dodaj = new JButton("Dodaj");
- Dodaj.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e)
- {
- mp.addBall();
- }
- });
- JButton Usun = new JButton("Usun");
- Usun.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e)
- {
- mp.deleteBall();
- }
- });
- JButton disco = new JButton("disco");
- disco.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e)
- {
- mp.discoSwitch();
- }
- });
- class SliderPanel extends JComponent {
- JSlider slider;
- JLabel label;
- SliderPanel() {
- setLayout(new BorderLayout());
- slider = new JSlider(0,70,25);
- slider.addChangeListener(new ChangeListener() {
- @Override
- public void stateChanged(ChangeEvent e)
- {
- mp.changeDelay(slider.getValue());
- }
- });
- label = new JLabel("Prędkość");
- add(label,BorderLayout.CENTER);
- add(slider,BorderLayout.SOUTH);
- }
- }
- SliderPanel sliderPanel = new SliderPanel();
- gorny.add(sliderPanel);
- gorny.add(disco);
- gorny.add(Start);
- gorny.add(Stop);
- gorny.add(Koniec);
- gorny.add(Dodaj);
- gorny.add(Usun);
- //gorny.add(slider);
- tlo.add(gorny,BorderLayout.NORTH);
- tlo.add(mp,BorderLayout.CENTER);
- }
- }
- class MojPanel extends JPanel
- {
- class Ball
- {
- public int x, y, vx, vy, r;
- Ball(int ax, int ay, int avx, int avy, int ar)
- {
- x = ax;
- y = ay;
- vx = avx;
- vy = avy;
- r = ar;
- }
- }
- ArrayList<Ball> ball = new ArrayList<Ball>();
- Color[] col = {Color.black, Color.blue, Color.cyan, Color.red, Color.magenta, Color.green, Color.orange, Color.gray};
- int ballSize;
- protected int red, blue, green, count;
- Random rand = new Random();
- boolean disco = false;
- private Timer tim;
- int wid, hei;
- private Color k_tlo, k_ramka;
- public MojPanel(Color ak_tlo, Color ak_ramka)
- {
- super();
- wid=200;
- hei=200;
- addStartingBalls();
- k_tlo = ak_tlo;
- k_ramka = ak_ramka;
- setBackground(k_tlo);
- Random rand = new Random();
- int r = rand.nextInt(12);
- //Timer z akcją co sek
- tim = new Timer(10,new ActionListener()
- {
- @Override
- public void actionPerformed(ActionEvent e)
- {
- //sprawdzenie czy nie ma sciany ani innych obiektow
- checkMove();
- checkObjects();
- //rysowanie w innym miejscu
- repaint();
- }
- });
- }
- @Override
- public void paintComponent(Graphics g)
- {
- super.paintComponent(g);
- wid = getWidth();
- hei = getHeight();
- ballSize = ball.size();
- int j;
- //g.setColor(Color.black);
- // g.drawString(""+ballSize,100,100);
- if (disco) {
- count++;
- for(int i = 0; i< ballSize; i++) {
- j=(i+(count/7))%col.length;
- g.setColor(col[j]);
- g.fillOval(ball.get(i).x-ball.get(i).r,ball.get(i).y-ball.get(i).r,2*ball.get(i).r,2*ball.get(i).r);
- //setBackground(col[j]);
- }
- }
- else {
- for(int i = 0; i< ballSize; i++) {
- j=(i+(count/7))%col.length;
- g.setColor(col[j]);
- g.fillOval(ball.get(i).x-ball.get(i).r,ball.get(i).y-ball.get(i).r,2*ball.get(i).r,2*ball.get(i).r);}
- }
- }
- public void addStartingBalls()
- {
- addBall(150, 150, 1, 1, 30);
- addBall(250, 250, 2, 1, 40);
- addBall(350, 150, 1, 2, 30);
- addBall(150, 350, 2, 2, 50);
- addBall(200, 200, 1, 1, 20);
- addBall(300, 200, 1, 1, 25);
- addBall();
- addBall();
- addBall();
- }
- public void addBall(int x, int y, int vx, int vy, int r) {
- ball.add(new Ball(x, y, vx, vy, r));
- }
- public void addBall() {
- if(ball.size()<20){
- Random rand = new Random();
- int r = rand.nextInt(31)+20;
- int x = rand.nextInt(wid-r)+r;
- int y = rand.nextInt(hei-r)+r;
- int vx = rand.nextInt(2)+1;
- int yx = rand.nextInt(2)+1;
- addBall(x,y,vx,yx,r);
- int lastBall = ball.size()-1;
- if (checkCollision(lastBall)||checkBorderXCollision(lastBall)||checkBorderYCollision(lastBall)) {
- deleteBall();
- addBall();
- }
- }
- else
- JOptionPane.showMessageDialog(null,"Ograniczenie do 20 kulek","Ogranieczenie",JOptionPane.ERROR_MESSAGE);
- }
- public void deleteBall() {
- if(ball.size()==0)
- JOptionPane.showMessageDialog(null,"Nie ma kulek do usuniecia","Brak kul",JOptionPane.ERROR_MESSAGE);
- else
- ball.remove(ball.size()-1);
- }
- public void checkMove()
- {
- //sprawdzanie kolizjii i zmiana wektorow ze scianami
- for(int i = 0; i < ball.size(); i++)
- {
- if(checkBorderXCollision(i))
- {
- ball.get(i).vx = -ball.get(i).vx;
- }
- if(checkBorderYCollision(i))
- {
- ball.get(i).vy = -ball.get(i).vy;
- }
- moveBalls(i);
- }
- }
- private boolean checkBorderXCollision (int i) {
- if ( ball.get(i).x >= (wid - ball.get(i).r) || ball.get(i).x <= ball.get(i).r) return true;
- else return false;
- }
- private boolean checkBorderYCollision (int i) {
- if( ball.get(i).y >= (hei - ball.get(i).r) || ball.get(i).y <= ball.get(i).r ) return true;
- else return false;
- }
- public void checkObjects()
- {
- //sprawdza kolizje z innymi objektami
- for(int j = 0; j < ball.size(); j++)
- {
- if (checkCollision(j)) {
- ball.get(j).vx = -ball.get(j).vx;
- ball.get(j).vy = -ball.get(j).vy;
- }
- }
- }
- public boolean checkCollision(int j) {
- for(int k = 0; k < ball.size(); k++)
- {
- if (k==j) continue;
- if(pythagoras(ball.get(j).x, ball.get(j).y, ball.get(k).x, ball.get(k).y, ball.get(k).r, ball.get(j).r))
- {
- return true;
- }
- }
- return false;
- }
- public boolean pythagoras(int ax, int ay, int bx, int by, int ar, int br)
- {
- //z pitagorasa
- if(Math.sqrt((bx-ax)*(bx-ax)+(by-ay)*(by-ay)) <= ar+ br)
- {return true;}
- else
- {return false;}
- }
- private void moveBalls(int i) {
- ball.get(i).x += ball.get(i).vx;
- ball.get(i).y += ball.get(i).vy;
- }
- public void zegarOn()
- {
- tim.start();
- }
- public void zegarOff()
- {
- tim.stop();
- }
- public void changeDelay(int speed)
- {
- int delay = 70;
- tim.setDelay(delay-speed);
- }
- public void discoSwitch() {
- disco = !disco;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement