Here's my JFrame code:
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.setSize(600,600);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyCustomWidget widget = MyCustomWidget.createWidget(400, 400);
widget.setVisible(true);
// just to set x and y
widget.setLocation(40, 40);
jf.getContentPane().add(widget);
jf.setVisible(true);
}
and here's the code for MyCustomWidget
:
public class MyCustomWidget extends JComponent {
public void paint(Graphics g)
{
super.paint(g);
}
public static MyCustomWidget createWidget(int width,int height)
{
MyCustomWidget tw = new MyCustomWidget();
tw.setBounds(0,0,width,height);
tw.setBackground(Color.RED);
return tw;
}
}
the thing is, the JComponent is not shown in the window, and I don't understand why. I even added a widget.setVisible(true)
just to make sure it is visible. Nothing works. Can you spot what I'm doing wrong?
After the modifications you guys suggested, the code now is:
package javaapplication2;
public class Main {
public static void main(String[] args) throws IOException {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame jf = new JFrame();
jf.setSize(600,600);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setLayout(null);
JComponent container = (JComponent) jf.getContentPane();
container.setDebugGraphicsOptions(DebugGraphics.FLASH_OPTION);
DebugGraphics.setFlashColor(Color.RED);
DebugGraphics.setFlashCount(2);
DebugGraphics.setFlashTime(100);
MyCustomWidget widget = MyCustomWidget.createTimeline(400, 400);
container.add(widget);
jf.setVisible(true);
}
});
}
}
and:
public class MyCustomWidget extends JComponent {
public void paintComponent(Graphics g)
{
setForeground(Color.BLACK);
drawLines(g);
}
// a little something to see that something is drawed
private void drawLines(Graphics g)
{
int distanceBetween = getHeight() / numberOfLines;
int start = 0;
int colourIndex = 0;
Color colours[] = {Color.BLUE,Color.WHITE,Color.YELLOW};
for(int i = 0;i < distanceBetween;start+=distanceBetween,i++)
{
g.setColor(colours[colourIndex]);
g.drawLine(0,start,40,40);
colourIndex %= colours.length;
}
}
private int numberOfLines = 4;
public MyCustomWidget()
{
setOpaque(true);
}
public static MyCustomWidget createTimeline(int width,int height)
{
MyCustomWidget tw = new TimelineWidget();
tw.setBounds(0,0,width,height);
return tw;
}
}