Solved

paint method for an ImageIcon

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

707 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

21 Experts available now in Live!

Get 1:1 Help Now