OOP repaint between methods


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: OOP repaint between methods

  1. #1
    Join Date
    Apr 2004
    Posts
    5

    Unhappy 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.

    Do I need to draw it first in the main class?

    //SimpleDraw.java
    import java.awt.*;
    import java.awt.event.*;

    public class SimpleDraw extends Frame{

    public static void main (String argv[]){
    SimpleDraw simple = new SimpleDraw();
    simple.setVisible(true);
    }


    public static final int FrameWidth = 600;
    public static final int FrameHeigth = 400;
    public int x = 450;
    public int y = 12;
    public int width = 50;
    public int height = 20;
    protected DrawBox drawBox;
    protected Color color;
    private int curveLeft=6;
    private int curveRight=6;



    private SimpleDraw(){
    setSize(FrameWidth,FrameHeigth);
    setTitle("As Simple As Me");
    drawBox = new DrawBox(x, y, width, height, curveLeft, curveRight);
    repaint( );
    }


    }



    //DrawBox.java
    import java.awt.*;
    import java.awt.event.*;


    public class DrawBox extends Rectangle{


    public int x = 450;
    public int y = 12;
    public int width = 50;
    public int height = 20;
    protected Color color;
    private int curveLeft=6;
    private int curveRight=6;
    public static final int FrameHeigth = 400;


    public DrawBox(int x, int y, int width, int height, int curveLeft, int curveRight){

    color = Color.blue;



    }

    public int dy(int y){
    return FrameHeigth - y;
    }


    public void paint(Graphics g){
    g.setColor(Color.red);
    g.fillRoundRect(x, dy(y), width, height,curveLeft,curveRight);
    }





    }

    Thanking you in advance!

    Claire

  2. #2
    Join Date
    Feb 2004
    Posts
    541
    Code:
    public DrawBox(int x, int y, int width, int height, int curveLeft, int curveRight)
    {
        color = Color.blue;
    }
    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

  3. #3
    Join Date
    Apr 2004
    Posts
    5

    Smile Thats Great Mike Thank You

    Hi Mike,

    Thanks so much for that. I now have this piece of code working, its a small example but I now know enough to do the rest of the assignment.

    Regards,

    Claire

    Thanks Again!

  4. #4
    Join Date
    Feb 2004
    Posts
    541
    no problem, feel free to come back if you have any more problems

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center