Results 1 to 3 of 3
  1. #1
    amith is offline Member
    Join Date
    May 2008
    Rep Power

    Default problem with jpanel

    would any body how the program below is working

    Java 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

    Java Code:
    package skeleton;
    import javax.swing.JFrame;
    public class Image1 extends JFrame{
    	public Image1() {
    		add(new Board());
    		setTitle("Amith example");
    	public static void main(String[] args) {
    			Image1 image = new Image1();

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    San Jose, CA, USA
    Rep Power


    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.
    My Hobby Project: LegacyClone

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    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.
    Last edited by Fubarable; 02-21-2010 at 07:48 PM.

Similar Threads

  1. Problem resizing JPanel on window resize
    By Nyet in forum AWT / Swing
    Replies: 4
    Last Post: 11-27-2009, 04:13 AM
  2. Jpanel painting problem
    By kcakir in forum AWT / Swing
    Replies: 3
    Last Post: 04-15-2009, 11:21 PM
  3. Not another Jpanel Scrollbar problem!
    By jiexx in forum AWT / Swing
    Replies: 3
    Last Post: 03-18-2009, 03:09 AM
  4. Problem on adding JButton on JPanel NEED HELP
    By boisk in forum AWT / Swing
    Replies: 15
    Last Post: 03-15-2009, 03:27 PM
  5. Problem with JPanel
    By ibanez270dx in forum New To Java
    Replies: 2
    Last Post: 11-09-2007, 06:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts