It seems that I can't add StateIcon objects outside the constructor because my reference to the JPanel (FsaPanel) is wrong somehow. I thought the pan=this; statement would make the global variable equal the FsaPanel.
Everything here compiles, however the StateIcon object is not visible.
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.geom.*;
public class FsaPanel extends JPanel implements FsaListener
{
private FsaPanel pan;
//constructor
public FsaPanel()
{
pan=this;
this.setSize(500,500);
this.setLayout(null);
}
public void statesChanged()
{
StateIcon icon=new StateIcon();
icon.setLocation(10,10);
pan.add(icon);
}
public class StateIcon extends JComponent implements StateListener
{
private Ellipse2D.Double circle;
public StateIcon(State s)
{
circle =new Ellipse2D.Double(0,0,80, 80);
this.setSize(100,100);
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d=(Graphics2D)g;
g2d.setColor(Color.blue);
g2d.fill(circle);
}
}
}

New Topic/Question
Reply



MultiQuote




|