Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import javax.swing.event.*;
- import javax.swing.text.JTextComponent;
- import java.awt.*;
- import java.awt.event.*;
- import java.awt.image.BufferedImage;
- import java.util.Random;
- import java.lang.Math;
- import java.lang.Object;
- import java.lang.Object.*;
- import java.io.File;
- import javax.imageio.ImageIO;
- import javax.imageio.ImageIO.*;
- import java.io.IOException;
- public class Board extends JPanel implements ChangeListener, ActionListener{
- JSlider velocitySlider;
- JSlider angleSlider;
- JTextField timeField;
- JLabel label1;
- JLabel labelVelocity;
- JLabel labelAngle;
- JLabel labelTime;
- JPanel toolbar;
- JRadioButton[] JRBColors = new JRadioButton[6];
- JRadioButton[] JRBExplosions = new JRadioButton[6];
- BufferedImage img;
- Random ran= new Random();
- Canvas x=new Canvas();
- Color[] colorsColor = {Color.red,Color.orange,Color.yellow,Color.blue,Color.green,new Color(255,255,255)};
- String[] colorsString = {"red","orange","yellow","blue","green","random"};
- String[] explosionsString = {"Concentric Circles","Real Firework","Line Firework", "Line Circles", "Circles","Image"};
- @Override
- public void stateChanged(ChangeEvent e) { //for velocity and angle
- if(e.getSource()==velocitySlider) {
- x.setVelocity(velocitySlider.getValue());
- labelVelocity.setText("Current velocity is " + velocitySlider.getValue() +" m/s.");
- System.out.println("velocity = " + x.getVelocity() +", angle = " + x.getTheta() +", time = " + x.getTime());
- }
- if(e.getSource()==angleSlider) {
- x.setTheta(angleSlider.getValue());
- labelAngle.setText("Current angle is " + angleSlider.getValue() +"°.");
- // System.out.println("velocity = " + x.getVelocity() +", angle = " + x.getTheta() +", time = " + x.getTime());
- }
- repaint();
- }
- @Override
- public void actionPerformed(ActionEvent e) { //for time
- if(e.getSource() == timeField) {
- String timeString = ((JTextComponent) e.getSource()).getText();
- int timeInt = Integer.parseInt(timeString);
- x.setTime(timeInt);
- labelTime.setText("Chosen time is " + timeInt +" s.");
- //System.out.println("velocity = " + x.getVelocity() +", angle = " + x.getTheta() +", time = " + x.getTime());
- }
- else {
- for(int i=0;i<6;i++) {
- if(e.getSource() == JRBColors[i]) {
- if(i!=5)
- x.setColor(colorsColor[i]);
- else {
- int redRandom,greenRandom,blueRandom;
- redRandom = ran.nextInt(256);
- greenRandom = ran.nextInt(256);
- blueRandom = ran.nextInt(256);
- x.setColor(new Color(redRandom,greenRandom,blueRandom));
- }
- }
- }
- for(int i=0;i<6;i++) {
- if(e.getSource() == JRBExplosions[i])
- x.setTypeOfExplosion(i);
- }
- }
- repaint();
- }
- //// @Override
- //// public void itemStateChanged(ItemEvent e) { //for color choice
- //// Color currColor;
- // System.out.println(e.getSource());
- //// for(int i=0;i<6;i++) {
- //// if(e.getSource()==JRBColors[i])
- //// System.out.println(colorsString[i]);
- ////
- //// }
- ////
- // }
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- for(int count = 0; count < 100; count++) {
- int xStar = ran.nextInt((int) (Math.abs(getWidth())))+1;
- int yStar = ran.nextInt((int) (Math.abs(getHeight()/4)))+1;
- g.setColor(new Color(255,248,138));
- Font font = new Font("Arial", Font.BOLD, 16);
- g.setFont(font);
- g.drawString("*", xStar, yStar);
- if(count>60)
- g.drawString("*", xStar, yStar+50);
- if(count>80)
- g.drawString("*", xStar, yStar+80);
- if(count>90)
- g.drawString("*", xStar, yStar+100);
- }
- double x1=0;
- double y1=0;
- double x2=0;
- double y2=getHeight();
- g.setColor(x.getColor());
- for(double i=0;i<=x.getTime();i+=0.69) {
- double angle = Math.toRadians(x.getTheta());
- x2 = x.getVelocity()*Math.cos(angle)*i;
- y2 = (x.getVelocity()*Math.sin(angle)*i - 5*i*i);
- y2 = getHeight()-y2;
- Font font = new Font("Arial", Font.BOLD, 12);
- // g.setFont(font);
- if(i>=0.69)
- g.drawLine((int)x1,(int) y1,(int)x2,(int)y2);
- x1=x2;
- y1=y2;
- if(x.getTime()-i<=0.69 && (x1!=0 && y1!=0)) {
- switch(x.getTypeOfExplosion()) {
- case 0:
- double x11 = x2;
- double y11 = y2;
- for(int count=0; count<40; count++){
- double ranAngle = ran.nextInt(628) / 100.0;
- double x22=0;
- double y22=0;
- System.out.println(x11 + " "+ y11);
- for(double z=0;z<=x.getTime()/4;z+=0.69) {
- x22 = x.getVelocity()*Math.cos(ranAngle)*z + x2;
- y22 = -(x.getVelocity()*Math.sin(ranAngle)*z- 5*z*z) + y2;
- //y22 = getHeight()-y22;
- // if(z==0)
- // g.drawLine((int)x2,(int) y2,(int) x22,(int) y22);
- if(x.getTime() - z > 0.69 && z>0)
- g.drawLine((int)x11,(int) y11,(int)x22,(int)y22);
- x11=x22;
- y11=y22;
- //g.fillOval((int) (x2-k/2),(int)(y2-k/2),k,k);
- }
- for(int count1 = 0; count1 < 300; count1++) { //we create 25 lines
- int ranBetween1 = ran.nextInt(2)+1; //either 1 or 2
- int ranBetween2 = ran.nextInt(2)+1; //either 1 or 2
- int xEnding = (int)x22 + (int)(Math.pow(-1, ranBetween1)*ran.nextInt(50));
- int yEnding = (int)y22 + (int)(Math.pow(-1, ranBetween2)*ran.nextInt(50));
- int r=ran.nextInt(256);
- int b=ran.nextInt(256);
- int n=ran.nextInt(256);
- Color randomColor=new Color(r,n,b);
- g.setColor(randomColor);
- g.drawLine((int)x22,(int) y22, xEnding, yEnding);
- }
- }
- break;
- case 1:
- for(int count = 0; count < 150; count++) { //we create 25 lines
- int ranBetween1 = ran.nextInt(2)+1; //either 1 or 2
- int ranBetween2 = ran.nextInt(2)+1; //either 1 or 2
- int xEnding = (int)x2 + (int)(Math.pow(-1, ranBetween1)*ran.nextInt(100));
- int yEnding = (int)y2 + (int)(Math.pow(-1, ranBetween2)*ran.nextInt(100));
- int xEnding1 = (int)x2 + (int)(Math.pow(-1, ranBetween1)*ran.nextInt(200));
- int yEnding1 = (int)y2 + (int)(Math.pow(-1, ranBetween2)*ran.nextInt(200));
- int r=ran.nextInt(256);
- int b=ran.nextInt(256);
- int n=ran.nextInt(256);
- Color randomColor=new Color(r,n,b);
- g.setColor(randomColor);
- g.fillOval(xEnding ,yEnding, 15, 15);
- if(count < 75)
- g.fillOval(xEnding1 ,yEnding1, 15, 15);
- }
- break;
- case 2: {
- for(int count = 0; count < 300; count++) { //we create 25 lines
- int ranBetween1 = ran.nextInt(2)+1; //either 1 or 2
- int ranBetween2 = ran.nextInt(2)+1; //either 1 or 2
- int xEnding = (int)x2 + (int)(Math.pow(-1, ranBetween1)*ran.nextInt(200));
- int yEnding = (int)y2 + (int)(Math.pow(-1, ranBetween2)*ran.nextInt(200));
- int r=ran.nextInt(256);
- int b=ran.nextInt(256);
- int n=ran.nextInt(256);
- Color randomColor=new Color(r,n,b);
- g.setColor(randomColor);
- g.drawLine((int)x2,(int) y2, xEnding, yEnding);
- g.fillOval(xEnding-5,yEnding-5, 10, 10);
- }
- // g.fillOval((int) (x2 - k/2),(int)(y2+k/2),k,k);
- break;
- }
- case 3:
- for(int j=12;j>=0;j--) {
- int r=ran.nextInt(256);
- int b=ran.nextInt(256);
- int n=ran.nextInt(256);
- Color randomColor=new Color(r,n,b);
- g.setColor(randomColor);
- int k=(10+j*10)*2;
- g.fillOval((int) (x2+k ),(int)(y2 - k ),k,k);
- }
- break;
- case 4:
- for(int j=12;j>=0;j--) {
- //Random ran= new Random();
- int r=ran.nextInt(256);
- int b=ran.nextInt(256);
- int n=ran.nextInt(256);
- Color randomColor=new Color(r,n,b);
- g.setColor(randomColor);
- int k=(10+j*10)/2;
- g.fillOval((int) (x2-k/2),(int)(y2-k/2),3*k,3*k);
- }
- break;
- case 5: {
- try {
- img = ImageIO.read(new File("image.jpg"));
- } catch(IOException e) {
- System.out.println("No image found!");
- }
- g.drawImage(img, (int)x2-130,(int)y2-86,260,172, this);//, dy2, sx1, sy1, sx2, sy2, observer)
- break;
- }
- }
- }
- }
- //g.fillOval((int) (x2-k/2),(int)(y2-k/2),k,k);//5ta eksplozija SS ISTI CENTAR, KONCENTRICNI
- }
- // public void drawStars(int x, int y) {
- //
- // }
- public Board() {
- this.setBackground(new Color(19,10,39));
- // for(int count = 0; count < 100; count++) {
- //// int xStar = ran.nextInt((int) (Math.abs(getWidth())))+1;
- //// int yStar = ran.nextInt((int) (Math.abs(getHeight()/4)))+1;
- // int xStar = 70;
- // int yStar = 70;
- // Graphics g = getGraphics();
- //
- // g.setColor(Color.white);
- // Font myFont = new Font("Arial", Font.BOLD, 12);
- // g.setFont(myFont);
- // g.drawString("*", xStar, yStar);
- //// drawStars(xStar,yStar);
- // }
- toolbar = new JPanel();
- toolbar.setBackground(new Color(0,102,204));
- velocitySlider = new JSlider(0,150);
- velocitySlider.setValue(0);
- angleSlider = new JSlider(0,90);
- angleSlider.setValue(0);
- timeField = new JTextField(10);
- label1 = new JLabel();
- labelVelocity = new JLabel("Current velocity is 0 m/s.");
- labelAngle = new JLabel("Current angle is 0°.");
- labelTime = new JLabel("Chosen time is 0 s.");
- angleSlider.setPreferredSize(new Dimension(150,30));
- toolbar.add(velocitySlider);
- toolbar.add(labelVelocity);
- toolbar.add(angleSlider);
- toolbar.add(labelAngle);
- toolbar.add(timeField);
- toolbar.add(labelTime);
- toolbar.add(label1);
- velocitySlider.addChangeListener(this);
- angleSlider.addChangeListener(this);
- timeField.addActionListener(this);
- //System.out.println("velocity = " + x.getVelocity() +", angle = " + x.getTheta() +", time = " + x.getTime());
- JPanel colorMenu= new JPanel();
- colorMenu.setLayout(new BoxLayout(colorMenu, BoxLayout.Y_AXIS));
- colorMenu.setBackground(new Color(0,102,204));
- colorMenu.add(new JLabel("Choose a color!"));
- ButtonGroup radioGroupColors = new ButtonGroup();
- ButtonGroup radioGroupExplosions = new ButtonGroup();
- for(int i=0;i<6;i++) {
- JRadioButton rb = new JRadioButton(colorsString[i]);
- JRBColors[i] = rb;
- radioGroupColors.add(rb);
- colorMenu.add(rb);
- JRBColors[i].addActionListener(this);
- JRBColors[i].setBackground(new Color(0,102,204));
- rb = new JRadioButton(explosionsString[i]);
- JRBExplosions[i] = rb;
- radioGroupExplosions.add(rb);
- colorMenu.add(rb);
- JRBExplosions[i].addActionListener(this);
- JRBExplosions[i].setBackground(new Color(0,102,204));
- }
- setLayout(new BorderLayout());
- this.add(toolbar, BorderLayout.NORTH);
- this.add(colorMenu, BorderLayout.EAST);
- }
- public static void main(String[] args) {
- JFrame frame = new JFrame();
- Board board = new Board();
- frame.add(board);
- frame.setVisible(true);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setSize(1000,800);
- frame.setTitle("Play a game!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement