Advertisement
Guest User

Untitled

a guest
Nov 16th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.14 KB | None | 0 0
  1. import javax.swing.*;
  2. import javax.swing.event.*;
  3. import javax.swing.text.JTextComponent;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. import java.awt.image.BufferedImage;
  7. import java.util.Random;
  8. import java.lang.Math;
  9. import java.lang.Object;
  10. import java.lang.Object.*;
  11. import java.io.File;
  12. import javax.imageio.ImageIO;
  13. import javax.imageio.ImageIO.*;
  14. import java.io.IOException;
  15.  
  16.  
  17. public class Board extends JPanel implements ChangeListener, ActionListener{
  18.     JSlider velocitySlider;
  19.     JSlider angleSlider;
  20.     JTextField timeField;
  21.     JLabel label1;
  22.     JLabel labelVelocity;
  23.     JLabel labelAngle;
  24.     JLabel labelTime;
  25.    
  26.     JPanel toolbar;
  27.     JRadioButton[] JRBColors = new JRadioButton[6];
  28.     JRadioButton[] JRBExplosions = new JRadioButton[6];
  29.     BufferedImage img;
  30.    
  31.    
  32.     Random ran= new Random();
  33.     Canvas x=new Canvas();
  34.    
  35.     Color[] colorsColor =  {Color.red,Color.orange,Color.yellow,Color.blue,Color.green,new Color(255,255,255)};
  36.     String[] colorsString = {"red","orange","yellow","blue","green","random"};
  37.     String[] explosionsString = {"Concentric Circles","Real Firework","Line Firework", "Line Circles", "Circles","Image"};
  38.     @Override
  39.     public void stateChanged(ChangeEvent e) { //for velocity and angle
  40.         if(e.getSource()==velocitySlider) {
  41.             x.setVelocity(velocitySlider.getValue());
  42.             labelVelocity.setText("Current velocity is " + velocitySlider.getValue() +" m/s.");
  43.             System.out.println("velocity = " + x.getVelocity() +", angle = " + x.getTheta() +", time = " + x.getTime());
  44.         }
  45.         if(e.getSource()==angleSlider) {
  46.             x.setTheta(angleSlider.getValue());
  47.             labelAngle.setText("Current angle is " + angleSlider.getValue() +"°.");
  48. //              System.out.println("velocity = " + x.getVelocity() +", angle = " + x.getTheta() +", time = " + x.getTime());
  49.         }
  50.      
  51.         repaint();
  52.     }
  53.     @Override
  54.     public void actionPerformed(ActionEvent e) { //for time
  55.         if(e.getSource() == timeField) {   
  56.             String timeString = ((JTextComponent) e.getSource()).getText();
  57.             int timeInt = Integer.parseInt(timeString);
  58.             x.setTime(timeInt);
  59.             labelTime.setText("Chosen time is " + timeInt +" s.");
  60.  
  61.             //System.out.println("velocity = " + x.getVelocity() +", angle = " + x.getTheta() +", time = " + x.getTime());
  62.         }
  63.         else {
  64.             for(int i=0;i<6;i++) {
  65.                 if(e.getSource() == JRBColors[i]) {
  66.                     if(i!=5)
  67.                         x.setColor(colorsColor[i]);
  68.                     else {
  69.                         int redRandom,greenRandom,blueRandom;
  70.                         redRandom = ran.nextInt(256);
  71.                         greenRandom = ran.nextInt(256);
  72.                         blueRandom = ran.nextInt(256);
  73.                         x.setColor(new Color(redRandom,greenRandom,blueRandom));
  74.                     }
  75.                 }
  76.             }
  77.             for(int i=0;i<6;i++) {
  78.                 if(e.getSource() == JRBExplosions[i])
  79.                     x.setTypeOfExplosion(i);
  80.             }
  81.            
  82.         }
  83.            
  84.         repaint();
  85.     }
  86. ////    @Override
  87. ////    public void itemStateChanged(ItemEvent e) { //for color choice
  88. ////        Color currColor;
  89. //      System.out.println(e.getSource());
  90. ////        for(int i=0;i<6;i++) {
  91. ////            if(e.getSource()==JRBColors[i])
  92. ////                System.out.println(colorsString[i]);
  93. ////               
  94. ////        }
  95. ////       
  96. //  }
  97.    
  98.     public void paintComponent(Graphics g) {
  99.         super.paintComponent(g);
  100.         for(int count = 0; count < 100; count++) {
  101.             int xStar = ran.nextInt((int) (Math.abs(getWidth())))+1;
  102.             int yStar = ran.nextInt((int) (Math.abs(getHeight()/4)))+1;
  103.             g.setColor(new Color(255,248,138));
  104.             Font font = new Font("Arial", Font.BOLD, 16);
  105.             g.setFont(font);
  106.             g.drawString("*", xStar, yStar);
  107.             if(count>60)
  108.                 g.drawString("*", xStar, yStar+50);
  109.             if(count>80)
  110.                 g.drawString("*", xStar, yStar+80);
  111.             if(count>90)
  112.                 g.drawString("*", xStar, yStar+100);
  113.            
  114.         }
  115.         double x1=0;
  116.         double y1=0;
  117.         double x2=0;
  118.         double y2=getHeight(); 
  119.         g.setColor(x.getColor());
  120.         for(double i=0;i<=x.getTime();i+=0.69) {   
  121.             double angle = Math.toRadians(x.getTheta());
  122.             x2 = x.getVelocity()*Math.cos(angle)*i;
  123.             y2 = (x.getVelocity()*Math.sin(angle)*i - 5*i*i);
  124.            
  125.             y2 = getHeight()-y2;
  126.             Font font = new Font("Arial", Font.BOLD, 12);
  127. //          g.setFont(font);
  128.             if(i>=0.69)
  129.                 g.drawLine((int)x1,(int) y1,(int)x2,(int)y2);
  130.             x1=x2;
  131.             y1=y2;
  132.             if(x.getTime()-i<=0.69 && (x1!=0 && y1!=0)) {
  133.                                    
  134.                 switch(x.getTypeOfExplosion()) {
  135.                     case 0:
  136.                         double x11 = x2;
  137.                         double y11 = y2;
  138.                         for(int count=0; count<40; count++){
  139.                             double ranAngle = ran.nextInt(628) / 100.0;
  140.                             double x22=0;
  141.                             double y22=0;
  142.                            
  143.                             System.out.println(x11 + " "+ y11);
  144.                             for(double z=0;z<=x.getTime()/4;z+=0.69) { 
  145.                                
  146.                                 x22 = x.getVelocity()*Math.cos(ranAngle)*z + x2;
  147.                                 y22 = -(x.getVelocity()*Math.sin(ranAngle)*z- 5*z*z) + y2;
  148.                                
  149.                                
  150.                                
  151.                                 //y22 = getHeight()-y22;
  152.                                
  153. //                              if(z==0)
  154. //                                  g.drawLine((int)x2,(int) y2,(int) x22,(int) y22);
  155.                                 if(x.getTime() - z > 0.69 && z>0)
  156.                                     g.drawLine((int)x11,(int) y11,(int)x22,(int)y22);
  157.                                    
  158.                                
  159.                                 x11=x22;
  160.                                 y11=y22;
  161.                             //g.fillOval((int) (x2-k/2),(int)(y2-k/2),k,k);
  162.                             }
  163.                             for(int count1 = 0; count1 < 300; count1++) { //we create 25 lines
  164.                                
  165.                                 int ranBetween1 = ran.nextInt(2)+1; //either 1 or 2
  166.                                 int ranBetween2 = ran.nextInt(2)+1; //either 1 or 2
  167.                                
  168.                                 int xEnding = (int)x22 + (int)(Math.pow(-1, ranBetween1)*ran.nextInt(50));
  169.                                 int yEnding = (int)y22 + (int)(Math.pow(-1, ranBetween2)*ran.nextInt(50));
  170.                                
  171.                                 int r=ran.nextInt(256);
  172.                                 int b=ran.nextInt(256);
  173.                                 int n=ran.nextInt(256);
  174.                                 Color randomColor=new Color(r,n,b);
  175.                                 g.setColor(randomColor);
  176.                                 g.drawLine((int)x22,(int) y22, xEnding, yEnding);
  177.                             }
  178.                         }  
  179.                         break;
  180.                     case 1:
  181.                         for(int count = 0; count < 150; count++) { //we create 25 lines
  182.                            
  183.                             int ranBetween1 = ran.nextInt(2)+1; //either 1 or 2
  184.                             int ranBetween2 = ran.nextInt(2)+1; //either 1 or 2
  185.                            
  186.                             int xEnding = (int)x2 + (int)(Math.pow(-1, ranBetween1)*ran.nextInt(100));
  187.                             int yEnding = (int)y2 + (int)(Math.pow(-1, ranBetween2)*ran.nextInt(100));
  188.                             int xEnding1 = (int)x2 + (int)(Math.pow(-1, ranBetween1)*ran.nextInt(200));
  189.                             int yEnding1 = (int)y2 + (int)(Math.pow(-1, ranBetween2)*ran.nextInt(200));
  190.                            
  191.                            
  192.                             int r=ran.nextInt(256);
  193.                             int b=ran.nextInt(256);
  194.                             int n=ran.nextInt(256);
  195.                             Color randomColor=new Color(r,n,b);
  196.                             g.setColor(randomColor);
  197.                             g.fillOval(xEnding ,yEnding, 15, 15);
  198.                             if(count < 75)
  199.                                 g.fillOval(xEnding1 ,yEnding1, 15, 15);
  200.                         }
  201.                         break;
  202.                     case 2: {
  203.                         for(int count = 0; count < 300; count++) { //we create 25 lines
  204.                            
  205.                             int ranBetween1 = ran.nextInt(2)+1; //either 1 or 2
  206.                             int ranBetween2 = ran.nextInt(2)+1; //either 1 or 2
  207.                            
  208.                             int xEnding = (int)x2 + (int)(Math.pow(-1, ranBetween1)*ran.nextInt(200));
  209.                             int yEnding = (int)y2 + (int)(Math.pow(-1, ranBetween2)*ran.nextInt(200));
  210.                            
  211.                             int r=ran.nextInt(256);
  212.                             int b=ran.nextInt(256);
  213.                             int n=ran.nextInt(256);
  214.                             Color randomColor=new Color(r,n,b);
  215.                             g.setColor(randomColor);
  216.                             g.drawLine((int)x2,(int) y2, xEnding, yEnding);
  217.                             g.fillOval(xEnding-5,yEnding-5, 10, 10);
  218.                        
  219.                         }
  220.                        
  221. //                      g.fillOval((int) (x2 - k/2),(int)(y2+k/2),k,k);
  222.                         break;
  223.                    
  224.                     }
  225.                     case 3:
  226.                         for(int j=12;j>=0;j--) {
  227.                            
  228.                             int r=ran.nextInt(256);
  229.                             int b=ran.nextInt(256);
  230.                             int n=ran.nextInt(256);
  231.                             Color randomColor=new Color(r,n,b);
  232.                             g.setColor(randomColor);
  233.                             int k=(10+j*10)*2;
  234.                             g.fillOval((int) (x2+k ),(int)(y2 - k ),k,k);
  235.                         }
  236.                         break;
  237.                     case 4:
  238.                         for(int j=12;j>=0;j--) {
  239.                             //Random ran= new Random();
  240.                             int r=ran.nextInt(256);
  241.                             int b=ran.nextInt(256);
  242.                             int n=ran.nextInt(256);
  243.                             Color randomColor=new Color(r,n,b);
  244.                             g.setColor(randomColor);
  245.                             int k=(10+j*10)/2;
  246.                             g.fillOval((int) (x2-k/2),(int)(y2-k/2),3*k,3*k);
  247.                         }
  248.                         break;
  249.                     case 5: {
  250.                         try {
  251.                             img = ImageIO.read(new File("image.jpg"));
  252.                         } catch(IOException e) {
  253.                             System.out.println("No image found!");
  254.                         }
  255.                         g.drawImage(img, (int)x2-130,(int)y2-86,260,172, this);//, dy2, sx1, sy1, sx2, sy2, observer)
  256.                         break;
  257.                     }
  258.                 }
  259.             }
  260.                    
  261.                    
  262.         }      
  263.                     //g.fillOval((int) (x2-k/2),(int)(y2-k/2),k,k);//5ta eksplozija SS ISTI CENTAR, KONCENTRICNI
  264.    
  265.            
  266.         }
  267. //  public void drawStars(int x, int y) {
  268. // 
  269. //  }
  270.     public Board() {
  271.         this.setBackground(new Color(19,10,39));
  272. //      for(int count = 0; count < 100; count++) {
  273. ////            int xStar = ran.nextInt((int) (Math.abs(getWidth())))+1;
  274. ////            int yStar = ran.nextInt((int) (Math.abs(getHeight()/4)))+1;
  275. //          int xStar = 70;
  276. //          int yStar = 70;
  277. //          Graphics g = getGraphics();
  278. //
  279. //          g.setColor(Color.white);
  280. //          Font myFont = new Font("Arial", Font.BOLD, 12);
  281. //          g.setFont(myFont);
  282. //          g.drawString("*", xStar, yStar);
  283. ////            drawStars(xStar,yStar);
  284. //      }
  285.         toolbar = new JPanel();
  286.         toolbar.setBackground(new Color(0,102,204));
  287.         velocitySlider = new JSlider(0,150);
  288.         velocitySlider.setValue(0);
  289.         angleSlider = new JSlider(0,90);
  290.         angleSlider.setValue(0);
  291.         timeField = new JTextField(10);
  292.         label1 = new JLabel();
  293.         labelVelocity = new JLabel("Current velocity is  0  m/s.");
  294.         labelAngle = new JLabel("Current angle is  0°.");
  295.         labelTime = new JLabel("Chosen time is  0  s.");
  296.        
  297.         angleSlider.setPreferredSize(new Dimension(150,30));
  298.        
  299.         toolbar.add(velocitySlider);
  300.         toolbar.add(labelVelocity);
  301.         toolbar.add(angleSlider);
  302.         toolbar.add(labelAngle);
  303.         toolbar.add(timeField);
  304.         toolbar.add(labelTime);
  305.  
  306.         toolbar.add(label1);
  307.         velocitySlider.addChangeListener(this);
  308.         angleSlider.addChangeListener(this);
  309.         timeField.addActionListener(this);
  310.         //System.out.println("velocity = " + x.getVelocity() +", angle = " + x.getTheta() +", time = " + x.getTime());
  311.    
  312.         JPanel colorMenu= new JPanel();
  313.        
  314.         colorMenu.setLayout(new BoxLayout(colorMenu, BoxLayout.Y_AXIS));
  315.        
  316.         colorMenu.setBackground(new Color(0,102,204));
  317.         colorMenu.add(new JLabel("Choose a color!"));
  318.        
  319.         ButtonGroup radioGroupColors = new ButtonGroup();
  320.         ButtonGroup radioGroupExplosions = new ButtonGroup();
  321.  
  322.         for(int i=0;i<6;i++) {
  323.             JRadioButton rb = new JRadioButton(colorsString[i]);
  324.             JRBColors[i] = rb;
  325.             radioGroupColors.add(rb);
  326.             colorMenu.add(rb);
  327.             JRBColors[i].addActionListener(this);
  328.             JRBColors[i].setBackground(new Color(0,102,204));
  329.  
  330.             rb = new JRadioButton(explosionsString[i]);
  331.             JRBExplosions[i] = rb;
  332.             radioGroupExplosions.add(rb);
  333.             colorMenu.add(rb);
  334.             JRBExplosions[i].addActionListener(this);
  335.             JRBExplosions[i].setBackground(new Color(0,102,204));
  336.            
  337.         }
  338.         setLayout(new BorderLayout());
  339.         this.add(toolbar, BorderLayout.NORTH);
  340.         this.add(colorMenu, BorderLayout.EAST);
  341.    
  342.     }
  343.     public static void main(String[] args) {   
  344.             JFrame frame = new JFrame();
  345.             Board board = new Board();
  346.             frame.add(board);
  347.             frame.setVisible(true);
  348.             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  349.             frame.setSize(1000,800);
  350.             frame.setTitle("Play a game!");
  351.     }
  352.    
  353. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement