Advertisement
Guest User

Java

a guest
Dec 9th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.30 KB | None | 0 0
  1. import javax.swing.*;
  2. import javax.swing.Timer;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.util.ArrayList;
  6. import java.util.*;
  7. import java.io.*;
  8.  
  9. public class Laborki {
  10.     public static void main(String[] args) {
  11.         new Okno("Program", 100, 100, 500, 500);
  12.     }  
  13. }
  14.  
  15. class Okno extends JFrame {
  16.     private Container tlo;
  17.     private MojPanel mp;
  18.     public Okno(String tytul, int x0, int y0, int szer, int wys) {
  19.         super(tytul);
  20.         setBounds(x0, y0, szer, wys);
  21.         tlo=getContentPane();
  22.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  23.         budujUI();
  24.         setVisible(true);
  25.     }
  26.     private void budujUI(){
  27.         tlo.setLayout(new BorderLayout());
  28.         JPanel g = new JPanel();
  29.         g.setBackground(Color.YELLOW);
  30.         tlo.add(g, BorderLayout.NORTH);
  31.         JButton b2 = new JButton("Odczytaj");
  32.         JButton b1 = new JButton("Close");
  33.         b1.addActionListener(new ActionListener(){
  34.             @Override
  35.             public void actionPerformed(ActionEvent e) {
  36.                 mp.stopZegar();
  37.                 dispose();
  38.             }
  39.         });
  40.         mp=new MojPanel();
  41.         b2.addActionListener(new myAction(mp));
  42.         g.add(b2);
  43.         g.add(b1);
  44.         tlo.add(mp, BorderLayout.CENTER);
  45.     }
  46. }
  47.  
  48. class MojPanel extends JPanel {
  49.     private ArrayList<Figura> figury = new ArrayList<>();
  50.     int x = 0;
  51.     private Timer t;
  52.     public MojPanel() {
  53.         super();
  54.         setBackground(Color.WHITE);
  55.         }
  56.     public void ruszaaaj() {
  57.         t=new Timer(20, new ActionListener() {
  58.             @Override
  59.             public void actionPerformed(ActionEvent e) {
  60.                 int a=getWidth();
  61.                 int b=getHeight();
  62.                 for(Figura k: figury) {
  63.                     k.rusz(a, b);
  64.                 }
  65.                 repaint();
  66.             }
  67.         });
  68.         t.start();
  69.     }
  70.     public void stopZegar() {
  71.         t.stop();
  72.     }
  73.     public void dodajFigure(Figura f) {
  74.         figury.add(f);
  75.     }
  76.     @Override
  77.     public void paintComponent(Graphics g) {
  78.         super.paintComponent(g);
  79.         for(Figura k: figury) {
  80.             k.rysuj(g);
  81.         }
  82.     }
  83. }
  84. abstract class Figura {
  85.     public int x, y, r, vx, vy;
  86.     public Color k;
  87.     public Figura(int x, int y, int r, Color k, int vx, int vy) {
  88.         this.x=x;
  89.         this.y=y;
  90.         this.r=r;
  91.         this.k=k;
  92.         this.vx=vx;
  93.         this.vy=vy;
  94.     }
  95.     public void rusz(int a, int b) {
  96.         if(x<=r || x>=a-r) vx=-vx;
  97.         if(y<=r || y>=b-r) vy=-vy;
  98.         x+=vx;
  99.         y+=vy;
  100.     }
  101.     abstract void rysuj(Graphics g);
  102. }
  103. //-----------------------------------------------------------------------------
  104. class Kulka extends Figura{
  105.     public Kulka(int x, int y, int r, Color k, int vx, int vy) {
  106.         super(x, y, r, k, vx, vy);
  107.     }
  108.     @Override
  109.     public void rysuj(Graphics g) {
  110.         g.setColor(k);
  111.         g.fillOval(x-r, y-r, 2*r, 2*r);
  112.     }
  113. }
  114. //-----------------------------------------------------------------------------
  115. class Kwadrat extends Figura{
  116.     public Kwadrat(int x, int y, int r, Color k, int vx, int vy) {
  117.         super(x, y, r, k, vx, vy);
  118.     }
  119.     @Override
  120.     public void rysuj(Graphics g) {
  121.         g.setColor(k);
  122.         g.fillRect(x-r, y-r, 2*r, 2*r);
  123.     }
  124. }
  125. //-----------------------------------------------------------------------------
  126. class Diament extends Figura{
  127.     public Diament(int x, int y, int r, Color k, int vx, int vy) {
  128.         super(x, y, r, k, vx, vy);
  129.     }
  130.     @Override
  131.     public void rysuj(Graphics g) {
  132.         g.setColor(k);
  133.         int[] ax={x-r, x, x+r, x};
  134.         int[] ay={y, y+r, y, y-r};
  135.         g.fillPolygon(ax, ay, 4);
  136.     }
  137. }
  138. //------------------------------------------------------------------------------
  139. class myAction implements ActionListener {
  140.     private MojPanel m;
  141.     public myAction(MojPanel m) {
  142.         super();
  143.         this.m=m;
  144.     }
  145.     @Override
  146.     public void actionPerformed(ActionEvent e) {
  147.         try {
  148.             //Scanner s = new Scanner(new File("C:\\Users\\Student.PC35\\eclipse-workspace\\Laborki10\\src\\dane.txt"));
  149.             Scanner s = new Scanner(new File("C:\\Users\\angel\\eclipse-workspace\\Laborki\\src\\dane.txt"));
  150.             int x, y, r, vx, vy, kr, g, b;
  151.             String txt;
  152.             while(s.hasNext()) {
  153.                 txt=s.next();
  154.                 x=s.nextInt();
  155.                 y=s.nextInt();
  156.                 r=s.nextInt();
  157.                 kr=s.nextInt();
  158.                 g=s.nextInt();
  159.                 b=s.nextInt();
  160.                 vx=s.nextInt();
  161.                 vy=s.nextInt();
  162.                 Figura f;
  163.                 switch(txt) {
  164.                 case "Kulka": f = new Kulka(x,y,r,new Color(kr,g,b),vx,vy); m.dodajFigure(f); break;
  165.                 case "Kwadrat": f = new Kwadrat(x,y,r,new Color(kr,g,b),vx,vy); m.dodajFigure(f); break;
  166.                 case "Diament": f = new Diament(x,y,r,new Color(kr,g,b),vx,vy); m.dodajFigure(f); break;
  167.                 }
  168.             }
  169.             m.ruszaaaj();
  170.             s.close();
  171.         }
  172.         catch(Exception ex) {
  173.             System.out.println(ex);
  174.         }
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement