Results 1 to 3 of 3
  1. #1
    thg99 is offline Member
    Join Date
    Apr 2012
    Location
    Sao Paulo, Brazil
    Posts
    2
    Rep Power
    0

    Exclamation Java Game Programming

    Hello folks!
    First of all, sorry if my English is a bit strange, I learned it by myself, so...
    I'm doing a game for children and it's part of a school project, but I'm having too much difficulties, and then I'd like your help. My game is an Anagram. Child has to form words with the given letters. But I'm having problems in the part where the game has to verify if the formed word is right.
    Please help me, I appreciate if you could help me. Thanks.

    Java Code:
    public class NivelFacil extends JFrame implements ActionListener {
    	
    	private JPanel pnlSorteio, pnlTentativa, pnlAcerto, pnlAcertoInfo;
    	private JButton btnNovo, btnVoltar;
    	private JTextArea txtPalavras;
    	private JLabel lblLetraSorteio, lblLetraSorteio2, lblLetraSorteio3, lblLetraSorteio4, lblLetraSorteio5;
    	private JButton btnLetraSorteio[] = new JButton[5];
    	private JLabel imgFundo, imgPersonagem, lblAcertos, lblSorteadas,
    			lblNumSorteadas, lblNumAcertos, lblLetrasSorteadas, lblPontuacao, lblScore;
    	
    	public NivelFacil(){
    		inicializarGUI();
    	}
    	
    	private void inicializarGUI(){
    		setSize(1024, 768);
    		setResizable(true);
    		setUndecorated(true);
    		setLocationRelativeTo(null);
    		setDefaultCloseOperation(NivelFacil.DISPOSE_ON_CLOSE);
    		
    		final Font calibriBold14 = new Font("Calibri", Font.BOLD, 14);
    		final Font calibriPlain14 = new Font("Calibri", Font.PLAIN, 14);
    		
    		final Color fonteBranco = new Color(255,255,255);
    		
    		btnNovo = new JButton(new ImageIcon("img/NivelFacil/btnNovoOver.png"));
    		btnNovo.setBounds(24, 24, 100, 40);
    		btnNovo.setContentAreaFilled(false);
    		btnNovo.setBorderPainted(false);
    		btnNovo.setRolloverEnabled(true);
    		btnNovo.setRolloverIcon(new ImageIcon("img/NivelFacil/btnNovo.png"));
    		btnNovo.addActionListener(this);
    		add(btnNovo);
    		
    		btnVoltar = new JButton(new ImageIcon("img/NivelFacil/btnVoltar.png"));
    		btnVoltar.setBounds(24, 66, 100, 40);
    		btnVoltar.setContentAreaFilled(false);
    		btnVoltar.setBorderPainted(false);
    		btnVoltar.setRolloverEnabled(true);
    		btnVoltar.setRolloverIcon(new ImageIcon("img/NivelFacil/btnVoltarOver.png"));
    		btnVoltar.addActionListener(this);
    		add(btnVoltar);
    		
    		pnlTentativa = new JPanel(null);
    		pnlTentativa.setBounds(367, 590, 289, 65);
    		pnlTentativa.setOpaque(false);
    		pnlTentativa.setBorder(new LineBorder(new java.awt.Color(255,255,255)));
    		add(pnlTentativa);
    		
    		pnlSorteio = new JPanel(null);
    		pnlSorteio.setName("pnlSorteio");
    		pnlSorteio.setBounds(367, 668, 289, 92);
    		add(pnlSorteio);
    		
    		lblLetrasSorteadas = new JLabel("Letras Sorteadas");
    		lblLetrasSorteadas.setBounds(86, 8, 131, 21);
    		lblLetrasSorteadas.setFont(calibriBold14);
    		pnlSorteio.add(lblLetrasSorteadas);
    		
    		int x = 10;
    
    		MouseMoveListener l = new MouseMoveListener(pnlTentativa, pnlSorteio);
    		
    		lblLetraSorteio = new JLabel("A");
    		lblLetraSorteio.setBounds(10, 32, 49, 49);
    		lblLetraSorteio.setBorder(new LineBorder(new Color(0,0,0)));
    		lblLetraSorteio.addMouseListener(l);
    		lblLetraSorteio.addMouseMotionListener(l);
    		pnlSorteio.add(lblLetraSorteio);
    		
    		lblLetraSorteio2 = new JLabel("G");
    		lblLetraSorteio2.setBounds(65, 32, 49, 49);
    		lblLetraSorteio2.setBorder(new LineBorder(new Color(0,0,0)));
    		lblLetraSorteio2.addMouseListener(l);
    		lblLetraSorteio2.addMouseMotionListener(l);
    		pnlSorteio.add(lblLetraSorteio2);
    		
    		lblLetraSorteio3 = new JLabel("U");
    		lblLetraSorteio3.setBounds(120, 32, 49, 49);
    		lblLetraSorteio3.setBorder(new LineBorder(new Color(0,0,0)));
    		lblLetraSorteio3.addMouseListener(l);
    		lblLetraSorteio3.addMouseMotionListener(l);
    		pnlSorteio.add(lblLetraSorteio3);
    		
    		lblLetraSorteio4 = new JLabel("A");
    		lblLetraSorteio4.setBounds(185, 32, 49, 49);
    		lblLetraSorteio4.setBorder(new LineBorder(new Color(0,0,0)));
    		lblLetraSorteio4.addMouseListener(l);
    		lblLetraSorteio4.addMouseMotionListener(l);
    		pnlSorteio.add(lblLetraSorteio4);
    		
    		/*btnLetraSorteio[1] = new JButton("A");
    		btnLetraSorteio[1].setBounds(30, 32, 49, 49);
    		//MouseMoveListener l = new MouseMoveListener(pnlTentativa, pnlSorteio);
    		btnLetraSorteio[1].addMouseListener(l);
    		btnLetraSorteio[1].addMouseMotionListener(l);
    		pnlSorteio.add(btnLetraSorteio[1]);*/
    		
    		pnlAcerto = new JPanel(null);
    		pnlAcerto.setBounds(824, 213, 177, 342);
    		pnlAcerto.setOpaque(false);
    		pnlAcerto.setBorder(new LineBorder(new java.awt.Color(255,255,255)));
    		add(pnlAcerto);
    		
    		txtPalavras = new JTextArea(5, 1);
    		txtPalavras.setBounds(8, 8, 161, 226);
    		txtPalavras.setEditable(false);
    		pnlAcerto.add(txtPalavras);
    		
    		pnlAcertoInfo = new JPanel(null);
    		pnlAcertoInfo.setBounds(0, 240, 177, 105);
    		pnlAcertoInfo.setOpaque(false);
    		pnlAcertoInfo.setBorder(new LineBorder(new java.awt.Color(255,255,255)));
    		pnlAcerto.add(pnlAcertoInfo);
    		
    		lblSorteadas = new JLabel("Palavras Sorteadas:");
    		lblSorteadas.setForeground(fonteBranco);
    		lblSorteadas.setFont(calibriBold14);
    		lblSorteadas.setBounds(14, 18, 133, 19);
    		pnlAcertoInfo.add(lblSorteadas);
    		
    		lblNumSorteadas = new JLabel("10");
    		lblNumSorteadas.setForeground(fonteBranco);
    		lblNumSorteadas.setFont(calibriPlain14);
    		lblNumSorteadas.setBounds(146, 18, 16, 19);
    		pnlAcertoInfo.add(lblNumSorteadas);
    		
    		lblAcertos = new JLabel("Acertos:");
    		lblAcertos.setForeground(fonteBranco);
    		lblAcertos.setFont(calibriBold14);
    		lblAcertos.setBounds(49, 43, 56, 19);
    		pnlAcertoInfo.add(lblAcertos);
    		
    		lblNumAcertos = new JLabel("10");
    		lblNumAcertos.setForeground(fonteBranco);
    		lblNumAcertos.setFont(calibriPlain14);
    		lblNumAcertos.setBounds(111, 43, 16, 19);
    		pnlAcertoInfo.add(lblNumAcertos);
    		
    		lblPontuacao = new JLabel("Pontuação:");
    		lblPontuacao.setForeground(fonteBranco);
    		lblPontuacao.setFont(calibriBold14);
    		lblPontuacao.setBounds(34, 68, 78, 19);
    		pnlAcertoInfo.add(lblPontuacao);
    		
    		lblScore = new JLabel("10");
    		lblScore.setForeground(fonteBranco);
    		lblScore.setFont(calibriPlain14);
    		lblScore.setBounds(118, 68, 24, 19);
    		pnlAcertoInfo.add(lblScore);
    		
    		imgPersonagem = new JLabel(new ImageIcon("img/NivelFacil/imgPersonagem.png"));
    		imgPersonagem.setBounds(32, 0, 609, 737);
    		imgPersonagem.setVisible(true);
    		add(imgPersonagem);
    		
    		imgFundo = new JLabel(new ImageIcon("img/NivelFacil/imgFundo.jpg"));
    		imgFundo.setBounds(0, 0, 1024, 768);
    		imgFundo.setVisible(true);
    		add(imgFundo);
    		
    	}
    
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		if (e.getSource() == btnVoltar){
    			 if (JOptionPane.showConfirmDialog(null,"Deseja retornar á tela principal?","Atenção!",JOptionPane.YES_NO_OPTION, 0) == JOptionPane.YES_OPTION){
    				 this.dispose();
    				 new TelaPrincipal().setVisible(true);
    			 }
    		}
    		if (e.getSource() == btnNovo){
    			if (JOptionPane.showConfirmDialog(null,"Deseja iniciar um novo jogo?","Atenção!",JOptionPane.YES_NO_OPTION, 0) == JOptionPane.YES_OPTION){
    				 this.repaint();
    			 }
    		}	
    	}
    	
    	private class MouseMoveListener extends MouseAdapter{
    		
    		private int inicialX = -1;  
    	    private int inicialY = -1;  
    	    private Rectangle2D r2d;  
    	    private JPanel[] panels;  
    	    private JPanel panelIni;
    	    private Point posIni;
    	    private int medidor = 0;
    	  
    	    public MouseMoveListener(JPanel... panels) {  
    	        this.panels = panels;
    	        this.r2d = new Rectangle2D.Double();
    	    }  
    	  
    	    @Override  
    	    public void mouseDragged(MouseEvent e) {  
    	        if ((inicialX != -1) && (inicialY != -1)) {   
    	            Component c = e.getComponent();  
    	            Container p = c.getParent();
    	  
    	            int x = c.getX() + e.getX() - inicialX;    
    	            int y = c.getY() + e.getY() - inicialY;    
    	  
    	            c.setLocation(x, y);    
    	            c.repaint();    
    	  
    	            
    	            if(c.getX() > p.getWidth() || c.getY() > p.getHeight() || c.getX()+c.getWidth()<0 || c.getY()+c.getHeight() < 0)  
    	                mudaPanel(c);  
    	        }    
    	    }  
    	  
    	    @Override  
    	    public void mousePressed(MouseEvent e) {  
    	        inicialX = e.getX();    
    	        inicialY = e.getY();
    	        
    	       if (!e.getComponent().getParent().getName().equals("pnlSorteio")){
    	        		        	
    	        	if(panelIni == null)  
    	                panelIni = (JPanel)e.getComponent().getParent();
    	           
    	            if(posIni == null)  
    	                posIni = e.getComponent().getLocation();
    	       }else{
    	    	   System.out.println("pnlSorteio");
    	       }
    	    }
    	    
    	    @Override  
    	    public void mouseReleased(MouseEvent e) {  
    	        inicialX = -1;    
    	        inicialY = -1;  
    	          
    	        Component c = e.getComponent();  
    	        Container p = c.getParent();  
    	  
    	        int x = c.getX();  
    	        int y = c.getY();  
    	  
    	        if(c.getX()+c.getWidth() > p.getWidth() )  
    	            x = p.getWidth() - c.getWidth();  
    	  
    	        if(c.getY()+c.getHeight() > p.getHeight() )  
    	            y = p.getHeight() - c.getHeight();  
    	  
    	        if(c.getX() < 0)  
    	            x = 0;  
    	  
    	        if(c.getY() < 0)  
    	            y = 0;  
    	  
    	        c.setLocation(x, y);
    	  
    	        if( p!=panelIni && !verificaPalavra(p)) {  
    	        	p.remove(c);   
    	            panelIni.add(c);  
    	            c.setLocation(posIni);  
    	            p.repaint();
    	        }else{
    	            panelIni = (JPanel)p;   
    	            posIni = c.getLocation();
    	            c.removeMouseListener(c.getMouseListeners()[0]);
    	            c.removeMouseMotionListener(c.getMouseMotionListeners()[0]);
    	            c.setEnabled(false);
    	        }
    	    
    	    }  
    	  
    	    private boolean verificaPalavra(Container p) {  // this method verify if the word is right, that's where i'm having problems
    	        String palavra = "";
    	        String Palavra = "AGUA"; // i want use an array here
    	        for(Component c:p.getComponents())  
    	            palavra += ((JLabel)c).getText();
    
    	        return palavra.equals(Palavra); // i used contains method, but i had the same problem
    	    }
    	    	      
    	    private void mudaPanel(Component c) {  
    	        Container atual = c.getParent();  
    	      
    	        r2d.setRect(atual.getX()+c.getX(), atual.getY()+c.getY(), c.getWidth(),c.getHeight());  
    	  
    	        for(JPanel novo:panels){  
    	            if(novo!=atual && r2d.intersects(novo.getBounds())){  
    	                atual.remove(c);  
    	                novo.add(c);  
    	                c.setLocation(  
    	                        (int)(r2d.getX()-novo.getX()),  
    	                        (int)(r2d.getY()-novo.getY())  
    	                        );  
    	                break;  
    	            }  
    	        }  
    	    }
    	}  
    }
    Last edited by thg99; 04-28-2012 at 10:35 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Java Game Programming

    Hello and welcome.

    You state that you are having problems in a section of your program, but you should tell us more -- for instance exactly *what* problems are you having? The details are important, and you can't assume that we understand just what each part of your code is trying to do unless you tell us.

  3. #3
    thg99 is offline Member
    Join Date
    Apr 2012
    Location
    Sao Paulo, Brazil
    Posts
    2
    Rep Power
    0

    Default Re: Java Game Programming

    Quote Originally Posted by Fubarable View Post
    Hello and welcome.

    You state that you are having problems in a section of your program, but you should tell us more -- for instance exactly *what* problems are you having? The details are important, and you can't assume that we understand just what each part of your code is trying to do unless you tell us.
    Thanks Fuberable. When I run the code Eclipse try to debug the code. I'm using Drag and Drop, and the user drag labels from one panel to another, and at each release of label the method verificaPalavra() - verify word - is executed, and if the letter of the dropped label is at the right position the Mouse Listener and Mouse Motion Listener are both removed from that label.

Similar Threads

  1. Programming a Board Game
    By makanti in forum New To Java
    Replies: 7
    Last Post: 03-10-2009, 03:32 AM
  2. The words *game* in java programming :confused:
    By ibmzz in forum Advanced Java
    Replies: 1
    Last Post: 01-23-2008, 10:23 AM
  3. Programming a Game?
    By gt123 in forum New To Java
    Replies: 4
    Last Post: 01-01-2008, 01:41 PM
  4. java game programming
    By gammaman in forum New To Java
    Replies: 1
    Last Post: 12-25-2007, 02:01 AM
  5. Replies: 1
    Last Post: 08-07-2007, 06:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •