Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BasicStroke;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Shape;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import java.awt.geom.Line2D;
- import java.awt.geom.Rectangle2D;
- import java.awt.*;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- public class Animation {
- JFrame frame;
- Figura figura;
- private int oneX = 100;
- private int oneY = 100;
- private int dX = 4;
- private int dY = 4;
- public static void main(String[] args)
- {
- new Animation().go();
- }
- void go()
- {
- frame = new JFrame("Test");
- frame.addWindowListener(
- new WindowAdapter(){
- public void windowClosing(WindowEvent we){
- System.exit(0);
- }
- }
- );
- figura = new Figura();
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- //frame.setResizable(false);
- frame.setSize(1920, 1080);
- frame.add(figura);
- frame.setVisible(true);
- moveIt2(); //- створений мною метод
- // moveIt_1();
- //moveIt();
- //тут можна викликати новий метод, а попередній закоментувати
- }
- class Figura extends JPanel
- {
- public Polygon poly;
- public Polygon poly1;
- public void paintComponent(Graphics g)
- {
- super.paintComponent(g);
- Graphics2D graph = (Graphics2D)g;
- graph.setStroke(new BasicStroke(1.0f));
- int xPoly[] = {oneX ,oneX + 50, oneX+100, oneX+ 100 , oneX + 50 , oneX};
- int yPoly[] = {oneY , oneY+25 , oneY ,oneY +100 , oneY + 125 , oneY +100};
- // Shape square1 = new Rectangle2D.Double(oneX, oneY,200,200);
- //Shape square2 = new Rectangle2D.Double(oneX, oneY,100,100);
- poly = new Polygon(xPoly, yPoly, xPoly.length);
- graph.setColor(Color.green);
- graph.fillPolygon(poly);
- }
- }
- private void moveIt_1()
- {
- while (true)
- {
- oneX = oneX + dX;
- //TODO change magical number
- if (oneX + dX < 0 || oneX + dX > 1700) {
- oneX = 0;
- dX = 0;
- }
- if (dX == 0) {
- oneY = oneY + dY;
- }
- try
- {
- Thread.sleep(1);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- frame.repaint();
- }
- }
- private void moveIt2()
- {
- while (true)
- {
- oneX = oneX + dX;
- //oneY = oneY + dY;
- //TODO change magical number
- if (oneX + dX < 0 || oneX + dX > 1400) {
- dX = -dX;
- }
- //if (oneY + dY < 0 || oneY + dY > 1000) {
- // dY = -dY;
- //}
- try
- {
- Thread.sleep(5);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- frame.repaint();
- }
- }
- private void moveIt()
- {
- while (true)
- {
- oneX = oneX + dX;
- oneY = oneY + dY;
- //TODO change magical number
- if (oneX + dX < 0 || oneX + dX > 280) {
- dX = -dX;
- }
- if (oneY + dY < 0 || oneY + dY > 260) {
- dY = -dY;
- }
- try
- {
- Thread.sleep(10);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- frame.repaint();
- }
- }
- //тут можна дописати новий метод
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement