Ola pessoa !!
Estou tentando colcar um JLabel sobre outro,
no meu codigo aprece um circulo laraja fora do “quadro” que crie e gostaria que
aparecesse dentro, porque estou montando um jogo de figuras geometricas.
Obrigado !!!
package jog;
// Importando as bibliotecas necessarias para execução do programa
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Janela {
final static boolean shouldFill = true;
final static boolean shouldWeightX = true;
final static boolean RIGHT_TO_LEFT = false;
public static void addComponentsToPane(Container pane) {
if (RIGHT_TO_LEFT) {
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
JButton button;
JLabel label;
JPanel pam;
pane.setLayout(new GridBagLayout());
GridBagConstraints comp = new GridBagConstraints();
label = new JLabel("JOG");
comp.fill = GridBagConstraints.PAGE_START;
comp.gridx = 3;
//comp.gridy = 0;
pane.add(label, comp);
label = new JLabel( QUADRO() );
comp.fill = GridBagConstraints.HORIZONTAL;
comp.ipady = 0; // Importancia do label para o programa
pane.add(label, comp);
label = new JLabel( CIRCULO() );
pane.add(label, comp);
label = new JLabel("TEMPO:");
comp.fill = GridBagConstraints.HORIZONTAL;
comp.ipady = 0; // Redefinindo para padrão
comp.weighty = 0.0; // Solicita espaço extra vertical
comp.anchor = GridBagConstraints.PAGE_END; // Fundo do espaço
comp.insets = new Insets(10,0,0,0); // Inserindo espaço em branco
comp.gridx = 2; // Alinhado com o botão 2
comp.gridwidth = 2; // 2 colunas de largura
comp.gridy = 2; // Terceira fila
pane.add(label, comp);
}
//Metodo para criar quadro onde as figuras geometricas serão mostradas
private static ImageIcon QUADRO() {
int width=600, height=500; // Tamanho do quadro
BufferedImage buffer = new BufferedImage( width, height, BufferedImage.TYPE_INT_RGB );
Graphics g = buffer.createGraphics();
g.setColor( Color.BLACK );
g.fillRect( 0, 0, width, height );
g.setColor( Color.WHITE);
g.fillRect( 2, 2, 596, 496 );
return new ImageIcon( buffer );
}
private static ImageIcon CIRCULO() {
BufferedImage buffer = new BufferedImage( 100, 100, BufferedImage.TRANSLUCENT );
Graphics g = buffer.createGraphics();
g.setColor( Color.ORANGE );
g.fillOval( 0, 0, 100, 100 );
return new ImageIcon( buffer );
}
private static void createAndShowGUI() {
//Cria e configura a janela
JFrame frame = new JFrame("JOG - Jogo da Geometria");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Configura o conteúdo do painel
addComponentsToPane(frame.getContentPane());
//Mostra a janela.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater (new Runnable()
{ public void run()
{ createAndShowGUI();}
});
}
}