Solved

paint method for an ImageIcon

Posted on 2002-07-24
4
1,399 Views
Last Modified: 2008-02-26
I'm writing a card game, but am having problems with the GUI part.

I created a class called Card, which extends JPanel (code below), which contains an ImageIcon, being the .gif file.

When I add the image itself, I have no problem, so there's no problem with the file path. The problem is with the paint() method, for some reason it won't accept the line in the file below. I know it should be easy, but I seem to have a writers' block.


<><><><><><><><><><><><><><><><><><><><><><><><><>

import javax.swing.*;
import java.awt.*;

public class Card extends JPanel {

  ImageIcon image;
  JLabel label;

  public Card() {
    image = new ImageIcon(Toolkit.getDefaultToolkit().getImage("src/images/b2.gif"));
    label = new JLabel(image);
    this.add(label, null);
  }

  public void paint(Graphics g) {
    g.drawImage(image,0,0,this);
  }

  public ImageIcon getImage() {
    return image;
  }
}
0
Comment
Question by:maor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 2

Expert Comment

by:flumpman
ID: 7174397
The paint method you have specified is overriding the paint method for JPanel so it won't paint the label (and therefore the ImageIcon).

Remove the paint method and it should work fine.

Do you need to have a subclass of a JPanel for your card as opposed to a subclass of a JComponent (or even JLabel)?

--
flumpman
0
 

Author Comment

by:maor
ID: 7174459
I tried without the paint() method, doesn't work. I started with JComponent, and tried changing it to JPanel. Didn't work.
0
 
LVL 2

Accepted Solution

by:
flumpman earned 50 total points
ID: 7174565
It's probably due to the size of the component not being set properly.  You could override the getPreferredSize method.

either of the two Card classes below work...

import java.awt.*;
import java.io.File;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

/*
public class Card extends JLabel {
     
     public Card(File imageFile) {
          setIcon(new ImageIcon(Toolkit.getDefaultToolkit().getImage(imageFile.getAbsolutePath())));
     }
     
//     Dimension getPreferredSize() {
//          return new Dimension(icon.getIconWidth(), icon.getIconHeight());
//     }
     
     public static void main(String[] args) {
          JFrame f = new JFrame();
          JPanel p = new JPanel();
          f.setContentPane(p);
         
          p.add(new Card(new File("somefile.gif")));
         
          f.setVisible(true);
     }
}
*/

public class Card extends JComponent {
     
     Image image;
     
     public Card(File imageFile) {
          image = Toolkit.getDefaultToolkit().getImage(imageFile.getAbsolutePath());
     }
     
//     public Dimension getPreferredSize() {
//          return new Dimension(image.getWidth(null), image.getHeight(null));
//     }
     
     public void paint(Graphics g) {
          g.drawImage(image, 0, 0,this);
     }
     
     public static void main(String[] args) {
          JFrame f = new JFrame();
          JPanel p = new JPanel();
          f.setContentPane(p);
         
          p.add(new Card(new File("somefile.gif")));
         
          f.setVisible(true);
     }
}

--
flumpman
0
 

Author Comment

by:maor
ID: 7175076
Went for the second solution, extending JComponent and using Image instead of ImageIcon, works so far. Thanks a lot!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question