If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

OOP repaint between methods

Before I have to end it all can anyone help me? I am new to Java but not totally new to programming and I work in I.T.

I have an assignment for college (which is now late : - ( where I have to take the classic cannon game by Budd and make it more OOP by subdividing the mass of code in one file into seperate classes that interact and pass messages to each other. I am having problems with the paint method.

Its a simple program that draws a cannon and target as a series of lines. If I include the paint method (and all the code to draw the lines) in the main class (i.e containing the main method) it will draw to the frame. If I remove this code into a subclass and pass all the variables to generate the lines/boxes (i.e x,y coordinates, height, width) it won't redraw. I am at my wits end at this stage. All the code examples I have seen on the web would imply it should work.

Could the problem be that when all in class A it calls the paint method, any change to the variables thus calls the repaint method. When you subdivide these into different classes A and B you ask A to pass to be B all the variables necessary to draw the object but you cannot call repaint as it was never painted?

Here is my code. This is just a simple example of what I am trying to do.

Why do you pass this method all those paramaters, and then do nothing with them? This isn't the reason why your cannon isn't repainting, but when we fix the problem this will need to be fixed as well. As it stands, all that method does is set color = Color.blue. The parameters don't do anything, just sit there watching. I assume you want to set the instance variable to equal the paramters passed to them, you need to actually do this though. Since your paramters have the same name as your instance variables you have to distinguise between the two using the keyword "this". Some people think that naming the paramters and variables the same as the instance variables is too confusing but personally i find it the most intuitive. Anyway, you need to do something like this for each of the parameters.

Code:

this.x = x;
this.y = y;

this.x represents the instance variable in your object, x represents the parameter. If you don't do this the variables used in the paint method will never actually change, so even if you were redrawing the cannon properly, it would never actually change.

Now to the main problem, the reason nothing is repainting is that the repaint method you call in your SimpleDraw class will call the paint method in your SimpleDraw class, not the one in your other class. There's no way it could even know about that one, it's not in the same class and you haven't made an object of the other class. Your SimpleDraw class has a paint method because it extends JFrame, and like all components it has a paint method to paint itself on your screen. You need to overide this paint method and write some extra things in it so it draws your cannon. Firstly you need to make an object of your DrawBox class inside SimpleDraw class. Then you need something like the following method

Code:

public void paint(Graphics g)
{
super(g) //calls the paint method of JFrame, the super class.
drawBoxObject.paint(g) //pass the graphics object to the DrawBox class so it can draw with it.
}

Do you understand all that? If not i'll try and explain it a little better