Uday Chadha
Uday Chadha

Reputation: 3

paintComponent is never called

I have been at this for a day now and I cannot understand why won't the paintComponent(Graphics g) method be called. I use my second constructor to instantiate the objects, but still nothing. My roommate has a VERY similar code and that works. What am I doing wrong?

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.JPanel;

public class squareImage extends JPanel{

Image picture;
String name;
baharKa outerPanel;    // a panel that I add these sqaures in
String topLabel;

int a = 0;
int b = 0;

public squareImage(baharKa out, String top, String name, Image im){  //This is the first constructor
    this.outerPanel = out;
    this.name = name;
    this.picture  = im;
    this.topLabel = top;
    this.setPreferredSize(new Dimension(80, 60));
    this.setBounds(a, b, getWidth(), getHeight());
    this.setPreferredSize(new Dimension(80,80));
    this.setOpaque(false);
}

public squareImage(baharKa out, String top, String name, Image im, int a, int b){  //This is second contructor
    this(out, top, name, im);
    this.a = a;
    this.b = b;
    this.setBounds(a, b, getWidth(), getHeight());
    out.revalidate();
}

protected void paintComponent(Graphics g) {     //This is never called
    super.paintComponent(g);
    System.out.println ("hi!");
    int i = g.getFontMetrics().stringWidth(name);
    int j = g.getFontMetrics().stringWidth(topLabel);
    g.drawImage(picture, 20, 20, 40, 40, null);
    g.setColor(Color.BLACK);
    g.setFont(new Font("Calibri", Font.PLAIN, 12));
    g.drawString(name, i/2 + 40, 45);
    g.drawString(name, j/2 + 40, 15);
}

}

/////////I use it here///////

protected void showMeTheSquares(){
        try {
            System.out.println("showme");
            wood = new squareImage(baharKaPanel, "0", "Wood", ImageIO.read(new File("images/wood.png")), 200, 75);
            metal = new squareImage(baharKaPanel, "0", "Metal", ImageIO.read(new File("images/metal.png")), 300, 75);
            plastic = new squareImage(baharKaPanel, "0", "Plastic", ImageIO.read(new File("images/plastic.png")), 400, 75);
            screwD = new squareImage(baharKaPanel, "0", "Plastic", ImageIO.read(new File("images/screwdriver.png")), 10, 100);
            hammer = new squareImage(baharKaPanel, "0", "Hammer", ImageIO.read(new File("images/hammer.png")), 10, 180);
            pliers = new squareImage(baharKaPanel,"0", "Pliers", ImageIO.read(new File("images/pliers.png")), 10, 340);
            scissors = new squareImage(baharKaPanel,"0", "Scissors", ImageIO.read(new File("images/scissors.png")), 10, 420);
            brush = new squareImage(baharKaPanel, "0", "Paint Brush", ImageIO.read(new File("images/paintbrush.png")), 10, 260);
            wood.repaint();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        addEverything();

    }

    protected void addEverything(){

        baharKaPanel.add(wood);
        baharKaPanel.add(metal);
        baharKaPanel.add(plastic);
        baharKaPanel.add(screwD);
        baharKaPanel.add(hammer);
        baharKaPanel.add(pliers);
        baharKaPanel.add(scissors);
        baharKaPanel.add(brush);
    }

Upvotes: 0

Views: 72

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285430

This and your use of null layout is going to create a 0 by 0 sized component:

this.setBounds(a, b, getWidth(), getHeight());

If you put in code at this point and print out what getWidth() and getHeight() return in an unrendered component, you're going to find out that it's 0 or 1.

You should not be using null layouts or setting bounds regardless, but rather using layout managers that respect preferred sizes, since the use of null layout as this makes for very inflexible GUI's that while they might look good on one platform look terrible on most other platforms or screen resolutions and that are very difficult to update and maintain.

Upvotes: 1

Related Questions