Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

paint method for an ImageIcon

Posted on 2002-07-24
4
Medium Priority
?
1,412 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 200 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
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 …
Suggested Courses

610 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