Advertisement
daniv1

Untitled

Mar 29th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.43 KB | None | 0 0
  1. import java.awt.BasicStroke;
  2. import java.awt.Color;
  3. import java.awt.Graphics;
  4. import java.awt.Graphics2D;
  5. import java.awt.event.WindowAdapter;
  6. import java.awt.event.WindowEvent;
  7. import java.awt.geom.Line2D;
  8. import javax.swing.JFrame;
  9. import javax.swing.JPanel;
  10.  
  11. public class Animation {
  12.  
  13.     JFrame frame;            
  14.     Figura figura;
  15.  
  16.     private int oneX = 30;
  17.     private int oneY = 30;
  18.  
  19.     private int dX = 3;
  20.     private int dY = 4;
  21.  
  22.     public static void main(String... args)
  23.     {
  24.         new Animation().go();
  25.     }
  26.  
  27.     void go()
  28.     {
  29.         frame = new JFrame("Test");
  30.         frame.addWindowListener(
  31.                   new WindowAdapter(){
  32.                       public void windowClosing(WindowEvent we){
  33.                       System.exit(0);
  34.                      }
  35.                     }
  36.                   );
  37.         figura = new Figura();
  38.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  39.        
  40.         //frame.setResizable(false);
  41.         frame.setSize(1920, 1080);
  42.         frame.add(figura);
  43.         frame.setVisible(true);
  44.         moveIt();
  45.         //тут можна викликати новий метод, а попередній закоментувати
  46.     }
  47.  
  48.     class Figura extends JPanel
  49.     {    
  50.         public void paintComponent(Graphics g)
  51.         {
  52.             Graphics2D graph = (Graphics2D)g;
  53.           //  g.setColor(Color.BLUE);
  54.           //  g.fillRect(0, 0, this.getWidth(), this.getHeight());
  55.          //   g.setColor(Color.RED);
  56.          //   g.fillRect(3, 3, this.getWidth() - 6, this.getHeight() - 6);
  57.          //   g.setColor(Color.WHITE);
  58.           //  g.fillRect(6, 6, this.getWidth() - 12, this.getHeight() - 12);
  59.             Line2D line = new Line2D.Double(oneX,oneY,oneX+200,oneY+200);
  60.             graph.setColor(Color.BLACK);
  61.             graph.setStroke(new BasicStroke(10.0f));
  62.             graph.draw(line);
  63.             //graph.fillRect(oneX, oneY, 150, 150);
  64.         }
  65.     }
  66.  
  67.     private void moveIt()
  68.     {
  69.         while (true)
  70.         {
  71.             oneX = oneX + dX;
  72.            oneY = oneY + dY;
  73.            
  74.             //TODO change magical number
  75.             if (oneX + dX < 0 || oneX + dX > 1700) {
  76.                 dX = -dX;
  77.             }
  78.             if (oneY + dY < 0 || oneY + dY > 900) {
  79.                 dY = -dY;
  80.             }
  81.             try
  82.             {
  83.                 Thread.sleep(10);
  84.             }
  85.             catch (Exception e)
  86.             {
  87.                 e.printStackTrace();
  88.             }
  89.             frame.repaint();
  90.         }
  91.     }
  92.    
  93.  
  94. }
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124. import java.awt.BasicStroke;
  125. import java.awt.Color;
  126. import java.awt.Graphics;
  127. import java.awt.Graphics2D;
  128. import java.awt.event.WindowAdapter;
  129. import java.awt.event.WindowEvent;
  130. import java.awt.geom.Line2D;
  131. import javax.swing.JFrame;
  132. import javax.swing.JPanel;
  133.  
  134. public class Genuk {
  135.  
  136.     JFrame frame;            
  137.     Figura figura;
  138.  
  139.     private double oneX = 500;
  140.     private double oneY = 500;
  141.  
  142.     private double dX = 1;
  143.     private double dY = 1;
  144.  
  145.     public static void main(String... args)
  146.     {
  147.         new Genuk().go();
  148.     }
  149.  
  150.     private void go()
  151.     {
  152.         frame = new JFrame("Test");
  153.         frame.addWindowListener(
  154.                   new WindowAdapter(){
  155.                       public void windowClosing(WindowEvent we){
  156.                       System.exit(0);
  157.                      }
  158.                     }
  159.                   );
  160.         figura = new Figura();
  161.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  162.        
  163.         //frame.setResizable(false);
  164.         frame.setSize(1920, 1080);
  165.         frame.add(figura);
  166.         frame.setVisible(true);
  167.         moveIt();
  168.         //тут можна викликати новий метод, а попередній закоментувати
  169.     }
  170.  
  171.     class Figura extends JPanel
  172.     {    
  173.         public void paintComponent(Graphics g)
  174.         {
  175.             Graphics2D graph = (Graphics2D)g;
  176.           //  g.setColor(Color.BLUE);
  177.           //  g.fillRect(0, 0, this.getWidth(), this.getHeight());
  178.          //   g.setColor(Color.RED);
  179.          //   g.fillRect(3, 3, this.getWidth() - 6, this.getHeight() - 6);
  180.          //   g.setColor(Color.WHITE);
  181.           //  g.fillRect(6, 6, this.getWidth() - 12, this.getHeight() - 12);
  182.             Line2D line = new Line2D.Double(oneX,Math.cos(oneY)+300,oneX+200,600*Math.cos(oneY));
  183.             graph.setColor(Color.BLACK);
  184.             graph.setStroke(new BasicStroke(10.0f));
  185.             graph.draw(line);
  186.             //graph.fillRect(oneX, oneY, 150, 150);
  187.         }
  188.     }
  189.  
  190.     private void moveIt()
  191.     {
  192.         while (true)
  193.         {
  194.             oneX = oneX + dX;
  195.            oneY = oneY + dY;
  196.            
  197.             //TODO change magical number
  198.             if (oneX + dX < 0 || oneX + dX > 1500) {
  199.                 dX = -dX;
  200.             }
  201.             if (oneY + dY < 0 || oneY + dY > 900) {
  202.                 dY = -dY;
  203.             }
  204.             try
  205.             {
  206.                 Thread.sleep(1);
  207.             }
  208.             catch (Exception e)
  209.             {
  210.                 e.printStackTrace();
  211.             }
  212.             frame.repaint();
  213.         }
  214.     }
  215.  
  216. }
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247. import java.awt.BasicStroke;
  248. import java.awt.Color;
  249. import java.awt.Graphics;
  250. import java.awt.Graphics2D;
  251. import java.awt.Shape;
  252. import java.awt.event.WindowAdapter;
  253. import java.awt.event.WindowEvent;
  254. import java.awt.geom.Line2D;
  255. import java.awt.geom.Rectangle2D;
  256. import java.awt.*;
  257.  
  258. import javax.swing.JFrame;
  259. import javax.swing.JPanel;
  260.  
  261.  
  262. public class Igor {
  263.     //public Polygon poly;
  264.     JFrame frame;            
  265.     Figura figura;
  266.  
  267.     private int oneX = 100;
  268.     private int oneY = 100;
  269.  
  270.     private int dX = 4;
  271.     private int dY = 0;
  272.  
  273.     public static void main(String... args)
  274.     {
  275.         new Igor().go();
  276.     }
  277.  
  278.     void go()
  279.     {
  280.         frame = new JFrame("Test");
  281.         frame.addWindowListener(
  282.                   new WindowAdapter(){
  283.                       public void windowClosing(WindowEvent we){
  284.                       System.exit(0);
  285.                      }
  286.                     }
  287.                   );
  288.         figura = new Figura();
  289.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  290.        
  291.         //frame.setResizable(false);
  292.         frame.setSize(1920, 1080);
  293.         frame.add(figura);
  294.         frame.setVisible(true);
  295.         moveIt();
  296.         //тут можна викликати новий метод, а попередній закоментувати
  297.     }
  298.  
  299.     class Figura extends JPanel
  300.     {    
  301.         public Polygon poly;
  302.         public Polygon poly1;
  303.         public void paintComponent(Graphics g)
  304.         {
  305.             super.paintComponent(g);
  306.             Graphics2D graph = (Graphics2D)g;
  307.              graph.setStroke(new BasicStroke(1.0f));
  308.           //  g.setColor(Color.BLUE);
  309.           //  g.fillRect(0, 0, this.getWidth(), this.getHeight());
  310.          //   g.setColor(Color.RED);
  311.          //   g.fillRect(3, 3, this.getWidth() - 6, this.getHeight() - 6);
  312.          //   g.setColor(Color.WHITE);
  313.           //  g.fillRect(6, 6, this.getWidth() - 12, this.getHeight() - 12);
  314.              
  315.             //Line2D line = new Line2D.Double(oneX,oneY,oneX+300,oneY);
  316.            
  317.              int xPoly[] = {oneX+100,oneX+200, oneX+100, oneX};
  318.              int yPoly[] = {oneY , oneY+100 , oneY+200 ,oneY+100};
  319.              int xPoly1[] = {oneX+100,oneX+150, oneX+100, oneX+50};
  320.              int yPoly1[] = {oneY , oneY+50 , oneY+100 ,oneY+50};
  321.         //  Shape square1 = new Rectangle2D.Double(oneX, oneY,200,200);
  322.             //Shape square2 = new Rectangle2D.Double(oneX, oneY,100,100);
  323.             poly = new Polygon(xPoly, yPoly, xPoly.length);
  324.             poly1 = new Polygon(xPoly1, yPoly1, xPoly1.length);
  325.             graph.drawPolygon(poly);
  326.             //poly = new Polygon(xPoly, yPoly, xPoly.length);
  327.             graph.setColor(Color.red);
  328.             graph.fillPolygon(poly);
  329.             //graph.rotate(Math.toRadians(45));
  330.             //graph.draw(square1);
  331.             //graph.fill(square1);
  332.             graph.setColor(Color.white);
  333.             graph.drawPolygon(poly1);
  334.            
  335.             graph.fillPolygon(poly1);
  336.             //graph.draw(square2);
  337.             //graph.fill(square2);
  338.            
  339.             graph.setStroke(new BasicStroke(1.0f));
  340.            
  341.           //  graph.draw(line);
  342.            
  343.             //graph.rotate(0.7);
  344.             //graph.fillRect(oneX, oneY, 150, 150);
  345.         }
  346.     }
  347.  
  348.     private void moveIt()
  349.     {
  350.         while (true)
  351.         {
  352.             oneX = oneX + dX;
  353.            oneY = oneY + dY;
  354.            
  355.             //TODO change magical number
  356.             if (oneX + dX < 0 || oneX + dX > 1500) {
  357.                 dX = -dX;
  358.             }
  359.             if (oneY + dY < 0 || oneY + dY > 1000) {
  360.                 dY = -dY;
  361.             }
  362.             try
  363.             {
  364.                 Thread.sleep(10);
  365.             }
  366.             catch (Exception e)
  367.             {
  368.                 e.printStackTrace();
  369.             }
  370.             frame.repaint();
  371.         }
  372.     }
  373.    
  374.  
  375. }
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396. import java.awt.BasicStroke;
  397. import java.awt.Color;
  398. import java.awt.Graphics;
  399. import java.awt.Graphics2D;
  400. import java.awt.event.WindowAdapter;
  401. import java.awt.event.WindowEvent;
  402. import java.awt.geom.Line2D;
  403. import javax.swing.JFrame;
  404. import javax.swing.JPanel;
  405.  
  406. public class Nabi {
  407.  
  408.     JFrame frame;            
  409.     Figura figura;
  410.  
  411.     private int oneX = 30;
  412.     private int oneY = 30;
  413.  
  414.     private int dX = 3;
  415.     private int dY = 3;
  416.  
  417.     public static void main(String... args)
  418.     {
  419.         new Nabi().go();
  420.     }
  421.  
  422.     private void go()
  423.     {
  424.         frame = new JFrame("Test");
  425.         frame.addWindowListener(
  426.                   new WindowAdapter(){
  427.                       public void windowClosing(WindowEvent we){
  428.                       System.exit(0);
  429.                      }
  430.                     }
  431.                   );
  432.         figura = new Figura();
  433.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  434.        
  435.         //frame.setResizable(false);
  436.         frame.setSize(1920, 1080);
  437.         frame.add(figura);
  438.         frame.setVisible(true);
  439.         moveIt();
  440.         //тут можна викликати новий метод, а попередній закоментувати
  441.     }
  442.  
  443.     class Figura extends JPanel
  444.     {    
  445.         public void paintComponent(Graphics g)
  446.         {
  447.             Graphics2D graph = (Graphics2D)g;
  448.           //  g.setColor(Color.BLUE);
  449.           //  g.fillRect(0, 0, this.getWidth(), this.getHeight());
  450.          //   g.setColor(Color.RED);
  451.          //   g.fillRect(3, 3, this.getWidth() - 6, this.getHeight() - 6);
  452.          //   g.setColor(Color.WHITE);
  453.           //  g.fillRect(6, 6, this.getWidth() - 12, this.getHeight() - 12);
  454.             Line2D line = new Line2D.Double(oneX,oneY,oneX+200,oneY+200);
  455.             graph.setColor(Color.BLACK);
  456.             graph.setStroke(new BasicStroke(10.0f));
  457.             graph.draw(line);
  458.             //graph.fillRect(oneX, oneY, 150, 150);
  459.         }
  460.     }
  461.  
  462.     private void moveIt()
  463.     {
  464.         while (true)
  465.         {
  466.             oneX = oneX + dX;
  467.          
  468.            
  469.             //TODO change magical number
  470.             if (oneX + dX < 0 || oneX + dX > 1700) {
  471.                 oneX = 0;
  472.                 dX = 0;
  473.             }
  474.            if (dX == 0) {
  475.                
  476.                 oneY = oneY + dY;
  477.             }
  478.             try
  479.             {
  480.                 Thread.sleep(1);
  481.             }
  482.             catch (Exception e)
  483.             {
  484.                 e.printStackTrace();
  485.             }
  486.             frame.repaint();
  487.         }
  488.     }
  489.    
  490.  
  491. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement