The goal of this application is to get a random value and to make that value the speed of the images themselves. Whoever reaches the end of Jframe the fastest, then the new JDialog is activated and it is written who is the winner.
I couldn't solve the mistake, so here's the code, I hope someone succeeds
package interfejs;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import javax.imageio.ImageIO;
/**
*
* @author SMRTNIK
*/
public class AnimatedBoat {
public static void main(String[] args) {
new AnimatedBoat();
}
public ArrayList<Integer> lista = new ArrayList<>();
Random rand = new Random();
private int xPos = 0;
private int direction = 1;
int low = 1;
int high = 11;
public boolean gotovCrveni = false;
public boolean gotovPlavi = false;
public boolean gotovRozi = false;
public boolean gotovZuti = false;
static ArrayList<Integer> kraj = new ArrayList<>();
public void setGotovZuti(boolean gotovZuti) {
this.gotovZuti = gotovZuti;
}
public void setGotovPlavi(boolean gotovPlavi) {
this.gotovPlavi = gotovPlavi;
}
public void setGotovRozi(boolean gotovRozi) {
this.gotovRozi = gotovRozi;
}
public void setGotovCrveni(boolean gotovCrveni) {
this.gotovCrveni = gotovCrveni;
}
public AnimatedBoat() {
for (int i = 5; i <= 300; i -= -25) {
lista.add(i);
}
Collections.shuffle(lista);
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Trke");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(900, 500));
frame.setLayout(new BorderLayout());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setLayout(new GridLayout(4, 1));
frame.setBackground(Color.white);
enemyRed enemyRed = new enemyRed(); // sub-panel 1
enemyPink enemyPink = new enemyPink(); // sub-panel 2
enemyBlue enemyBlue = new enemyBlue(); // sub-panel 3
enemyYellow enemyYellow = new enemyYellow(); // sub-panel 4
frame.add(enemyRed);
frame.add(enemyPink);
frame.add(enemyBlue);
frame.add(enemyYellow);
frame.setLocationRelativeTo(null);
d1.setPreferredSize(new Dimension(200, 400));
label.setLocation(100, 200);
d1.add(label);
d1.pack();
d1.setLocationRelativeTo(frame);
d1.setVisible(false);
}
});
}
public void dijalog() {
int i = Collections.min(kraj);
int pobednik = kraj.indexOf(i);
if (gotovCrveni == true && gotovRozi == true && gotovPlavi == true && gotovZuti == true) {
if (pobednik == 0) {
label.setText("Pobednik je crveni!");
d1.setVisible(true);
return;
} else if (pobednik == 1) {
label.setText("Pobednik je rozi!");
d1.setVisible(true);
return;
} else if (pobednik == 2) {
label.setText("Pobednik je plavi!");
d1.setVisible(true);
return;
} else if (pobednik == 3) {
label.setText("Pobednik je zuti!");
d1.setVisible(true);
return;
}
} else {
dijalog();
}
}
JDialog d1 = new JDialog();
JLabel label = new JLabel("obavestenje");
public class enemyRed extends JPanel {
private BufferedImage enemyRed;
int result = rand.nextInt(high - low) + low;
int brzina = lista.get(result);
int brojac = 1;
public enemyRed() {
System.out.println(brzina);
vratiBroj(brzina);
try {
enemyRed = ImageIO.read(new File("C:\\Users\\SMRTNIK\\Documents\\NetBeansProjects\\Sistem elektronske biblioteke - SEBv3\\images\\enemy - red.png"));
Timer timer1 = new Timer(brzina, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
xPos += direction;
if (xPos + enemyRed.getWidth() > getWidth()) {
if (brojac == 1) {
++brojac;
setGotovCrveni(true);
dijalog();
}
} else {
repaint();
}
}
});
timer1.start();
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
public Dimension getPreferredSize() {
return enemyRed == null ? super.getPreferredSize() : new Dimension(enemyRed.getWidth() * 4, enemyRed.getHeight());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(enemyRed, xPos, 0, this);
// g.drawImage(enemyPink, xPos, 50, this);
// g.drawImage(enemyBlue, xPos, 100, this);
// g.drawImage(enemyYellow, xPos, 150, this);
}
}
public class enemyPink extends JPanel {
private BufferedImage enemyPink;
int result = rand.nextInt(high - low) + low;
int brzina = lista.get(result);
int brojac = 1;
public enemyPink() {
vratiBroj(brzina);
try {
enemyPink = ImageIO.read(new File("C:\\Users\\SMRTNIK\\Documents\\NetBeansProjects\\Sistem elektronske biblioteke - SEBv3\\images\\enemy - pink.png"));
Timer timer2 = new Timer(brzina, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
xPos += direction;
if (xPos + enemyPink.getWidth() > getWidth()) {
if (brojac == 1) {
++brojac;
setGotovRozi(true);
dijalog();
}
} else {
repaint();
}
}
});
timer2.start();
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
public Dimension getPreferredSize() {
return enemyPink == null ? super.getPreferredSize() : new Dimension(enemyPink.getWidth() * 4, enemyPink.getHeight());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(enemyPink, xPos, 0, this);
}
}
public class enemyBlue extends JPanel {
private BufferedImage enemyBlue;
int result = rand.nextInt(high - low) + low;
int brzina = lista.get(result);
int brojac = 1;
public enemyBlue() {
vratiBroj(brzina);
try {
System.out.println(brzina);
enemyBlue = ImageIO.read(new File("C:\\Users\\SMRTNIK\\Documents\\NetBeansProjects\\Sistem elektronske biblioteke - SEBv3\\images\\enemy - blue.png"));
Timer timer3 = new Timer(brzina, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
xPos += direction;
if (xPos + enemyBlue.getWidth() > getWidth()) {
if (brojac == 1) {
++brojac;
setGotovPlavi(true);
dijalog();
}
} else {
repaint();
}
}
});
timer3.start();
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
public Dimension getPreferredSize() {
return enemyBlue == null ? super.getPreferredSize() : new Dimension(enemyBlue.getWidth() * 4, enemyBlue.getHeight());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(enemyBlue, xPos, 0, this);
}
}
public class enemyYellow extends JPanel {
private BufferedImage enemyYellow;
int result = rand.nextInt(high - low) + low;
int brzina = lista.get(result);
int brojac = 1;
public enemyYellow() {
vratiBroj(brzina);
try {
System.out.println(brzina);
enemyYellow = ImageIO.read(new File("C:\\Users\\SMRTNIK\\Documents\\NetBeansProjects\\Sistem elektronske biblioteke - SEBv3\\images\\enemy - yellow.png"));
Timer timer4 = new Timer(brzina, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
xPos += direction;
if (xPos + enemyYellow.getWidth() > getWidth()) {
if (brojac == 1) {
++brojac;
setGotovZuti(true);
dijalog();
}
} else if (xPos < 0) {
xPos = 0;
repaint();
} else {
repaint();
}
}
});
timer4.start();
} catch (IOException ex) {
ex.printStackTrace();
}
}
//stay swag homie
@Override
public Dimension getPreferredSize() {
return enemyYellow == null ? super.getPreferredSize() : new Dimension(enemyYellow.getWidth() * 4, enemyYellow.getHeight());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(enemyYellow, xPos, 0, this);
}
}
static public void vratiBroj(int v) {
kraj.add(v);
}
}
Aucun commentaire:
Enregistrer un commentaire