Paint in a separate class?
Author |
Message |
Fozmik
|
Posted: Thu Jan 08, 2009 1:58 am Post subject: 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?
code: |
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. |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
wtd
|
Posted: Thu Jan 08, 2009 3:38 am Post subject: Re: Paint in a separate class? |
|
|
Well, have you added the Square object to your Frame?
You could use the Frame class' [url=http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Container.html#add(java.awt.Component)]add[/url] method, but the signature for that requires that you add a java.awt.Component object. Your Square object is not a Component, so it won't work there.
Do you understand the is a relationship in object-oriented programming? |
|
|
|
|
 |
Fozmik
|
Posted: Thu Jan 08, 2009 3:30 pm Post subject: Re: Paint in a separate class? |
|
|
I tried adding the Square to Frame with,
and making the Square a component with,
code: | public class Square extends Panel{
...
}
|
It is still not appearing though
*edit* It works now, thanks |
|
|
|
|
 |
|
|