0

Here what I want is that when the Buttons are clicked the shapes should appear immediately, but the shapes does not appear unless the JFrame is minimized and restored.

package tutorial;
public class Tutorial extends JPanel{
JButton b1,b2,b3,b4,b5;

boolean nodeA, nodeB,communicate, key, acknowledge = false;
public  Tutorial(){

    b1 = new JButton("Node A");
    add(b1);
    b2 = new JButton("Node B");
    add(b2);
    b3 = new JButton("Communicate");
    add(b3);
    b4 = new JButton("send key");
    add(b4);
    b5 = new JButton("Request B");
    add(b5);
}

public void paintComponent(Graphics g){

    super.paintComponent(g);
    g.setColor(Color.black);
    g.fillRect(300, 100,100, 50);
    g.setColor(Color.red);
    g.drawString("KDC/KMC",320,130);


    b1.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e) {
            nodeA = true;
        }
    });

    if(nodeA == true){
        g.setColor(Color.green);
        g.fillOval(100, 300, 70, 70);
        g.setColor(Color.red);
        g.drawString("Node A", 113, 340); 
        g.drawString("Node A added",150,220);
        g.drawLine(150, 300, 300, 150);
    }

    b2.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e) {
            nodeB = true;    
        }
     });
    if(nodeB == true){
        g.setColor(Color.green);
        g.fillOval(530, 300, 70, 70);
        g.setColor(Color.red);
        g.drawString("Node B",545,340);
        g.drawString("Node B added",473,220);
        g.drawLine(550, 300, 400, 150); 
    }

    b3.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e) {
            if(nodeA == true && nodeB == true)
                communicate = true;
        }
     });
    if(communicate == true){
        g.drawString("A requests for B's Session Key", 230,260);
        g.drawLine(165, 310, 325, 150);
    }

    b4.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e) {
            if(communicate == true)
                key = true;    
        }
     });
    if(key == true){
        g.drawString("A's key is 4",210,175);
        g.drawString("B's key is 5",430,175);
    }

    b5.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e) {
            if(key == true)
                acknowledge = true;    
        }
     });
    if(acknowledge == true){
        g.drawLine(170,325,530,325);
        g.drawString("A sends part of session key to B", 260, 320);
        g.drawLine(170,350,530,350);
        g.drawString("if sessiion key match then B sent Acknowledgement", 215, 365);
  }}

public static void main(String[] args) {

    Tutorial t = new Tutorial();
    JFrame jf = new JFrame("Assignment");

    jf.setSize(1200,900);
    jf.add(t);
    jf.setVisible(true);        
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}

Here it looks like when clicked but not minimized enter image description here and here it looks like when JFrame is minimized and restoredenter image description here

Shery
  • 72
  • 10
  • 1
    I assume you do something to add that green node later? If you modify classes directly without them being Swing classes, I think you manually have to issue a `repaint()` on your Swing class/container or Swing doesn't know that the components needs to be updated on the screen (Swing classes do this automatically when they are changed, so you don't have to do it when you call `add()` for example). – markspace May 17 '18 at 16:21
  • 1
    Related: *Repaint() vs. Revalidate()* https://stackoverflow.com/questions/1097366/java-swing-revalidate-vs-repaint?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa Do some searches too to understand how Swing's repaint system works (lots of tutorials). – markspace May 17 '18 at 16:23
  • Thnks alot sir,,, it worked for me – Shery May 17 '18 at 16:30

1 Answers1

0

I assume you do something to add that green node later? If you modify classes directly without them being Swing classes, I think you manually have to issue a repaint() on your Swing class/container or Swing doesn't know that the components needs to be updated on the screen (Swing classes do this automatically when they are changed, so you don't have to do it when you call add() for example). – markspace

Related: Repaint() vs. Revalidate() stackoverflow.com/questions/1097366/… Do some searches too to understand how Swing's repaint system works (lots of tutorials). – markspace

Shery
  • 72
  • 10