I'm currently making a basic connect four game with a GUI and I'm trying to figure out how I can place the pieces on the board. I have a method that can locate which space the user would like to put the piece on, but I'm unsure how to paint the pieces in. Since paintComponent()
is always called without an actual method call and the default constructor only takes a Graphics object, how could I overload paintComponent()
so that it can take different arguments (these arguments are the location of the piece) and the compiler will know to call my new paintComponent()
method?
how could I overload
paintComponent()
so that it can take different arguments
You can't. Or rather, you can create an overloaded method, but the JRE won't call it.
Instead, the code needs to keep a model of the game state, when it changes, call repaint()
. In the normal paintComponent(Graphics)
method, use the model to determine how it should be drawn.
Obviously, for this to work, the model must be within the scope of the method - so you might make it a class attribute.