• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Problem with Graphics initialization

 
Greenhorn
Posts: 8
Mac Netbeans IDE Java
  • 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.





 
Saloon Keeper
Posts: 10308
217
  • 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
Mac Netbeans IDE Java
  • 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: 10308
217
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!