istoisso Posted April 3, 2013 at 05:50 PM Report #501468 Posted April 3, 2013 at 05:50 PM (edited) Boa tarde comunidade portugal-a-programar! Aqui deixo um exemplo de uma "roleta" da sorte feita por mim à uns tempos, tive algumas duvidas para a criação dela mas agora está a funcionar, espero que ajude quem tem duvidas neste campo, pois é com exemplos que se tiram muitas duvidas 😉 Aqui fica a class principal: 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); } } } E a class dos eventos: package Roleta_pkg; 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 } } Uma imagem, Alguma duvida digam 😉 Download do projecto completo pra quem quiser aqui: http://rapidgator.net/file/22843519db634a24c526df13be67b802/Roleta_pt-a-programar.rar.html Cumprimentos! Edited April 3, 2013 at 05:52 PM by pmg Falta LP no GeSHi
LuisMendes330 Posted April 3, 2013 at 08:55 PM Report #501493 Posted April 3, 2013 at 08:55 PM Como faço exactamente para correr esse programa no Netbeans?
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now