Re: Extending problems...
Does that code compile?
I ask because of the line
Code:
GameFrame.add(Gobject);
GameFrame is an instance of JFrame, and Gobject is an instance of Graphics2D. As far as I know frames do not have a method that allows you to add graphics contexts. So, unless I am missing something, that code won't compile. It is not clear what that line is supposed to do.
-----
You should use standard Java coding conventions and begin variables with a lowercase letter.
Also it would be a good idea to make and display the application's frame on the event dispatch thread. See the discussion of Initil Threads in Oracle's Tutorial. An example of the code they mention there can be found in FrameDemo.java which can serve as a good "recipe" for frame creation and display.
-----
[Edit] Perhaps, also, the Tutorial may be helpful in its discussion of how painting occurs in Swing: where the graphics context comes from and how it is used in painting code.
Re: Extending problems...
yes it complies and works with the oval on frame if i make the position a variable - im looking on how to make playerx and playery extend to the Graphics2D class
Re: Extending problems...
I think your code is confusing in that you're naming a class you've created "Graphics2D" and there already exists a Graphics2D class in the Java core libraries, and this class is used often when doing Swing graphics programming. So first off, to avoid confusing us, yourself and others, consider renaming this class to something else.
Next, this is a very bad idea:
Code:
public void paint(){
repaint();
}
If the JVM weren't smart, this could lead to a nasty recursive nightmare. If you want to create a "game-loop" for a Swing GUI, use a Swing Timer instead. You can find out more about this here: How to use Swing Timers
Re: Extending problems...
Ah, I see! Graphics2D is a sort of panel!
I would highly recommend that you don't use names from the standard Java library for your own classes. This will certainly confuse the likes of me. And it will very likely confuse your compiler at some point.
-----
The player positions are ints. They don't extend anything. That track panel might possibly contain the position variables as instance variables (instead of having them as unused variables of the other class)
More generally though you might find it useful to have a class representing a RaceBall. An instance of such a class would have a position and velocity and be able to update the position based on the velocity.
-----
I'm thinking along the following lines. (There are *certainly* other ways of doing things)
BallRacer
main() method to put a TrackPanel in a frame and display it
TrackPanel
contains RaceBall instance
contains RaceTrack instance
updatePosition() method to move the ball to a new position on the track. Something interesting should happen if the new position is invalid
(later you may want to animate this by having the updatePosition() method called every so often)
RaceTrack
isValid(x,y) - a method which reports if an x/y position is actully on the track
RaceBall
getX()/getY() - reports the ball's position
updatePosition() - updates the ball's position based on its velocity
(later you might want to have methods that alter the size and direction of the ball's velocity)
I'm only really saying all this (because you haven't really asked a question...) to illustrate my thinking that your game needs more, simpler, smaller classes. And not extending classes.
-----
And, to repeat, please follow Java coding conventions: your own descriptive variables and classes. Variables beginning with a lowercase letter, and classes with a uppercase one.
Re: Extending problems...
All this really helps thanks!