0

How would someone call the paintComponent method?

As an exercise from one of my university course, I must call the paintComponent method later on in my code (after the object as been created).

Any hints? Sorry for my noobness, im still learning ;)

    public class testPanel extends JPanel {

private testModel testModelHandle;
private Color[] colors;

public testPanel(testModel testModel) {
    if (testModel == null)
        throw new IllegalArgumentException("Should provide a valid instance of testModel!");
    this.testModelHandle = testModel;
    initializeColors();
}

private void initializeColors() {
    // Some tile colours in the '0xRRGGBB' format
    String[] testColorCodes = new String[]{
            "0x89CFF0", "0xF4C2C2", "0xFFBF00", "0xFBCEB1",
            "0x6495ED", "0x9BDDFF", "0xFBEC5D", "0xFF7F50",
            "0x00FFFF", "0x98777B", "0x99BADD", "0x654321"
    };
    colors = new Color[testColorCodes.length];
    for (int i = 0; i < colors.length; ++i)
        colors[i] = Color.decode(testColorCodes[i]);
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    //other stuff
}

}

  • "How would someone call the paintComponent method?" -- you don't. Swing graphics are passive and you will want to read the Swing graphics tutorial to learn more on how to use them (if you're asking this). [Lesson: Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html): introductory tutorial to Swing graphics – Hovercraft Full Of Eels Jun 05 '18 at 02:05
  • [Painting in AWT and Swing](http://www.oracle.com/technetwork/java/painting-140037.html): advanced tutorial on Swing graphics – Hovercraft Full Of Eels Jun 05 '18 at 02:05
  • "As an exercise from one of my university course, I must call the paintComponent method later on in my code (after the object as been created)." -- reread the instructions, because I can guarantee that they don't require that you actually call this method. You instead change the state of fields of the class, call `repaint()` and the painting system will *usually* call paintComponent for you, but this is all explained in the tutorial links I've given you. – Hovercraft Full Of Eels Jun 05 '18 at 02:08
  • Thank you for your answer. I actually just managed to solve this using the repaint() method from the super class. I don't know why I got that brain freeze at first... – thecanadianroot Jun 05 '18 at 02:14

0 Answers0