Reputation: 77
package BlackjackPanels;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
public class MainPanel extends JFrame implements ActionListener {
private JPanel background;
public MainPanel() {
super("Alan's Blackjack");
setDefaultCloseOperation(EXIT_ON_CLOSE);
background = new JPanel()
{
@Override
protected void paintComponent(Graphics g)
{
try
{
BufferedImage image = ImageIO.read(new File("C:/Users/Ssangwook/Desktop/programming/javafiles/Blackjack/src/BlackjackImages/blackjackTableResized.jpg"));
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
}
catch (IOException e)
{
e.printStackTrace();
}
}
};
background.setPreferredSize(new Dimension(1000,500));
add(background);
pack();
setLocationRelativeTo(null);
setResizable(false);
//isRunning();
setVisible(true);
}
public void isRunning() {
background.setLayout(new BorderLayout(10, 10));
DealerPanel dealer=new DealerPanel();
background.add(dealer, BorderLayout.LINE_START);
repaint();
}
@Override
public void actionPerformed(ActionEvent a) {
}
public static void main(String [] args) {
MainPanel game=new MainPanel();
game.isRunning();
}
}
package BlackjackPanels;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
public class DealerPanel extends JPanel {
private JButton hit = new JButton("Hit");
public DealerPanel() {
super();
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
setPreferredSize(new Dimension(100,100));
setOpaque(false);
setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.DARK_GRAY), "DEALER"));
}
protected void askBets() {
}
public void addListener(ActionListener a) {
hit.addActionListener(a);
}
}
Hello, I'm just getting started on making blackjack with GUI. However, I am running into a problem and I was really hoping somebody could explain it to me.
The problem seems to be with the repaint() inside the isRunning() method. Whenever I run the program on Eclipse, I get the DealerPanel panel on my background only half of the times. The other half times that don't show the panel, I have to minimize the JFrame window and bring it back up again and the panel for DealerPanel is somehow displayed again.
I did a little search on my own and found out that repaint() merely requests the AWT thread to call update() which then calls paint() and is therefore not a direct call to paint. Also, repaint() can be problematic since repaint() only schedules for paint() and returns immediately. Could this potentially have anything to do with my problem?
To summarize, my panel for background is always showing. However, my panel for Dealer Panel does not always show when I initially run the program. The times that the Dealer panel don't show, I hide and bring up the window and the panel is displayed. Anybody knows the reason behind this erratic behavior?
Upvotes: 1
Views: 192
Reputation: 347184
super.paintComponent(g);
should always be called, normally first, regardless of what else you might be doing in the paintComponent
methodsetPreferred/Minimum/MaximumSize
revalidate
(or invalidate
, validate
on JFrame
) on the top level container so that the container hierarchy is marked as needing to be relaid out, repaint
won't do this.Upvotes: 2