Reputation: 75
After looking around for a bit, I finally found out how to actually call the paintComponent()
method. The problem I'm having right now, is that the line g.drawRect(100, 100, 100, 6);
is not actually drawing the rectangle. I put two System.out.println
above and below this line of code to see if actually got to that line, and it did.
Here is the constructor of the class. the paintComponent() method is called at the end
View(Model m)
{
this.m = m;
setTitle("Word Guesssing");
super.setSize( new Dimension (475, 500) );
drawPanel = new JPanel ( new MigLayout() );
guessPanel = new JPanel ( new MigLayout() );
wordPanel = new JPanel ( new MigLayout() );
guessedLettersPanel = new JPanel ( new MigLayout() );
setLetterTextFields(); //Adds the letter JTextFields to an ArrayList in the Model class.
addToGuessPanel(); //Method where components are added to the guessPanel.
addToWordPanel(); //Method where components are added to the wordPanel.
addToGuessedLettersPanel(); //Method where components are added to the guessedLettersPanel.
//Sets a boarder for each panel.
wordPanel.setBorder (new LineBorder(new Color(0, 0, 0)));
drawPanel.setBorder (new LineBorder(new Color(0, 0, 0)));
guessPanel.setBorder (new LineBorder(new Color(0, 0, 0)));
guessedLettersPanel.setBorder(new LineBorder(new Color(0, 0, 0)));
//Sets the sizes for each panel
drawPanel.setPreferredSize(new Dimension (475, 500));
wordPanel.setPreferredSize ( new Dimension(450, 50) );
guessPanel.setPreferredSize( new Dimension(350, 100) );
guessedLettersPanel.setPreferredSize( new Dimension(125, 600) );
setRadioButtonGroup();
addToPane();
setVisible(true);
setResizable(false);
paintComponent(drawPanel.getGraphics());
}
here is the paintComponent() method
public void paintComponent( Graphics g )
{
System.out.println("here 1");
g.drawRect(100, 100, 100, 6);
System.out.println("here 2");
}
Upvotes: 1
Views: 242
Reputation: 213
Make sure to repaint()
as Kayaman suggested, but probably a bigger issue is that in paintComponent you don't call the superclass's paintComponent. Try adding super.paintComponent(g)
to the top of that function.
EDIT: you should have it extend a JPanel, and then add it to a JFrame if you want it to show up. A good way of thinking about this is that the JFRame is the window, but the JComponents (an JPanel is a JComponent) tell the window what to draw
Upvotes: 3