[exemplo]Roleta da sorte (interface grafica)

Boas pessoal,

Já à algum tempo fiz este programa que é uma roleta da sorte, foi dos meus primeiros com interface grafica :slight_smile:
Tive várias duvidas mas resolveram-se. Deixo aqui o programa para que possam tirar quaisquer duvidas que tenham :wink:

[code]import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Roulette {
JFrame window_x;
JPanel rollete_panel;
//3 labels img’s
JLabel limg1;
JLabel limg2;
JLabel limg3;
JButton bstop;
JPanel final_panel;
JLabel final_l;
ImageIcon play = new ImageIcon(getClass().getResource(“play.png”));
ImageIcon pause = new ImageIcon(getClass().getResource(“pausa.png”));
String[] images = {“img1.png”, “img2.png”, “img3.png”, “img4.png”, “img5.png”}; //0-4
int coco = 0, stop_r = 0;
JPanel background_panel;

public static void main(String[] args) throws InterruptedException {
Roulette x = new Roulette();
x.window_x.setVisible(true);
x.a_background();

}

public Roulette(){
//window_x
window_x = new JFrame();
window_x.setBounds(new Rectangle(500,200));
window_x.setTitle(“Wheel of fortune!”);
window_x.setLocationRelativeTo(null);
window_x.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Panel background
background_panel = new JPanel();
background_panel.setLayout(new GridLayout(1,1));

//Panel rollete_panel
rollete_panel = new JPanel();
rollete_panel.setLayout(new GridLayout(1,4));

//labels
limg1 = new JLabel(new ImageIcon(getClass().getResource(“parte1.png”)));
limg2 = new JLabel(new ImageIcon(getClass().getResource(“parte2.png”)));
limg3 = new JLabel(new ImageIcon(getClass().getResource(“parte3.png”)));

// stop
bstop = new JButton("");
bstop.setIcon(play);
bstop.setOpaque(false);
bstop.setContentAreaFilled(false);
bstop.setBorderPainted(false);
bstop.addActionListener(new Events(this));
bstop.setVisible(true);

//add’s
rollete_panel.add(limg1);
rollete_panel.add(limg2);
rollete_panel.add(limg3);
rollete_panel.add(bstop);
background_panel.add(rollete_panel);
window_x.add(background_panel);
}

public void play_stop(){
if (bstop.getIcon().equals(play)){
coco = 1;
bstop.setIcon(pause);
}
else{
coco = 0;
check();
bstop.setIcon(play);
}
}

public void a_background() throws InterruptedException{
Random x = new Random();
int n1, n2, n3;
while(true){
Thread.sleep(100);
if (stop_r == 1){
break;
}

if (coco == 1){
n1 = x.nextInt(2);
n2 = x.nextInt(2);
n3 = x.nextInt(2);
limg1.setIcon(new ImageIcon(getClass().getResource(images[n1])));
limg2.setIcon(new ImageIcon(getClass().getResource(images[n2])));
limg3.setIcon(new ImageIcon(getClass().getResource(images[n3])));
}
}
}

public void check(){
if ((limg1.getIcon().toString().equals(limg2.getIcon().toString())) && (limg2.getIcon().toString().equals(limg3.getIcon().toString()))){
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
System.out.println(“Error”);
e.printStackTrace();
}
final_panel = new JPanel();
window_x.remove(rollete_panel);
window_x.remove(background_panel);
final_l = new JLabel(new ImageIcon(getClass().getResource(“fim.jpg”)));
final_panel.add(final_l);
window_x.setExtendedState(Frame.MAXIMIZED_BOTH);
window_x.add(final_panel);
}
}
}[/code]

E aqui a class dos eventos:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Events implements ActionListener{

private Roulette r;

public Events(Roulette r){
this.r = r;
}

public void actionPerformed(ActionEvent e) {
Object o=e.getSource();

if (o == r.bstop){
r.play_stop();
}//button to stop roulette ;)
}

}

E, já agora, uma imagem do programa logo depois de aberto :wink:

Alguma dúvida, disponham :wink:

E pra quem quiser o projecto inteiro, aqui tem: http://rapidgator.net/file/22843519db634a24c526df13be67b802/Roleta_pt-a-programar.rar.html

Cumprimentos a todos os programadores de java!

Poderia ter utilizado o GitHub, se você vai deixar o projeto para estudos. Lá poderiamos analisar o código sugerir melhorias com solicitações de Pull etc =]