Results 1 to 7 of 7
  1. #1
    MarioBrawler is offline Member
    Join Date
    May 2012
    Posts
    18
    Rep Power
    0

    Default ImageIcon painting twice.. And both times with different size??

    Okay so I'm having a bit of trouble. At first, it seemed that my program wouldn't allow my pictures to be transparent even with setOpaque(false);. But Then, I learned it was because I have the same image behind it but bigger!
    I'm using JLayeredPane because I need my image to stack ontop of each other and I'm using imageicon and JLabel because I need listeners added.

    So here is
    Java Code:
    import java.awt.*;
    import javax.swing.*;
     
    public class Example extends JApplet {
     
       ImageIcon ArcChoice = new ImageIcon("ArcChoice.png");
       JLabel LArcChoice; // JLabel so I can add the ImageIcon into it later
       JLayeredPane lpane = new JLayeredPane(); 
     
       public void init() {
     
          LArcChoice = new JLabel(ArcChoice) {
             public void paintComponent(Graphics g) {
                super.paintComponent(g);
                if (ArcChoice != null) {
                   g.drawImage(ArcChoice.getImage(), 0, 0, 68, 266, null);
                }
             }
          };
     
          LArcChoice.setBounds(227, 28, 68, 266);
          LArcChoice.setOpaque(false);// Transparent
     
          lpane.add(LArcChoice, new Integer(0), 0);// Add the Layered pane
       }
    }
    So i only copied the necessary code. I've discovered that it goes through the if statement twice. The first time, it ignores the size I added. It paints. The second time, it looks at the size and paints again.
    I know this because I used booleans to make this run once and it showed that the first time ignores the size I wanted.
    So I have no idea on what to do. As I said before, I tried using booleans to make it run once, but the one time it runs isn't good.
    Last edited by MarioBrawler; 06-25-2012 at 05:29 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: ImageIcon painting twice.. And both times with different size??

    You're doing things within the paintComponent method that should never be done there such as adding a new component to a container. Realize that you never have full control over when or even if paintComponent is called, and also this method *must* be blazingly fast else your program's responsiveness will suffer. For this reason program logic and GUI manipulation (other than painting) should not be done within this method.

    Edit: on review of your code, I may be wrong on this. I'm finding your code exceedingly difficult to follow due to your random indentations. Please standardize your code formatting before presenting it here.

    Yes, sorry, but I'm wrong. I've re-formatted your code like so:

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class Example extends JApplet {
    
       ImageIcon ArcChoice = new ImageIcon("ArcChoice.png");
       JLabel LArcChoice; // JLabel so I can add the ImageIcon into it later
       JLayeredPane lpane = new JLayeredPane(); 
    
       public void init() {
    
          LArcChoice = new JLabel(ArcChoice) {
             public void paintComponent(Graphics g) {
                super.paintComponent(g);
                if (ArcChoice != null) {
                   g.drawImage(ArcChoice.getImage(), 0, 0, 68, 266, null);
                }
             }
          };
    
          LArcChoice.setBounds(227, 28, 68, 266);
          LArcChoice.setOpaque(false);// Transparent
    
          lpane.add(LArcChoice, new Integer(0), 0);// Add the Layered pane
       }
    }
    and see that your paintComponent method is OK.

    Please tell us specifically what sizes the code is "ignoring".
    Last edited by Fubarable; 06-25-2012 at 05:04 AM.

  3. #3
    MarioBrawler is offline Member
    Join Date
    May 2012
    Posts
    18
    Rep Power
    0

    Default Re: ImageIcon painting twice.. And both times with different size??

    The only thing that is in the paint Component method is
    Java Code:
    super.paintComponent (g);
                        if(ArcChoice != null) g.drawImage(ArcChoice.getImage(),0,0,68,266,null);
    Everything else is outside the paintComponent. I'm not sure if my closed curly brackets are clearly shown.

    I'm not sure if I'm misunderstanding you. But if you did see the curly closed brackets, then I guess I am misunderstanding you.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: ImageIcon painting twice.. And both times with different size??

    See the edit to my original post. And again, please clean up your code, especially if you are asking us to help you. We are all volunteers helping on our free time, and it's not asking too much for you to put in the effort to post well formatted easily readable code.

  5. #5
    MarioBrawler is offline Member
    Join Date
    May 2012
    Posts
    18
    Rep Power
    0

    Default Re: ImageIcon painting twice.. And both times with different size??

    Yeah I know, I tried cleaning up my code by adding comments but I guess that isn't enough. Well anyways, thanks so much for helping :).
    so this is what's happening
    I mentioned that It reads this code twice
    Java Code:
                super.paintComponent(g);
                if (ArcChoice != null) {
                   g.drawImage(ArcChoice.getImage(), ******0, 0, 68, 266*******, null);
    The one that is surrounded by stars is what the program ignores the first time.

    v Then it decides to read it again understanding that I had added dimensions. It paints over the first one. so you can see two of the same images, the resized one is ontop of the smaller one.
    Java Code:
                super.paintComponent(g);
                if (ArcChoice != null) {
                   g.drawImage(ArcChoice.getImage(),0, 0, 68, 266, null);
    Also, I forgot to mention that I used System.out.println("Test");. I don't know if it goes through the
    Java Code:
    LArcChoice= new JLabel(ArcChoice){
    twice or the
    Java Code:
    public void paintComponent (Graphics g) {
    twice.
    Last edited by MarioBrawler; 06-25-2012 at 05:26 AM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: ImageIcon painting twice.. And both times with different size??

    Since you're adding the ImageIcon to the JLabel, it will be painted by the JLabel automatically and in code that occurs behind the scenes. When you code to explicitly draw the ImageIcon's image, you are telling the JLabel to draw the image twice, and risk having strange side effects. I would advise you to simply add the ImageIcon to the JLabel and not to override the JLabel's paintComponent method. Let the JLabel do its own drawing of it's icon.

  7. #7
    MarioBrawler is offline Member
    Join Date
    May 2012
    Posts
    18
    Rep Power
    0

    Default Re: ImageIcon painting twice.. And both times with different size??

    Hey thank you so much! Thanks to you, I learned what the issue is. I actually had a lot typed here before I start writing this. While I was typing, I was trying things out. What you suggested is great but there was a small issue with that. Thus I had two main conclusions. Then I decided to remove the
    Java Code:
    super.paintComponent (g);
    and it worked the way I wanted!

    Once again, thank you so much. I've discovered the issue because you told how it was being drawn twice.
    So this is what I think is the problem. First, when it goes through the codes, it sees the super.paintComponent(g);. So it paints the image. then it continues to the next line and notices that I've set sizes. then it paints again after it sees the closing squiggly bracket.

Similar Threads

  1. Using ImageIcon
    By David1234204 in forum AWT / Swing
    Replies: 4
    Last Post: 03-21-2012, 09:14 PM
  2. Need help with ImageIcon and int
    By proceeded in forum New To Java
    Replies: 9
    Last Post: 01-23-2011, 11:10 PM
  3. ImageIcon HELP!
    By ben1989 in forum New To Java
    Replies: 1
    Last Post: 05-05-2010, 09:48 PM
  4. Setting frame size to the size of an image
    By Yoruichi in forum AWT / Swing
    Replies: 5
    Last Post: 04-22-2009, 05:37 PM
  5. ImageIcon and size
    By wokena in forum Java 2D
    Replies: 3
    Last Post: 12-21-2008, 09:47 AM

Posting Permissions

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