Problemas com JFrame

4 respostas
matheuslmota

E ae galera blza? Eu sou iniciante em Java e to desenvolvendo um jogo Tetris pa Java e jah to na fase final
Eu tento inserir um menu e o jogo da erro.

Eis uma parte do código

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

public class Jogo extends JFrame implements KeyListener, Pecavel, Serializable{
	protected int i, j, x, y, a = -3, b = -2, c = -1, d = 0, z = 4, cont = 0, g = 0, pontos = 0, w = 300, rota = 0;
	protected boolean k = true;
	public int linha;
	public int tabu[][] = new int[15][30];
	public int guarda[][] = new int[15][30];
	public int tabuaux[][] = new int[15][30];
	public int memoria[][] = new int[15][30];
	Quad qua[][] = new Quad[15][30];
	Tela tela;
	Tela2 tela2;
	Moldura moldura;
	Peca pp;
	Peca mem;
	Peca aux;
	Quadro quadro;
	JLabel label;
	JLabel label2;
	JLabel label3;
	Font BigFont;
	GameOver gameover;
	JMenuBar jogo;
	JMenu menu;
	JMenuItem abrir;
	JMenuItem salvar;
	JMenuItem novo;
	
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		Jogo j = new Jogo();
		j.motor();
	}
	public Jogo(){
		super("Tetris Matheus");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		BigFont = new Font("sanserif", Font.BOLD, 26);
		tela = new Tela();
		tela2 = new Tela2();
		moldura = new Moldura();
		moldura.setSize(700, 670);
		moldura.setLocation(0, 0);
		tela2.setLayout(null);
		tela2.setLocation(450, 0);
		tela2.setSize(1000, 1000);
		label = new JLabel();
		label.setText("Pontuação");
		label.setSize(200, 50);
		label.setLocation(20, 400);
		label.setFont(BigFont);
		label2 = new JLabel();
		label2.setSize(200, 50);
		label2.setLocation(20, 500);
		label2.setFont(BigFont);
		tela2.add(label);
		tela2.add(label2);
		tela.add(tela2);
		tela.add(moldura);
		quadro = new Quadro();
		quadro.setLocation(0, 200);
		quadro.setSize(200, 300);
		quadro.setLayout(null);
		tela2.add(quadro);
		tela.setLayout(null);
		tela.setSize(700, 700);
		tela.setLocation(0, 30);
		abrir = new JMenuItem("Abrir");
		salvar = new JMenuItem("Salvar");
		novo = new JMenuItem("Novo");
		menu = new JMenu("Jogo");
		jogo = new JMenuBar();
		menu.add(novo);
		menu.add(abrir);
		menu.add(salvar);
		jogo.add(menu);
		add(BorderLayout.NORTH, menu);
		add(BorderLayout.CENTER, tela);
		setSize(700, 700);
		setVisible(true);
		addKeyListener(this);
		GameOver gameover = new GameOver();
	}
}

O problema aparece quando eu tento inserir um menu no jogo.
Quando eu insiro o menu o jogo da erro (não erro de bug, mas um erro de funcionamento, de logica).
Eu crio um JFrame, e crio uma tela e um menu.
A tela extende JPanel e nela eh q ocore toda a ação do jogo. Ai, usando o gerenciador BorderLayout eu tento inserir o menu na região Norte e a tela no centro. Ocorre como se o painel da tela não tivesse sido reconhecido e a imagem do tetris fica estranha.

Eis o código completo do jogo:

Classe do Jogo:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

public class Jogo extends JFrame implements KeyListener, Pecavel, Serializable{
	protected int i, j, x, y, a = -3, b = -2, c = -1, d = 0, z = 4, cont = 0, g = 0, pontos = 0, w = 300, rota = 0;
	protected boolean k = true;
	public int linha;
	public int tabu[][] = new int[15][30];
	public int guarda[][] = new int[15][30];
	public int tabuaux[][] = new int[15][30];
	public int memoria[][] = new int[15][30];
	Quad qua[][] = new Quad[15][30];
	Tela tela;
	Tela2 tela2;
	Moldura moldura;
	Peca pp;
	Peca mem;
	Peca aux;
	Quadro quadro;
	JLabel label;
	JLabel label2;
	JLabel label3;
	Font BigFont;
	GameOver gameover;
	JMenuBar jogo;
	JMenu menu;
	JMenuItem abrir;
	JMenuItem salvar;
	JMenuItem novo;
	
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		Jogo j = new Jogo();
		j.motor();
	}
	public Jogo(){
		super("Tetris Matheus");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		BigFont = new Font("sanserif", Font.BOLD, 26);
		tela = new Tela();
		tela2 = new Tela2();
		moldura = new Moldura();
		moldura.setSize(700, 670);
		moldura.setLocation(0, 0);
		tela2.setLayout(null);
		tela2.setLocation(450, 0);
		tela2.setSize(1000, 1000);
		label = new JLabel();
		label.setText("Pontuação");
		label.setSize(200, 50);
		label.setLocation(20, 400);
		label.setFont(BigFont);
		label2 = new JLabel();
		label2.setSize(200, 50);
		label2.setLocation(20, 500);
		label2.setFont(BigFont);
		tela2.add(label);
		tela2.add(label2);
		tela.add(tela2);
		tela.add(moldura);
		quadro = new Quadro();
		quadro.setLocation(0, 200);
		quadro.setSize(200, 300);
		quadro.setLayout(null);
		tela2.add(quadro);
		tela.setLayout(null);
		tela.setSize(700, 700);
		tela.setLocation(0, 30);
		/*abrir = new JMenuItem("Abrir");
		salvar = new JMenuItem("Salvar");
		novo = new JMenuItem("Novo");
		menu = new JMenu("Jogo");
		jogo = new JMenuBar();
		menu.add(novo);
		menu.add(abrir);
		menu.add(salvar);
		jogo.add(menu);
		add(BorderLayout.NORTH, menu);*/
		add(BorderLayout.CENTER, tela);
		setSize(700, 700);
		setVisible(true);
		addKeyListener(this);
		GameOver gameover = new GameOver();
	}
	public int getA() {
		return a;
	}

	public void setA(int s) {
		if(s<19){
			this.a = s;
		}
	}

	public int getB() {
		return b;
	}

	public void setB(int s) {
		if(s<20){
			this.b = s;
		}
	}
	
	@Override
	public int getC() {
		return c;
	}
	
	@Override
	public void setC(int s) {
		if(s<21){
			this.c = s;
		}	
	}
	@Override
	public int getD() {
		return d;
	}

	@Override
	public void setD(int s) {
		if(s<22){
			this.d = s;
		}
		
	}

	public int getZ() {
		return z;
	}

	public void setZ(int s) {
		if(s>0 & s<14 - pp.q){
			this.z = s;
		}
	}
		
	public void pontuacao(){
		for (i = 1; i < 30; i++) {
			for (j = 1; j < 14; j++) {
				if(guarda[j][i] != 0){
					g++;
				}
			}
			if(g == 13){
				linha = i;
				atualiza();
				g = 0;
			}else{
				g = 0;
			}
		}
	}
	public void fimdejogo(){
		removeAll();
		gameover = new GameOver();
		this.add(gameover);
	}
	public void atualiza(){
		for (int r = linha; r > 0; r--) {
			if(r == linha){
				for (int s = 0; s < 14; s++) {
					guarda[s][r] = 0;
				}
			}else{
				for (int s = 0; s < 14; s++) {
					tabuaux[s][r+1] = guarda[s][r];
					guarda[s][r] = 0;
					guarda[s][r+1] = tabuaux[s][r+1];
				}
			}
		}
		pontos = pontos + 100;
		label2.setText(""+pontos);
		if(pontos%300 == 0){
			w -=100;
		}
	}
	
	class Quad extends JPanel{
		
	}
	class Quad1 extends Quad {
		public Quad1() {
			setSize(30, 30);
			setBackground(Color.yellow);
		}

	}

	class Quad2 extends Quad {
		public Quad2() {
			setSize(30, 30);
			setBackground(Color.RED);
		}

	}
	class Quad3 extends Quad {
		public Quad3() {
			setSize(30, 30);
			setBackground(Color.BLUE);
		}

	}
	class Quad4 extends Quad {
		public Quad4() {
			setSize(30, 30);
			setBackground(Color.GREEN);
		}

	}
	class Quad5 extends Quad {
		public Quad5() {
			setSize(30, 30);
			setBackground(Color.ORANGE);
		}

	}
	public boolean valida(){
		if(pp.tam == 1){
			return true;
		}else{
			return false;
		}
	}
	public void motor() {
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				Peca pecas[] = {new Peca1(Jogo.this, Jogo.this), new Peca2(Jogo.this, Jogo.this),  new Peca3(Jogo.this, Jogo.this), new Peca4(Jogo.this, Jogo.this), new Peca5(Jogo.this, Jogo.this) }; 
				while(true){
					if(cont == 0){
						int aa = (int) (Math.random()*5);
						pp = pecas[aa];
						int ab = (int) (Math.random()*5);
						mem = pecas[ab];
						for (int i = 0; i < 21; i++) {
							if(i == 20){
								pp.mover(k);
								desenhar();
								pontuacao();
								for (int j = 0; j < 15; j++) {
									for (int j2 = 0; j2 < 30; j2++) {
										switch (tabu[j][j2]){
										case 1:
											tabu[j][j2] = 0;
											guarda[j][j2] = 1;
											break;
										case 2:
											tabu[j][j2] = 0;
											guarda[j][j2] = 2;
											break;
										case 3:
											tabu[j][j2] = 0;
											guarda[j][j2] = 3;
											break;
										case 4:
											tabu[j][j2] = 0;
											guarda[j][j2] = 4;
											break;
										case 5:
											tabu[j][j2] = 0;
											guarda[j][j2] = 5;
											break;
										}
									}
								}
							}else{
								setA(a+1);
								setB(b+1);
								setC(c+1);
								setD(d+1);
								if(pp.verifica11()){
									pp.mover(k);
									desenhar();
									try {
									
										Thread.sleep(w);
										
									} catch (Exception ex) {
									}
									limpa();
								}else{
									setA(a-1);
									setB(b-1);
									setC(c-1);
									setD(d-1);
									pp.guarda(k);
									break;
								}
							}
						}
					}else{
						pp = mem;
						int ab = (int) (Math.random()*5);
						mem = pecas[ab];
						for (int i = 0; i < 21; i++) {
							if(i == 20){
								pontuacao();
									pp.mover(k);
								desenhar();
								for (int j = 0; j < 15; j++) {
									for (int j2 = 0; j2 < 30; j2++) {
										switch (tabu[j][j2]){
										case 1:
											guarda[j][j2] = 1;
											break;
										case 2:
											guarda[j][j2] = 2;
											break;
										case 3:
											guarda[j][j2] = 3;
											break;
										case 4:
											guarda[j][j2] = 4;
											break;
										case 5:
											tabu[j][j2] = 0;
											guarda[j][j2] = 5;
											break;
										}
									}
								}
							}else{
								setA(a+1);
								setB(b+1);
								setC(c+1);
								setD(d+1);
								if(pp.verifica11()){
									pp.mover(k);
									desenhar();
									try {
									
										Thread.sleep(w);
										
									} catch (Exception ex) {
									}
									limpa();
								}else{
									setA(a-1);
									setB(b-1);
									setC(c-1);
									setD(d-1);
									pp.guarda(k);
									break;
								}
							}
						}
					}
					a = -3;
					b = -2;
					c = -1;
					d = 0;
					z = 4;
					k = true;
					cont++;
					rota = 0;
					if(cont == 3){
					}
				}
			}

			private void limpa() {
				for (int j = 0; j < 15; j++) {
					for (int j2 = 0; j2 < 30; j2++) {
							tabu[j][j2] = 0;
					}
				}
				tela.removeAll();
			}
		}).start();
	}

	public void desenhar() {
		pontuacao();
		tela.add(tela2);
		tela.add(moldura);
		mem.getFigura().setLocation(40, 40);
		mem.getFigura().setSize(120, 160);
		quadro.removeAll();
		quadro.add(mem.getFigura());
		for (int i = 0; i < 15; i++) {
			for (int j = 0; j < 30; j++) {
				QuadGuarda(i, j);
			}
		}
		for (int i = 0; i < 15; i++) {
			for (int j = 0; j < 30; j++) {
				QuadTabu(i, j);
			}
		}
		tela.repaint();
	}

	private void QuadTabu(int i, int j) {
		switch(tabu[i][j]){
		case 1:
			x = i * 30;
			y = j * 30;
			Quad1 q1 = new Quad1();
			q1.setLocation(x, y);
			qua[i][j] = q1;
			tela.add(qua[i][j]);
			break;
		case 2:
			x = i * 30;
			y = j * 30;
			Quad2 q2 = new Quad2();
			q2.setLocation(x, y);
			tela.add(q2);
			break;
		case 3:
			x = i * 30;
			y = j * 30;
			Quad3 q3 = new Quad3();
			q3.setLocation(x, y);
			tela.add(q3);
			break;
		case 4:
			x = i * 30;
			y = j * 30;
			Quad4 q4 = new Quad4();
			q4.setLocation(x, y);
			tela.add(q4);
			break;
		case 5:
			x = i * 30;
			y = j * 30;
			Quad5 q5 = new Quad5();
			q5.setLocation(x, y);
			tela.add(q5);
			break;
		}
	}

	public void QuadGuarda(int i, int j) {

		switch(guarda[i][j]){
		case 1:
			x = i * 30;
			y = j * 30;
			Quad1 q = new Quad1();
			q.setLocation(x, y);
			qua[i][j] = q;
			tela.add(qua[i][j]);
			break;
		case 2:
			x = i * 30;
			y = j * 30;
			Quad2 q2 = new Quad2();
			q2.setLocation(x, y);
			qua[i][j] = q2;
			tela.add(qua[i][j]);
			break;
		case 3:
			x = i * 30;
			y = j * 30;
			Quad3 q3 = new Quad3();
			q3.setLocation(x, y);
			qua[i][j] = q3;
			tela.add(qua[i][j]);
			break;
		case 4:
			x = i * 30;
			y = j * 30;
			Quad4 q4 = new Quad4();
			q4.setLocation(x, y);
			qua[i][j] = q4;
			tela.add(qua[i][j]);
			break;
		case 5:
			x = i * 30;
			y = j * 30;
			Quad5 q5 = new Quad5();
			q5.setLocation(x, y);
			tela.add(q5);
			break;
		}
	}

	class Tela extends JPanel {

		public void paintComponent(Graphics g) {
			g.setColor(Color.black);
			g.fillRect(0, 0, 450, 630);
		}
	}
	class Moldura extends JPanel{
		public void paintComponent(Graphics g){
			g.setColor(Color.gray);
			g.fillRect(0, 0, 30, 630);
			g.fillRect(420, 0, 80, 630);
			g.fillRect(0, 0, 700, 30);
			g.fillRect(0, 630, 700, 100);
		}
	}
	class Tela2 extends JPanel {

		public void paintComponent(Graphics g) {
			g.setColor(Color.gray);
			g.fillRect(0, 0, 250, 630);
		}
	}
	class GameOver extends JPanel{
		public void paintComponent(Graphics g) {
			g.setColor(Color.BLACK);
			g.fillRect(0, 0, this.getWidth(), this.getHeight());
			g.setColor(Color.WHITE);
			g.drawString("Fim de Jogo", 200, 200);
		}
	}
	class Quadro extends JPanel {

		public void paintComponent(Graphics g) {
			g.setColor(Color.WHITE);
			g.fillRect(0, 0, 200, 200);
		}
	}

	public void keyPressed(KeyEvent e) {
		if(e.getKeyCode() == 32){
			if(k == true){
				k = false;
			}else{
				k = true;
			}
		}
		if (e.getKeyCode() == 37) {
			setZ(z-1);
			tela.repaint();
			
		}
		if (e.getKeyCode() == 39) {
			if(pp.verifica21()){
				setZ(z+1);
				tela.repaint();
			
			}
		}
		if (e.getKeyCode() == 40) {
			if(pp.verifica31()){
				setA(a+1);
				setB(b+1);
				setC(c+1);
				setD(d+1);
				tela.repaint();
			}		
		}
		if(e.getKeyCode() == 65){
			fimdejogo();
		}
	}

	public void keyReleased(KeyEvent e) {

	}
	
	public void keyTyped(KeyEvent e) {
		
	}
	

	public void guarda() {
		
	}

	public void mov() {
		
	}

	public void verifica1() {
		
	}
	
	public void setTabu(int t, int u, int valor){
		if(t>=0 && t<15){
			if(u>0 && u<30){
				tabu[t][u] = valor;
			}
		}
	}
	
	public void setGuarda(int t, int u, int valor){
		if(t>=0 && t<15){
			if(u>0 && u<30){
				guarda[t][u] = valor;
			}
		}
	}
	class Novo implements ActionListener{
		public void actionPerformed(ActionEvent e){
			Jogo game = new Jogo();
			game.motor();
		}
	}
	class Salvar implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			try{
				ObjectOutputStream t = new ObjectOutputStream(new FileOutputStream("game.txt"));
				t.writeObject(j);
			}catch(Exception ex){
				
			}
		}
	}
	class Abrir implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			try{
				ObjectInputStream t = new ObjectInputStream(new FileInputStream("game.txt"));
				Jogo aj = (Jogo) t.readObject();
				aj.motor();
			}catch(Exception ex){
				
			}
		}
	}
}

Se alguem puder me ajudar eu vou agradecer muito.
Obrigado a todos pela atenção :D

4 Respostas

aconstantino

Oi Matheus,

na verdade vc nao adiciona o menu e sim seta ele no JFrame através do método setJMenuBar(JMenuBar menubar)

acho que resolve

Abrass

matheuslmota

Cara eu tentei fazer isso mas nao deu muito certo nao.
O problema eh assim:
Meu jogo foi feito em cima de uma matriz. Então eu configurei uma matriz inicialmente como todos os elementos como nulos e as peças iam se movimentando na tela conforme eu mudasse o valor do numero na matriz.
Acontesse que uando eu insiro o menu o jogo passa a se comportar de maneira estranha, a peça vai descendo e deixando uma marca na tela.
O código fonte do arquivo do jogo segue em anexo
Vlew ae pela atenção;
:smiley:

mario.fts

acho que o seu problema seria limpar a tela antes de mover a peça, pra limpar a posição anterior dela.

matheuslmota

Pois eh Mario eu faço isso e funciona normalmente, soh q estranhamente quando eu adiciono o menu o jogo deixa de apagar a posição anterior. Sem falar q a tela do Tetris eh preta e quando eu adiciono o menu ela fica como se nao tivesse nada no frame, soh q o jogo roda com o problema q eu disse, nao apagando a posição anterior. Acho q o meu problema eh d BIOS (Burro Ignorante Operando o Sistema)…
Q coisa estranh esse erro neh?

Criado 10 de março de 2010
Ultima resposta 10 de mar. de 2010
Respostas 4
Participantes 3