Boa noite colegas do guj.
Estou a fazer um menu de jogo só que as imagens não são desenhadas na tela por algum motivo que não consigo identificar então venho pedir ajuda a vocês.
Agradeço desde já.
Main
[code]package menus;
import javax.swing.JFrame;
public class Arkanoid extends JFrame {
final static int WINDOW_WIDTH = 1024;
final static int WINDOW_HEIGHT = 660;
public Arkanoid() {
this.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(new MenuVisual()); // necessário implements JPanel em Menu
this.setLocationRelativeTo(null);
this.setTitle("Arkanoid");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(null);
this.setVisible(true);
}
public static void main(String[] args) {
// TODO code application logic here
Arkanoid arkanoid = new Arkanoid();
}
}
[/code]Menu visual[code]package menus;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
class MenuVisual extends JPanel implements KeyListener {
MenuLogico logicMenu;
BufferedImage backBuffer;
public MenuVisual() {
this.initComponents();
this.addComponents();
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
// desenharGraficos();
actionPerformed(null);
try {
Thread.sleep(1000 / 30);
} catch (Exception e) {
e.getStackTrace();
}
}
}
}).start();
}
private void initComponents() {
logicMenu = new MenuLogico(4);
logicMenu.itens[0] = "START GAME";
logicMenu.itens[1] = "SCORING LEADERS";
logicMenu.itens[2] = "OPTIONS";
logicMenu.itens[3] = "EXIT";
backBuffer = new BufferedImage(Arkanoid.WINDOW_WIDTH, Arkanoid.WINDOW_HEIGHT, BufferedImage.TYPE_INT_RGB);
logicMenu.bbg = backBuffer.getGraphics();
}
private void addComponents() {
this.addKeyListener(this);
}
public void actionPerformed(ActionEvent e) {
repaint();
}
@Override
public void paintComponent(Graphics g) {
desenharGraficos(g);
}
private void desenharGraficos(Graphics g) {
// Graphics g = getGraphics();
Graphics bbg = backBuffer.getGraphics();
bbg.setColor(Color.WHITE);
g.fillRect(0, 0, Arkanoid.WINDOW_WIDTH, Arkanoid.WINDOW_HEIGHT);
logicMenu.desenharMenu();
this.cenario();
g.drawImage(backBuffer, 0, 0, this);
}
private void cenario() {
Graphics bbg = backBuffer.getGraphics();
bbg.setFont(new Font("Calibri", Font.BOLD, 15));
if (logicMenu.cenario == 0) {
bbg.setColor(Color.BLACK);
bbg.fillRect(0, 0, Arkanoid.WINDOW_WIDTH, Arkanoid.WINDOW_HEIGHT);
bbg.drawString("Você escolheu start game", 100, 200);
} else if (logicMenu.cenario == 1) {
bbg.setColor(Color.BLACK);
bbg.fillRect(0, 0, Arkanoid.WINDOW_WIDTH, Arkanoid.WINDOW_HEIGHT);
bbg.drawString("Você escolheu scoring leaders", 100, 200);
} else if (logicMenu.cenario == 2) {
bbg.setColor(Color.BLACK);
bbg.fillRect(0, 0, Arkanoid.WINDOW_WIDTH, Arkanoid.WINDOW_HEIGHT);
bbg.drawString("Você escolheu options", 100, 200);
} else if (logicMenu.cenario == 3) {
System.exit(0);
}
}
public void keyTyped(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void keyPressed(KeyEvent e) {
logicMenu.controlar(e);
logicMenu.voltarAoMenu(e);
}
public void keyReleased(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
[/code]Lógica do menu[code]package menus;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
public class MenuLogico {
int cenario = -1, itemSelecionado = 0;
boolean ativo;
String itens[];
Graphics bbg;
public MenuLogico(int numeroDeItens) {
itens = new String[numeroDeItens];
this.ativo = true;
}
public void controlar(KeyEvent e) {
if (ativo) {
controlarMenu(e);
}
}
private void controlarMenu(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP) {
itemSelecionado--;
} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
itemSelecionado++;
}
// controla o limite do menu
if (itemSelecionado >= itens.length) {
itemSelecionado = 0;
} else if (itemSelecionado < 0) {
itemSelecionado = itens.length - 1;
}
// desativa controlar caso algum itens seja escolhido
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
cenario = itemSelecionado;
ativo = false;
}
}
void voltarAoMenu(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
this.cenario = -1; // sai do cenário selecionado
this.ativo = true; // permite utilizar o método que controla o menu
}
}
void desenharMenu() {
bbg.setFont(new Font("Calibri", Font.BOLD, 15));
for (int i = 0; i < itens.length; i++) {
if (itemSelecionado == i) {
bbg.setColor(Color.red);
bbg.drawString(itens[i], 100, 200);
} else {
bbg.setColor(Color.black);
bbg.drawString(itens[i], 100, 200);
}
}
}
}
[/code]