

-----------------------------------

Fozmik

Thu Jan 08, 2009 1:58 am


Paint in a separate class?

-----------------------------------

I'm trying to create a class which paints a square using the method paint (Graphics g). In another class, in my main method, I create an AWT frame and an object of the square-painting class, but the square does not appear on my frame. 

How can I make something like this work?

 
package prog;
import prog.*;
import java.awt.*;
public class MainClass{
    public static void main(String[] args){
        Square brdSq1 = new Square(40, 0);
        Frame game = new Frame("Test");
        game.setSize(280, 280);
        game.show();
    }
}

//new file

package prog;
import java.awt.*;
public class Square{
    Color sqClr = new Color(0xf9, 0xf8, 0xea);
    int sqX = 0, sqY = 0;
    public Square(int x, int y){
        sqX = x;
        sqY = y;
    }
    public void paint(Graphics g){
        g.setColor(sqClr);
        g.fillRect(sqX, sqY, 40, 40);
    }
}


Sorry if this is a dumb question.


-----------------------------------

wtd

Thu Jan 08, 2009 3:38 am


Re: Paint in a separate class?

-----------------------------------

Well, have you added the Square object to your Frame?

You could use the Frame class' is a relationship in object-oriented programming?


-----------------------------------

Fozmik

Thu Jan 08, 2009 3:30 pm


Re: Paint in a separate class?

-----------------------------------

I tried adding the Square to Frame with, 
game.add(brdSq1);

and making the Square a component with, 
public class Square extends Panel{
...
}


It is still not appearing though

*edit* It works now, thanks

