I am having trouble updating my "panel" when I want to change it's background.
In my program, whenever you reach a certain number of clicks in that panel, the panel will change it's background but I have trouble doing so.
here are my codes.
this is the constructor/gui class
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;;
public class demo {
JFrame frame = new JFrame ("Idle Game Test!");
JPanel backGroundPanel = new JPanel ();
static JPanel statusPanel = new JPanel();
JPanel buttonPanel = new JPanel ();
JPanel bigPanel = new JPanel ();
static JTextArea message = new JTextArea (34,43);
//MessageDisplay msg = new MessageDisplay ();
//Constructor
demo () {
//gets the dimension of the screen
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
backGroundClass bgc = new backGroundClass ();
frame.setSize (850,700);
frame.setResizable (false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible (true);
//get size in pixels
int w = frame.getSize().width;
int h = frame.getSize().height;
int x = (dim.width - w)/2;
int y = (dim.height - h)/2;
//set the location
frame.setLocation (x,y);
backGroundPanel.setLayout (null);
backGroundPanel.setBackground (Color.DARK_GRAY);
frame.add (backGroundPanel);
statusPanel.setSize(250, 600);
statusPanel.setLocation (15,55);
statusPanel.add(bgc.panel);
statusPanel.addMouseListener(new mouseEvent ());
backGroundPanel.add (statusPanel);
buttonPanel.setSize (550,100);
buttonPanel.setLocation (280,555);
backGroundPanel.add (buttonPanel);
JScrollPane scroll = new JScrollPane (message, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
DefaultCaret caret = (DefaultCaret)message.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
message.setWrapStyleWord(true);
message.setLineWrap(true);
message.setBackground (Color.gray);
message.setEditable (true);
message.setForeground(Color.white);
message.setFont(new Font ("Georgia", Font.PLAIN, 12));
message.setEditable(false);
//message.setSize (500,500);
bigPanel.add(scroll);
bigPanel.setSize (550, 490);
bigPanel.setLocation (280,55);
backGroundPanel.add (bigPanel);
}
}
The class that makes a panel w/background
import java.awt.*;
import javax.swing.*;
@SuppressWarnings("serial")
class backGroundClass extends JPanel {
//ImagePanel panel = new ImagePanel(new ImageIcon("images/background.png").getImage());
int controlNumber = 0 ;
String []imagePath= {"Image1Ulquiora.jpg","Image2Diva.jpg","Image2DivaSized.jpg","Image3GirlInSword.jpg"};
ImageIcon icon = new ImageIcon (getClass().getResource(imagePath[controlNumber]));
ImagePanel panel = new ImagePanel (icon.getImage());
}
@SuppressWarnings("serial")
class ImagePanel extends JPanel {
private Image img;
public ImagePanel(String img) {
this(new ImageIcon(img).getImage());
}
public ImagePanel(Image img) {
this.img = img;
Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
}
public void paintComponent(Graphics g) {
g.drawImage(img, 0, 0, null);
}
}
and the action event that suppose to update the panel (im not sure if this is right or what XD)
import java.awt.event.*;
public class mouseEvent implements MouseListener {
backGroundClass bgc = new backGroundClass ();
@Override
public void mouseClicked(MouseEvent e) {
if (!MainProg.flag){
demo.message.append(Integer.toString(e.getClickCount()));
}
if (e.getClickCount() > 5) {
bgc.controlNumber = 3;
bgc.panel.repaint(); //this does not work :<
demo.statusPanel.revalidate(); //this also
demo.statusPanel.repaint(); //same
}
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
I tried the repaint both on the panel that created w/background image AND the panel holds the panel that creates the image (redundant XD) but unfortunately none of them works.
also is there other way to access those static variables? the way i implemented them works but i do think there are other ways that are more suitable or better but i cant figure out. I hope someone could help me thanks in advance and more power!