Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lab8;
- import javax.swing.*;
- import javax.swing.Timer;
- import javax.swing.event.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.*;
- class Okno extends JFrame{
- private Container tlo;
- private MojPanel mp = new MojPanel();
- public Okno(String tyt, int x0, int y0, int szer, int wys){
- super(tyt);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(x0,y0,szer,wys);
- tlo=getContentPane();
- budujUI();
- setVisible(true);
- }
- private void budujUI(){
- tlo.setLayout(new BorderLayout());
- JPanel gorny = new JPanel();
- tlo.add(gorny, BorderLayout.NORTH);
- gorny.setBackground(Color.YELLOW);
- tlo.add(mp, BorderLayout.CENTER);
- JButton koniec = new JButton("Koniec");
- gorny.add(koniec);
- koniec.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e)
- {
- dispose();
- }
- });
- JButton odczyt = new JButton("Odczytaj");
- gorny.add(odczyt);
- odczyt.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e)
- {
- Random r = new Random();
- Kulka k = new Kulka(250+r.nextInt(100),250+r.nextInt(100),10,new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256)),r.nextInt(21)-10,r.nextInt(21)-10);
- figury.add(k);
- }
- });
- }
- }
- class MojPanel extends JPanel{
- Timer t;
- private ArrayList <Figura> figury = new ArrayList<>();
- public MojPanel(){
- super();
- Random r = new Random();
- for (int i = 0; i<=0;i++){
- Kulka k = new Kulka(250+r.nextInt(100),250+r.nextInt(100),10,new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256)),r.nextInt(21)-10,r.nextInt(21)-10);
- figury.add(k);
- Kwadrat t = new Kwadrat(250+r.nextInt(100),250+r.nextInt(100),20,new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256)),r.nextInt(21)-10,r.nextInt(21)-10);
- figury.add(t);
- Trojkat v = new Trojkat(250+r.nextInt(100),250+r.nextInt(100),20,new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256)),r.nextInt(21)-10,r.nextInt(21)-10);
- figury.add(v);
- poly h = new poly(250+r.nextInt(100),250+r.nextInt(100),20,new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256)),r.nextInt(21)-10,r.nextInt(21)-10);
- figury.add(h);
- }
- setBackground(Color.WHITE);
- t = new Timer(20, new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e){
- for(Figura f: figury){
- f.rusz(getWidth(),getHeight());
- }
- repaint();
- }
- });
- t.start();
- }
- @Override
- public void paintComponent(Graphics g){
- super.paintComponent(g);
- for(Figura f: figury){
- f.rysuj(g);
- }
- }
- }
- abstract class Figura{
- protected int x,y,r,vx,vy;
- protected Color k;
- public Figura(int xx,int yy,int rr,Color kk, int vxvx,int vyvy){
- x = xx;
- y = yy;
- r = rr;
- k = kk;
- vx = vxvx;
- vy = vyvy;
- }
- abstract public void rysuj(Graphics g);
- public void rusz(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(int xx,int yy,int rr,Color kk, int vxvx,int vyvy){
- super(xx,yy,rr,kk,vxvx,vyvy);
- }
- @Override
- public void rysuj(Graphics g){
- g.setColor(k);
- g.fillOval(x-r,y-r,2*r,2*r);
- }
- }
- public class Lab8 {
- public static void main(String[] args) {
- Okno o = new Okno("Kulki",100,100,1700,1100);
- private ArrayList <Figura> figury = new ArrayList<>();
- }
- }
- class Kwadrat extends Figura{
- public Kwadrat(int xx,int yy,int rr,Color kk, int vxvx,int vyvy){
- super(xx,yy,rr,kk,vxvx,vyvy);
- }
- @Override
- public void rysuj(Graphics g){
- g.setColor(k);
- g.fillRect(x-r,y-r,2*r,2*r);
- }
- }
- class Trojkat extends Figura{
- public Trojkat(int xx,int yy,int rr,Color kk, int vxvx,int vyvy){
- super(xx,yy,rr,kk,vxvx,vyvy);
- }
- @Override
- public void rysuj(Graphics g){
- g.setColor(k);
- int[] a={x,(x+r),x,(x-r)};
- int[] b={(y-r),y,(y+r),y};
- g.fillPolygon(a,b,3);
- }
- }
- class poly extends Figura{
- public poly(int xx,int yy,int rr,Color kk, int vxvx,int vyvy){
- super(xx,yy,rr,kk,vxvx,vyvy);
- }
- @Override
- public void rysuj(Graphics g){
- g.setColor(k);
- int[] a={x,(x+r),x,(x-r)};
- int[] b={(y-r),y,(y+r),y};
- g.fillPolygon(a,b,4);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement