Solved

paint method for an ImageIcon

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
use lov values 2 71
Java syntax, or is it Selenium 6 37
Using jdbcTemplate.batchUpdate to improve INSERT performance 6 26
Java array 10 55
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 …
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

762 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