J2SE 1.4 Broken?

Hey, so I have some code here, and for some reason even though it’s commented out, it’s still causing problems in J2SE 1.4. The issue does NOT happen with J2SE 1.3 or J2SE 5.0. I have tested this with both BlueJ and JCreator Pro. The Java version is to blame entirely.

I was wondering if someone could explain why or what’s going on here.

Here is the code:

Class 1:

import javax.swing.JFrame; 
import java.awt.Toolkit; 

public class Main extends JFrame 
{
    static JFrame jF = new Main (); 
    
    /** Screen Object */
    Screen sO = new Screen ();
    
    public Main ()
    {
        //gives a runtime error not to use and lists something else 
        //add (sO); 
        
        //getContentPane (); 
        setContentPane (sO); 
        //getContentPane (); 
        
        setSize (Toolkit.getDefaultToolkit().getScreenSize()); 
        setVisible (true); 
    }
    
    public static void main (String [] args)
    {
        
    }
}

Class 2:

import javax.swing.JPanel; 
import java.awt.Color; 
import java.awt.Graphics; 

public class Screen extends JPanel 
{
    //PolygonObject pO1; 
    
    public Screen () 
    {
        //pO1 = new PolygonObject (new int [] {10, 200, 400}, 
        //new int [] {10, 200, 400}, 
        //Color.black); 
    }
    
    public void paintComponent (Graphics g)
    {
        g.fillOval (10, 10, 500, 500); 
        
        //pO1.drawPolygon(g);
    }
}

Class 3:

import java.awt.Graphics; 
import java.awt.Color; 
import java.awt.Polygon; 

public class PolygonObject 
{
    Polygon p; 
    Color c; 
    
    public PolygonObject (int [] x, int [] y, Color c) 
    {
        p = new Polygon (); 
        
        p.xpoints = x; 
        p.ypoints = y; 
        p.npoints = x.length; 
        
        this.c = c; 
    }
    
    public void drawPolygon (Graphics g)
    {
        g.setColor(c); 
        g.drawPolygon (p); 
    }
}

Why are you using J2SE 1.4? It’s VERY old.

Trying to work in constrained environments.

I prefer the later editions because of Scanner, nicer IDE’s, etc… but I’m stuck with old versions for now.

I get this exception:

Exception in thread "main" java.lang.Error: Do not use Main.add() use Main.getContentPane().add() instead
    at javax.swing.JFrame.createRootPaneException(JFrame.java:465)
    at javax.swing.JFrame.addImpl(JFrame.java:491)
    at java.awt.Container.add(Container.java:307)
    at Main.<init>(Main.java:13)
    at Main.<clinit>(Main.java:6)

You can add setRootPaneCheckingEnabled(false); in your Main() and it should work, albeit it’s probably best to just use a content pane instead.

I don’t get an error message. It’s just displaying a black window.

Are you sure you’re using J2SE 1.4 or any of its variants?

Java 1.4 the latest release from oracle: j2sdk-1_4_2_18-linux-i586.bin

Maybe try running it on Windows

You need to add a layout manager to your contentpane it would seem. This renders as you intend:

JPanel contentPane = new JPanel(new GridLayout(1, 1));
contentPane.setBackground(Color.RED);
contentPane.add(sO);

setContentPane(contentPane);
setSize(Toolkit.getDefaultToolkit().getScreenSize());

setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

But I don’t need one in J2SE 1.3 or J2SE 5.0.

See what I’m saying?
It seems to be specific only to this version of Java, J2SE 1.4.

Maybe, maybe not, it’s still the “correct” way of doing it since the initial content pane of a JFrame is subject to change.

The code above should work the same in all Java versions up to today :slight_smile:

NOTE: I haven’t tried it in 1.3.

Seems weird, do you have the JDK sources available? Might be worth comparing them to figure out what is causing this…

I just got them from Oracle’s site.
I’ve just been using the Windows versions.

To save time, I tested with the latest and the earliest updates of J2SE 1.4. I have not checked the updates in between the latest and earliest, as I don’t see how that matters - as I assume they would be the same result, and do not feel its worth the time to test every single one in between.

I’m not sure how I would figure out what’s causing this. I only know that this issue appears to be occurring for the entire version of J2SE 1.4.

It may just be a bug or some subtle implementation detail in JPanel.

If I take your code and insert a super.paintComponent(g) in the Screen#paintComponent method I get a circle and a polygon.

However if your Screen class extends JComponent instead of JPanel it works as intended without calling super.

Edit: typo

Edit 2: It seems to be related to the opaqueness of the JPanel. Calling setOpaque(false); in the constructor of Screen (when extending JPanel) works as well.