Solved

paint method for an ImageIcon

Posted on 2002-07-24
4
1,384 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
allswap challenge 6 79
Java Server Faces parameter pass? 6 39
JList custom Cell Renderer refresh 15 53
javap bin 2 28
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now