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 Igor {
- //public Polygon poly;
- JFrame frame;
- Figura figura;
- private int oneX = 100;
- private int oneY = 200;
- private int dX = 4;
- private int dY = 4;
- public static void main(String... args)
- {
- new Igor().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);
- // moveIt();
- // moveIt_1();
- moveIt_2();
- //тут можна викликати новий метод, а попередній закоментувати
- }
- 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));
- // g.setColor(Color.BLUE);
- // g.fillRect(0, 0, this.getWidth(), this.getHeight());
- // g.setColor(Color.RED);
- // g.fillRect(3, 3, this.getWidth() - 6, this.getHeight() - 6);
- // g.setColor(Color.WHITE);
- // g.fillRect(6, 6, this.getWidth() - 12, this.getHeight() - 12);
- //Line2D line = new Line2D.Double(oneX,oneY,oneX+300,oneY);
- int xPoly[] = {oneX+100,oneX+200, oneX+100, oneX};
- int yPoly[] = {oneY , oneY+100 , oneY+200 ,oneY+100};
- int xPoly1[] = {oneX+100,oneX+150, oneX+100, oneX+50};
- int yPoly1[] = {oneY , oneY+50 , oneY+100 ,oneY+50};
- // 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);
- poly1 = new Polygon(xPoly1, yPoly1, xPoly1.length);
- graph.drawPolygon(poly);
- //poly = new Polygon(xPoly, yPoly, xPoly.length);
- graph.setColor(Color.red);
- graph.fillPolygon(poly);
- //graph.rotate(Math.toRadians(45));
- //graph.draw(square1);
- //graph.fill(square1);
- graph.setColor(Color.white);
- graph.drawPolygon(poly1);
- graph.fillPolygon(poly1);
- //graph.draw(square2);
- //graph.fill(square2);
- // graph.setStroke(new BasicStroke(1.0f));
- // graph.draw(line);
- //graph.rotate(0.7);
- //graph.fillRect(oneX, oneY, 150, 150);
- }
- }
- 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 moveIt_2()
- {
- while (true)
- {
- // oneX = (int)(200*Math.cos(Math.toRadians(oneX ))) + dX;
- oneX +=dX;
- oneY = (int)(200*Math.cos(Math.toRadians(oneY))) + dY + 300;
- // (oneX,Math.cos(oneY)+300,oneX+200,600*Math.cos(oneY));
- //TODO change magical number
- if (oneX + dX < 0 || oneX + dX > 1500) {
- dX = -dX;
- }
- if (oneY + dY < 0 || oneY + dY > 900) {
- dY = -dY;
- }
- try
- {
- Thread.sleep(60);
- }
- 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 > 1400) {
- dX = -dX;
- }
- //if (oneY + dY < 0 || oneY + dY > 1000) {
- // dY = -dY;
- //}
- try
- {
- Thread.sleep(5);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- frame.repaint();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement