-
problem with jpanel
would any body how the program below is working
Code:
package skeleton;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class Board extends JPanel {
Image star;
public Board() {
star=new ImageIcon("img/star.png").getImage();
}
public void paint(Graphics g) {
Graphics2D graphics2D=(Graphics2D)g;
graphics2D.drawImage(star, 100, 10, null);
}
}
when iam running the application the star.png is drawn without calling repaint method would any body explain why it is happening
Code:
package skeleton;
import javax.swing.JFrame;
public class Image1 extends JFrame{
public Image1() {
add(new Board());
setResizable(false);
setVisible(true);
setSize(100,200);
setTitle("Amith example");
}
public static void main(String[] args) {
Image1 image = new Image1();
}
}
-
First, instead of using paint(...) use paintComponent(...) instead.
Secondly, the paint can be called outside of your control. Like if another window outside of your application crosses over. Or, in this case, when you add the panel to the frame and it's also called when it's shown for the first time.
-
Cross-posted: Java 2D - explain component
Original poster, please read this FAQ on cross-posting: BeForthrightWhenCrossPostingToOtherSites
Also just FYI: I notice that in past threads you've been in a habit of posting questions and then ignoring the replies, of never acknowledging having read them. These two behaviors (cross-posting and ignoring replies) can often place you on the do-not-help lists of many forum regulars.
Much luck.