0

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!

  • Calling `super.paintComponent` before doing any custom painting might be a good start. Also, it's generally a better idea to override `getPreferred/Minimum/MaximumSize` – MadProgrammer Apr 22 '15 at 10:09
  • You never paint anything different based on the value `controlNumber`, which should not accessible directly, but should be via setter/getter... – MadProgrammer Apr 22 '15 at 10:10
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Apr 22 '15 at 10:19
  • uhm. Ok. I'm really lost. I don't get anything out of what you said guys? :( @MadProgrammer can you give me an example? that would help me better in understanding XD – John Cedrick Agapito Apr 22 '15 at 10:52
  • *"I don't get anything out of what you said guys?"* If you don't even understand enough to ask specific, directed questions, I doubt you have the skills to be a programmer. – Andrew Thompson Apr 22 '15 at 10:57
  • it's not that I HAVE or DONT have the skills to be programmer or what. what i dont understand is what you are trying to say mr. @AndrewThompson if your talking about specific and/or directed questions I believe my question was direct enough? and i dont mean anything MEAN about that :) – John Cedrick Agapito Apr 22 '15 at 11:31
  • I meant specific and directed questions about what we wrote! A programmer (at least not a successful one) doesn't just throw their hands up in the air and out when they don't understand something that's said to them, they might respond with *"I don't understand what you mean by X when it's applied to Y. How does X affect Y?"*. They **don't** respond with *"I don't get anything out of what you said guys?"*. – Andrew Thompson Apr 22 '15 at 11:40
  • well then i'll apologize for that maybe it's because we have a different culture that's why we misunderstood each other on the way we converse. also if you ask me with "i don't get anything out of what you said" I do understand that the person asks that does not understand me at all. and as for me with what I said is that I don't get really what you wrote Like what it is for and how/why can it help me. and we are getting out of topic here mr.@AndrewThompson I dont mind prolonging this but let's end this with this :) and I really appreciate your time and effort thank you :) – John Cedrick Agapito Apr 22 '15 at 11:55
  • and oh btw mr.@AndrewThompson I did checked http://stackoverflow.com/questions/19209650/example-images-for-code-and-mark-up-qas and I really dont have Idea on what or how would the thread help me? – John Cedrick Agapito Apr 22 '15 at 11:58
  • Your code does not compile. What is `MainProg`? – user1803551 Apr 23 '15 at 11:27
  • Also, `getClickCount` is the click count for a single event. The value is 2 for a double-click. If you set it to 5 then you want the user to quickly press 5 times, which is very unusual. – user1803551 Apr 23 '15 at 11:46

0 Answers0