Problem for the ImageIcon can't show after paintComponent()

i want to draw a chessboard on a panel,and set a robot (a imageicon of JLabel) on the chessboard,then move the robot .but the question is ,i have to paintComponent () to draw the chessboard,then when the board add JLabel,i can't see the imageicon. if i do not use method of paintComponent() to draw ,i can see the imageicon.

this is my code,there are three classes.

import javax.swing.*;

import java.awt.*;

publicclass Labelextends JLabel{

private ImageIcon icon;

public Label(){

icon=new ImageIcon(this.getClass().getResource("robot.gif"));

this.setIcon(icon);

}

}

import javax.swing.*;

import java.awt.*;

publicclass ImageTestextends JFrame{

private Board board=null;

public ImageTest(){

this.setSize(700,700);

this.setLocation(200,150);

board=new Board();

this.getContentPane().add(board);

setVisible(true);

}

publicstaticvoid main(String args[]){

ImageTest imageTest=new ImageTest();

}

}

import java.awt.*;

import javax.swing.*;

publicclass Boardextends JPanel{

private Label label=null;

public Board(){

label=new Label();

this.add(label);

}

// this is the problem,if i don't use this method and draw nothing,i can see the imageicon when the program run

publicvoid paint(Graphics g){

super.paintComponent(g);

this.add(label);

}

}

[3049 byte] By [suncs2001a] at [2007-11-27 4:35:18]
# 1

1) Don't call you class "Label", this is already a class in the AWT and its confusing

2) The following code is just plain ugly:

public void paint(Graphics g){

super.paintComponent(g);

this.add(label);

}

a) you don't invoke paintComponent(), you invoke paint

b) you don't add components in the painting methods. These methods are invoked multiple times whever the JVM determines a frame or component needs to be repainted.

c) custom painting is done in the paintComponent() method.

You don't even need to do any custom painting for a chessboard. Just create an 8X8 grid and add panels of alternating color to the grid. Then add JLabels with icons to the various panels.

camickra at 2007-7-12 9:45:20 > top of Java-index,Desktop,Core GUI APIs...
# 2
thanks for camickr!! when i remove super.paintComponent(g);this.add(label);then i can see the imageicon.but for i am a beginer,i was puzzled by what camickr said,is there some method to create a grid without paint?
suncs2001a at 2007-7-12 9:45:20 > top of Java-index,Desktop,Core GUI APIs...
# 3
take a look at layout managersand GridLayout.... should be exactly what you are looking for
Nibura at 2007-7-12 9:45:20 > top of Java-index,Desktop,Core GUI APIs...
# 4
> is there some method to create a grid without paint? http://forum.java.sun.com/thread.jspa?forumID=57&threadID=518707&start=3
camickra at 2007-7-12 9:45:20 > top of Java-index,Desktop,Core GUI APIs...
# 5
your famous chess board, i really think that should should be packaged with the next JDK, LOL,count how many posts to that link you have given and how many people it has helped... it is amazing,
Nibura at 2007-7-12 9:45:20 > top of Java-index,Desktop,Core GUI APIs...