Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import javax.swing.Timer;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.ArrayList;
- import java.util.*;
- import java.io.*;
- public class Laborki {
- public static void main(String[] args) {
- new Okno("Program", 100, 100, 500, 500);
- }
- }
- class Okno extends JFrame {
- private Container tlo;
- private MojPanel mp;
- public Okno(String tytul, int x0, int y0, int szer, int wys) {
- super(tytul);
- setBounds(x0, y0, szer, wys);
- tlo=getContentPane();
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- budujUI();
- setVisible(true);
- }
- private void budujUI(){
- tlo.setLayout(new BorderLayout());
- JPanel g = new JPanel();
- g.setBackground(Color.YELLOW);
- tlo.add(g, BorderLayout.NORTH);
- JButton b2 = new JButton("Odczytaj");
- JButton b1 = new JButton("Close");
- b1.addActionListener(new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent e) {
- mp.stopZegar();
- dispose();
- }
- });
- mp=new MojPanel();
- b2.addActionListener(new myAction(mp));
- g.add(b2);
- g.add(b1);
- tlo.add(mp, BorderLayout.CENTER);
- }
- }
- class MojPanel extends JPanel {
- private ArrayList<Figura> figury = new ArrayList<>();
- int x = 0;
- private Timer t;
- public MojPanel() {
- super();
- setBackground(Color.WHITE);
- }
- public void ruszaaaj() {
- t=new Timer(20, new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- int a=getWidth();
- int b=getHeight();
- for(Figura k: figury) {
- k.rusz(a, b);
- }
- repaint();
- }
- });
- t.start();
- }
- public void stopZegar() {
- t.stop();
- }
- public void dodajFigure(Figura f) {
- figury.add(f);
- }
- @Override
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- for(Figura k: figury) {
- k.rysuj(g);
- }
- }
- }
- abstract class Figura {
- public int x, y, r, vx, vy;
- public Color k;
- public Figura(int x, int y, int r, Color k, int vx, int vy) {
- this.x=x;
- this.y=y;
- this.r=r;
- this.k=k;
- this.vx=vx;
- this.vy=vy;
- }
- public void rusz(int a, int b) {
- if(x<=r || x>=a-r) vx=-vx;
- if(y<=r || y>=b-r) vy=-vy;
- x+=vx;
- y+=vy;
- }
- abstract void rysuj(Graphics g);
- }
- //-----------------------------------------------------------------------------
- class Kulka extends Figura{
- public Kulka(int x, int y, int r, Color k, int vx, int vy) {
- super(x, y, r, k, vx, vy);
- }
- @Override
- public void rysuj(Graphics g) {
- g.setColor(k);
- g.fillOval(x-r, y-r, 2*r, 2*r);
- }
- }
- //-----------------------------------------------------------------------------
- class Kwadrat extends Figura{
- public Kwadrat(int x, int y, int r, Color k, int vx, int vy) {
- super(x, y, r, k, vx, vy);
- }
- @Override
- public void rysuj(Graphics g) {
- g.setColor(k);
- g.fillRect(x-r, y-r, 2*r, 2*r);
- }
- }
- //-----------------------------------------------------------------------------
- class Diament extends Figura{
- public Diament(int x, int y, int r, Color k, int vx, int vy) {
- super(x, y, r, k, vx, vy);
- }
- @Override
- public void rysuj(Graphics g) {
- g.setColor(k);
- int[] ax={x-r, x, x+r, x};
- int[] ay={y, y+r, y, y-r};
- g.fillPolygon(ax, ay, 4);
- }
- }
- //------------------------------------------------------------------------------
- class myAction implements ActionListener {
- private MojPanel m;
- public myAction(MojPanel m) {
- super();
- this.m=m;
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- try {
- //Scanner s = new Scanner(new File("C:\\Users\\Student.PC35\\eclipse-workspace\\Laborki10\\src\\dane.txt"));
- Scanner s = new Scanner(new File("C:\\Users\\angel\\eclipse-workspace\\Laborki\\src\\dane.txt"));
- int x, y, r, vx, vy, kr, g, b;
- String txt;
- while(s.hasNext()) {
- txt=s.next();
- x=s.nextInt();
- y=s.nextInt();
- r=s.nextInt();
- kr=s.nextInt();
- g=s.nextInt();
- b=s.nextInt();
- vx=s.nextInt();
- vy=s.nextInt();
- Figura f;
- switch(txt) {
- case "Kulka": f = new Kulka(x,y,r,new Color(kr,g,b),vx,vy); m.dodajFigure(f); break;
- case "Kwadrat": f = new Kwadrat(x,y,r,new Color(kr,g,b),vx,vy); m.dodajFigure(f); break;
- case "Diament": f = new Diament(x,y,r,new Color(kr,g,b),vx,vy); m.dodajFigure(f); break;
- }
- }
- m.ruszaaaj();
- s.close();
- }
- catch(Exception ex) {
- System.out.println(ex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement