2

I've looked at the other examples on here but the revalidate() or repaint() after I create all of my components. I've also tried the this.setVisible(this); and that didn't work. I've tried creating my components in a createGUI() method and running that from the init() method within a try/catch statement.

EDIT I tried all of your examples, as you can see in the comments. But from what everyone has said why does this now work?

package basic;

import java.awt.*;
//import java.applet.*;
import java.applet.Applet;
import javax.swing.*;
import java.awt.event.*;

public class Shapes extends Applet implements ActionListener
{
JButton rectBtn = new JButton("Rectangle");
JButton circBtn = new JButton("Circle");
JLabel rectLbl = new JLabel("Rectangle"), circLbl = new JLabel("Circle");
JLabel widthLbl = new JLabel("Width"), heightLbl = new JLabel("Height");
JTextField widthTF = new JTextField(6), heightTF = new JTextField(6), colorTF;

boolean rectOn;
boolean circOn;
int x,y, width, height;
String xcord, ycord, widthSize, heightSize;

public void init()
{   

    JPanel TotalGUI = new JPanel(new GridLayout(2,0));
    TotalGUI.add(rectLbl);      TotalGUI.add(rectBtn);
        rectBtn.addActionListener(this);
    TotalGUI.add(circLbl);      TotalGUI.add(circBtn);
        circBtn.addActionListener(this);
    TotalGUI.add(widthLbl);     TotalGUI.add(widthTF);
    TotalGUI.add(heightLbl);    TotalGUI.add(heightTF);
    add(TotalGUI, BorderLayout.WEST);
    //this.setVisible(true);
    TotalGUI.repaint();
    //pack();
}


//@Override
public void paintComponent(Graphics g)
{
    //super.paintComponent(g);
    //Graphics g2 = getGraphics();

    if(rectOn)//if Rectangle has been pressed
    {
        g.drawRect(x,y, width,height);
    }
    if(circOn)//if Circle has been pressed
    {
        g.drawOval(x,y, width, height);
    }
}

public void actionPerformed(ActionEvent e)
{
    if(e.getSource() == rectBtn)
    {
        rectOn = true;
    }
    if(e.getSource() == circBtn)
    {
        circOn = true;
    }
    //Reads coordinates and sizes as Strings and converts to integers
    try{
        widthSize = widthTF.getText();
        width = Integer.parseInt(widthSize);    
        heightSize = heightTF.getText();
        height = Integer.parseInt(heightSize);
    }
    catch(Exception err)    { JOptionPane.showMessageDialog(null, "Enter a number!");   }
    repaint();
}

}

Thank you for your help!

liloka
  • 1,016
  • 4
  • 14
  • 29
  • For better help sooner, post an [SSCCE](http://sscce.org/). But before you do that, remove `Graphics g = getGraphics();` and replace every reference to `g` with `g2`. You won't see the components yet, but it might stop the flashing. – Andrew Thompson Jan 06 '12 at 14:48
  • I posted the code, and removed the Graphics g = getGraphics(); that didn't help the flashing though unfortunately. – liloka Jan 06 '12 at 15:10

2 Answers2

4

The main problem with your original code was that your overrode the paint() method without invoking super.paint(g). When you changed that method to paintComponent() the code worked because that method doesn't even exits in an Applet so it was dead code.

The problems with your code:

  1. You should be extending JApplet for a Swing applet
  2. You should NOT override the paint() (or paintComponent()) method of an Applet. If you need to do custom painting then you override the paintComponent() method of a JPanel (or JComponent) and add the panel to the content pane of the applet.
  3. The code should be executed on the EDT.
  4. The applet will display the components automatically, there is no need to invoke repaint)
  5. Never use getGraphics() to do custom painting. Use the Graphics object of the paintComponent() method.
  6. When you attempt to override a method don't forget to use the @Override annontation before the method signature to make sure you override the method correctly.

Start by reading the Swing tutorial for a better explanation and working example. Start with the sections on:

  1. How to Make Applets
  2. Performing Custom Painting
camickr
  • 321,443
  • 19
  • 166
  • 288
  • 1->Correct 2->Correct. Nobody suggested otherwise :) 5-> Yes. 6->Irrelevant, but always valid – Nikola Yovchev Jan 06 '12 at 16:05
  • I looked up Applet vs JApplet and one some forums it did suggest use Applet instead despite it being the old way. I changed my program to use JApplet and everything disappeared so I chose to stick with Applet. What does number 3 mean, execute on the EDT? – liloka Jan 06 '12 at 16:14
  • 1
    The forums that suggest to use Applet with Swing components are wrong! If your code doesn't work with JApplet then you have invalid code. That is why I listed all the potential problems with your code. EDT stands for Event Dispatch Thread which is described in the section on "Concurrency". A proper example of how to use the EDT is also given in the tutorial on "How to Make Applets" where the code uses `invokeAndWait`. – camickr Jan 06 '12 at 16:46
1

You should call repaint() on TotalGUI.

The reason your gui refreshes after resize, is that resize automatically calls repaint() for you. However, if you want you gui changes to appear instantly, you should call repaint();

A preferred approach, however, is to use in your totalGUI's paint(Graphics g)/paintComponent(Graphics g) method/s:

super.paintComponent(g);

as described by these posts:

JPanel repaint issue

http://www.sitepoint.com/forums/showthread.php?273522-super.paintComponent()

Community
  • 1
  • 1
Nikola Yovchev
  • 9,498
  • 4
  • 46
  • 72
  • Maybe I'm putting the repaint() in the wrong place, as that is still not working. I'm calling it as the last thing in the init method. I'll try the super.paintComponent(g) method and see how that goes, so thank you :) – liloka Jan 06 '12 at 14:32
  • -1 There is no need to call repaint(). The applet will do this. You would also never override the paintComponent() method of an Applet because this method doesn't exist. – camickr Jan 06 '12 at 15:39
  • @camickr TotalGUI is a JPanel, not an applet. A JPanel is a Component and as such has the method paintComponent. – Nikola Yovchev Jan 06 '12 at 15:43
  • 1
    The code posted here is fully contained in the extended Applet class. There is no reason to override the paintComponent() method of the TotalGUI class because the code is just adding components to it. I now understand what you where trying to suggest but the suggestion was not clear. As you can see from the posters updated code he just changed the paint() method to paintComponent() method. Also, you should not suggest to override the paint() method because custom painting is done in the paintComponent() method of a JPanel. – camickr Jan 06 '12 at 15:53