Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //animal
- public class Animal{
- private String nome;
- private boolean aberta;
- public Animal(){
- this.nome = null;
- this.aberta = false;
- }
- public boolean getAberta(){
- return this.aberta;
- }
- public Animal(String nome){
- this.nome = nome;
- this.aberta = false;
- }
- public void setNome (String n){
- this.nome = n;
- }
- public String getNome(){
- return this.nome;
- }
- public void setFechada(){
- this.aberta = false;
- }
- public void setAberta(){
- this.aberta=true;
- }
- }
- //Bichos
- import java.util.List;
- import java.util.ArrayList;
- import java.util.Collections;
- import javax.swing.ImageIcon;
- public class Bichos {
- private List <Animal> zoologico = new ArrayList <Animal>();
- public Bichos(){
- Animal a;
- String nome1, nome2, nome3, nome4, nome5, nome0;
- nome0 = String.format ("borboleta.png");
- nome1 = String.format ("cachorro.png");
- nome2 = String.format ("carangueijo.png");
- nome3 = String.format ("cobra.png");
- nome4 = String.format ("elefante.png");
- nome5 = String.format ("urso.png");
- int conta, j;
- for (j=0;j<2;j++){
- conta = 0;
- while (conta<6){
- a = new Animal();
- switch (conta){
- case 0: a.setNome (nome0); break;
- case 1: a.setNome (nome1); break;
- case 2: a.setNome (nome2); break;
- case 3: a.setNome (nome3); break;
- case 4: a.setNome (nome4); break;
- case 5: a.setNome (nome5); break;
- }
- conta++; zoologico.add(a);
- }
- }
- }
- public void zerazologico(){
- zoologico.clear();
- }
- public int qtdade(){
- return zoologico.size();
- }
- public void embaralha(){
- Collections.shuffle(zoologico);
- }
- public Animal getAnimal(int posicao){
- Animal c=zoologico.get(posicao);
- return c;
- }
- public ImageIcon getIcone(int posicao){
- Animal c = zoologico.get(posicao);
- String nome = c.getNome();
- ImageIcon ic = new ImageIcon (getClass().getResource(nome));
- return ic;
- }
- public String getNome (int posicao){
- Animal c=zoologico.get(posicao);
- return c.getNome();
- }
- }
- import java.util.*;
- import java.awt.BorderLayout;
- import java.awt.GridLayout;
- import java.awt.event.*;
- import javax.swing.*;
- public class Jogar extends JFrame implements ActionListener{
- Bichos b;
- JButton btMatriz[][] = new JButton [2][6];
- Icon IconeFechado;
- boolean primeiro=true;
- String primeiroNome=null;
- int numeroPosicao;
- int iant, jant;
- public Jogar(){
- super ("Jogo da Memoria");
- setLayout(new BorderLayout());
- criacomponentes();
- }
- public void criacomponentes(){
- int i, j, posicao;
- b=new Bichos ();
- b.embaralha();
- IconeFechado = new ImageIcon(getClass().getResource("fechado.png"));
- JPanel painelcentro = new JPanel (new GridLayout(2,6,5,5));
- for (i=0;i<2;i++){
- for(j=0; j<6;j++){
- btMatriz [i][j] = new JButton();
- btMatriz [i][j].setIcon(IconeFechado);
- btMatriz [i][j].addActionListener(this);
- painelcentro.add(btMatriz[i][j]);
- }
- }
- add(painelcentro, BorderLayout.CENTER);
- }
- public int retornaPosicao(int lin, int col){
- int posicao=0;
- if (lin== 0 && col == 0) posicao = 0;
- if (lin== 0 && col == 1) posicao = 1;
- if (lin== 0 && col == 2) posicao = 2;
- if (lin== 0 && col == 3) posicao = 3;
- if (lin== 0 && col == 4) posicao = 4;
- if (lin== 0 && col == 5) posicao = 5;
- if (lin== 1 && col == 0) posicao = 6;
- if (lin== 1 && col == 1) posicao = 7;
- if (lin== 1 && col == 2) posicao = 8;
- if (lin== 1 && col == 3) posicao = 9;
- if (lin== 1 && col == 4) posicao = 10;
- if (lin== 1 && col == 5) posicao = 11;
- return posicao;
- }
- public void actionPerformed (ActionEvent e){
- int i, j, posicao, segundaPosicao;
- for (i=0; i<2; i++){
- for (j=0; j<6; j++){
- //melhorar
- if (e.getSource()== btMatriz[i][j]){
- posicao = retornaPosicao(i,j);
- Animal a = b.getAnimal(posicao);
- btMatriz[i][j].setIcon(b.getIcone(posicao));
- JOptionPane.showMessageDialog(null, b.getNome(posicao));
- //melhorar
- }
- }
- }
- }
- public static void main (String [] args){
- Jogar j=new Jogar();
- j.setSize(1000, 400);
- j.setLocation(250, 150);
- j.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement