Trying to code a pool game but the initial shape dissapears!!! im stuck!

Hi ,
I basically want to write a java graphics game application ......At this point in time all I want to do is add a green rectangle to the content pane.......I can do this but I want to stick with OO concepts so I have various classes......the thing is ....when a user resizes the window the graphic I have painted dissapeared
I will show the classes I have used and the code below......I want to do the program conforming to the way I was tought java which is to have a main controlling class that orchestrates communication between unrelated classes and their methods. In addition to my question if anyone sees stuff fundementally wrong with my code and has a better solution please feel free to enlighten me :-)
main controlling class:
import java.awt.Graphics;
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
public class PoolApp {
public PoolApp() {
//creates new gamescreen object
GameScreen aScreen = new GameScreen(this);
public static void main(String[] args) {
PoolApp poolApp1 = new PoolApp();
public Graphics getPaintedTable(GameScreen aGameScreen) {
Paint aPaint = new Paint(this);
Graphics tblObj = aPaint.getTbleGraphic(aGameScreen);
return tblObj;
------end main controlling class
------TLayer Class start
import javax.swing.*;
/* The purpose of this class was to create a generic frame that would
be inherited from all screen classes ...I initially had this creating a black background that would be inherited from child classes but that created
more problems when the screen was resized by one of the child classes*/
public class TLayer extends JFrame {
public TLayer() {
-----TLayer Class end
-----GameScreen class start--------
public class GameScreen extends TLayer{
private PoolApp thePoolApp;
public GameScreen(PoolApp aPoolApp) {
thePoolApp = aPoolApp; //set a reference from here to the controlling PoolApp initGameScreen(); //initialise and show GameScreen .....screen
public void initGameScreen() {
this.setSize(800,600);
this.show();
//now make a request to the poolapp controlling class to ask for a painted green table and get the paint class to paint it to screen
thePoolApp.getPaintedTable(this);
----GameScreen class end------------
----PaintScreen class start-------------
PoolApp thePoolApp;
public Paint() {
public Paint(PoolApp apoolApp) {
thePoolApp = apoolApp;
public Graphics getTbleGraphic(GameScreen aGameScreen) {
Container theCont = aGameScreen.getContentPane(); //assign the gamescreen content pane to a container
theCont.setSize(200,200); //set a viewable size to the container
Graphics thetbl = theCont.getGraphics(); //get container graphics context and assign it to a graphics object
thetbl.setColor(Color.green); //color the object
thetbl.fillRect(30,30,20,60); //fill the rectangle
return thetbl; //return the object
---PaintScreen class end---------------
This code actually draws the green rectangle to the gamescreen from the paint class .......so it works to a degree! ......whenever I resize the window the painted image dissapears, could anyone suggest a way around this also ...am I going about this the correct way? Im open to making mass changes to better the program ......the only thing I do not want is to use applets! .....as I want to learn the fundementals of custom painting my stuff
also I tried altering the paint class like so:<as well as many other suggested ways>
public class Paint extends TLayer
Graphics g;
PoolApp thePoolApp;
GameScreen theGameScreen;
public Paint() {
public Paint(PoolApp apoolApp) {
thePoolApp = apoolApp;
public Graphics getTbleGraphic(GameScreen aGameScreen) {
theGameScreen = aGameScreen;
paint(g,aGameScreen);
return g;
public void paint(Graphics g,GameScreen aGameScreen) {
super.repaint();
// draw your green rectangle here
Container c = aGameScreen.getContentPane();
Graphics thetbl = c.getGraphics();
thetbl.setColor(Color.green);
thetbl.fillRect(30,30,20,60);
I can get painting working but ...to be honest it would mean lumping things all together and Im trying to make
my program OO ...so that kind of defeats the purpose...Im really looking for an answer that is not generic ....in the sense that it will work for this program specifically.....
I have done lots of reading and tried the usual things .....I know reasnably well whats happening for the code to be drawn
but the fact that I have seperated classes based on there specific purposes ...it has made it tricky to stop the painted image vanishing when I
resize the screen.......I ask whoever is viewing this code to check out what classes inherits from each other. I have also tried overidding the paint and paintComponents methods but
the same annoying thing still happens
Any help is much needed and appreciated
David

Hello,
In order to make it painted all the time you have to override the method
paint(Graphics g) in the JFrame class. In your program you have to override it either in the TLayer class or in the GameScreen class.
This is the new GameScreen class after this modification; this makes the rectangle painted after resizing.
import java.awt.Graphics;
public class GameScreen extends TLayer {
     private PoolApp thePoolApp;
     public GameScreen(PoolApp aPoolApp) {
          thePoolApp = aPoolApp; // set a reference from here to the controlling
                                        // PoolApp initGameScreen(); //initialise and
                                        // show GameScreen .....screen
          initGameScreen();
     public void initGameScreen() {
          this.setSize(800, 600);
          this.show();
          // now make a request to the poolapp controlling class to ask for a
          // painted green table and get the paint class to paint it to screen
          thePoolApp.getPaintedTable(this);
     public void paint(Graphics arg0) {
          super.paint(arg0);
          thePoolApp.getPaintedTable(this);
}Hope this helps you.
Feel free to ask, if you have any question .
Regards,
Ahmed Saad

Similar Messages

Maybe you are looking for

  • How can I enable TWAIN scanning on PE13 (running on Win 7 Pro)?

    I've seen advice for this for earlier versions but they don't seem to map to PE13 on Win7 I have a WIA option but this is troublesome a) insufficient options b) asks to configuration on every use.

  • E75 Firmware update not found

    My E75 is currently installed with the 100.48.78 version. When I check this site using my productcode it now says that the 110.xxxxx is available. However, when using the phone the SW-"search" (my phone is in danish and I don't know the english word

  • Doubt in Conversion

    I am converting my Date parameter into the following format : YYYYMMDD to fit in the table name. But i am not able to convert the same. The following are the details: rundt in date -- IN Parameter V_bkp_table_nm varchar2(30); v_rundt DATE; Set of sta

  • How do i update my iPad without losing all the data?

    whenever i want to update my ipad, it tells me that in order to update to ios 5 i must delete all of the data and i dont want to.

  • Customer Exit  (ABAP) Error

    Hi Experts, I developed a Customer Exit Variable (BEx)..ZVDAT In Variable definition i defined  'Processing Type-Customer Exit' and in CMOD i have written below code My requirment is to display records whose Expiry Date is Greater than and equal(>=)