Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with Graphics initialization  RSS feed

 
andrea armani
Greenhorn
Posts: 8
Java Mac Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I've got a little problem:
I need to split my code in different classes.
When I try to call a method of another class with a Graphics object I find an error..

I'd like to use stampa.paint() in the method Inserisci of a class called Inserimento.



But if I write g=null ther will be an error (NullPointerException)
Then I try to don't write g=null



But if I don't write this there is an error: variable g may not have been initialized

This is Stampa.paint()




In stampa.paint() I called another method c.forza44() that have to use Graphics but this method works correctly.

So, I've got 3 questions:
- how can I fix this problem??
- I read that when I call paint() or repaint() I don't have to write Graphics G:
in my program I wrote stampa.paint(g,maxrig, maxcol, scacchiera, fineGioco, rosso, rosso, giallo), but paint() don't need to have g (the Graphics object).. I try to change the method name, but it doesn't work anyway. Why??
- Why when I call c.forza44() it correctly works??

I'm sorry for my bad english.





 
Stephan van Hulst
Saloon Keeper
Posts: 7815
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Andrea,

It's important that classes have a specific responsibility, and that they handle that responsibility, and nothing more. In your code, it seems you are mixing the game logic with the display logic. This will become a mess very soon.

Instead, make a class that represents your game logic, and a class that is responsible for displaying the game on screen. This is called the Model, View, Controller design, and will make your code much more robust.

Here's an example of the Model:
Now, as for the View, if you're using Swing, the easiest way is to create a subclass of JPanel, called GamePanel. Give it a private inner class Square, which also extends JPanel. GamePanel uses a GridLayout, and each cell in the grid contains a Square. Then override paintComponent() of Square to fill it with the correct color, depending on the state of the game, and the Squares location in the grid. Here's a skeleton:
 
andrea armani
Greenhorn
Posts: 8
Java Mac Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I change my classes, but I still have the same problem with Graphics..



When I write g.setColor() says " variable g might not have been initialized...
But I can't assign null to g..


 
Stephan van Hulst
Saloon Keeper
Posts: 7815
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not how graphics work.

Your window decides when parts of it need to be repainted. It then creates a Graphics object, and passes it to the paintComponent() method of components that need to be repainted. This is where you put your drawing logic.

Have you used Swing before? If not, go through this tutorial: http://docs.oracle.com/javase/tutorial/uiswing/TOC.html
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!