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
-
TS1702 I tried to download a free game but the screen froze how can I unfreeze it
I have just purchased an iphone 4 & I tried to download a free game but the screen has frozen. Please can anyone help me to unfreeze the screen.
Perform a Reset...
Reset ( No Data will be Lost )
Press and hold the Sleep/Wake button and the Home button at the same time for at least ten seconds, until the Apple logo appears. Release the Buttons.
http://support.apple.com/kb/ht1430
If no joy...
Connect to iTunes on the computer you usually Sync with and “ Restore “...
http://support.apple.com/kb/HT1414 -
Tried to install a new game that I bought (Xiii) for my MBP. All system req have been met and exceeded, however when trying to install it I get an error message saying "You can’t open the application “XIII Install” because PowerPC applications are no longer supported."
I have since read online that the operating software Yosemite has got rid of PowerPC applications, meaning I can't install my new game.
Is there anything simple that I can do to get this game to work. I'll be gutted if not as I have opened the game therefore the shop will not give a full refund, I'm sure .
Regards
SimonA lot longer ago than that. PowerPC apps haven't been supported since Lion, 10.7.x.
You may have just purchased the game, but the developers obviously haven't kept up to date with the requirements for newer versions of the Mac OS.
You have three options, depending on how old your Mac is.
1) If it were ever able to boot directly to Snow Leopard, you could create a separate partition and install SL onto that partition. Make sure to turn on the option during the installation to install Rosetta. That is OS X's PPC emulator.
2) If your Mac is too new to install Snow Leopard, then you can purchase and install Snow Leopard Server within a VM, such as Parallels, VirtualBox, or Vmware.
3) Use a separate older Mac for running outdated software. -
I am trying to verify my keychain account, but the verification code it being sent to a phone number I don't have access to. What can I do?
Frequently asked questions about two-step verification for Apple ID - Apple Support
You'll need another trusted device, or your Recovery Key, which you may have elected Apple to keep for you. -
Recently, I can't get any sound from my game applications. Sound works fine with video, streaming, etc, but not my game applications. I've tried powering down and re-booting, but the sound does not return. Can anyone help me?
Have you got notifications muted ? Only notifications (including games) get muted, so the Music and Videos apps, and headphones, still get sound.
Depending on what you've got Settings > General > Use Side Switch To set to (mute or rotation lock), then you can mute notifications by the switch on the right hand side of the iPad above the volume switch, or via control centre : swipe up from the bottom edge of the screen and it's the right-most of the 5 icons in the middle of it (if the icon is white then it's 'on', tap it to turn it grey and 'off'). The function that isn't on the side switch is set via control centre instead : http://support.apple.com/kb/HT4085 -
I'm trying to buy some EA games but they are not availalbe in my country, South Africa.
I'm trying to buy some EA games but they are not availalbe in my country, South Africa. Most of the apps I want to buy are not availalbe there. Splitting iTunes by country really is lame, I can buy anything anywhere on Ebay with the same credit card, thats the nature or eCommerce, do it all the time. How do I do this without having a foreign credit card?
"I'm trying to buy some EA games but they are not availalbe in my country"
Then you cannot buy them.
"Most of the apps I want to buy are not availalbe there."
Then you cannot buy them either.
You can ONLY buy frome the itunes store of your country of residence ( proven by the issuing country of your credit card AND the valid billing address) AND ONLY while you are physiclally located inside the borders of that country.
You cannot use other countrys itunes stores.
Sorry -
When I try to log onto my Apple ID to use facetime, it will not work and tells me to check my connection. I have even tried creating a new apple ID but the same message appears. I do not understand why this is happening, could anyone help me resolve this issue it is very frusterating that I can not enable facetime or imessage.
I could do that, however when I select the icloud button (or whatever the heck it is) I am asked to enter the apple id and password. So if you are suppose to create another one for icloud you'd think it would give you the option at this point which would be logical.
-
Tried charging my 2009 MacBook Pro but the green light won't turn on and now my computer won't turn on (it was working right before I attempted to charge it!) what do I do????? Even tried using another charger I had for it and nothing. No green light. Computer turn on. Now what??? Help!
QUOTE: "No green light. Computer turn on."
What? so were you able to turn on the computer with the new plug? does it give you any other light? amber/orange light? -
I have a new computer with Windows 7 professional, Office 2007, Adobe XI. I have just created a word document and tried save it as a PDF but the PDF option doesn't appear in the drop down menu. Any ideas please? Thank you.
What is "Adobe XI" - Reader, or Acrobat?
But either way, 'Save as Adobe PDF' is a Word function, independent of what Adobe software you have installed. -
I have an iPhone 5S, with iOS 8.1.2 I am trying to do a system restore but the message I get on the desktop is that there is not enough room on the iPhone. How much room is required?
I had an important app. (Memos by bluemarblesoftware.com) that I accidentally deleted. It of course had many memos, some with quite a lot of information. I did sync my phone on a regular basis and I think the memos should be on the desktop computer. I tried to do a restore function but I got a message saying there was not enough room on the iPhone. How much room is needed?generally you need 1.90gb
what you can do is back up your phone to your computer before doing the update, just as a precaution. -
i have read up on all about crashes and i cant even open it in safe mode :( i have tried deleting it and reinstalling it but the problem still persists. What would you suggest i do and is there any way i canget hold of firefox 6 for mac?
Does the regular Firefox 8 release version work or does the version crash as well?
*Firefox 8.0.x: http://www.mozilla.com/en-US/firefox/all.html
Create a new profile as a test to check if your current profile is causing the problems.
See "Basic Troubleshooting: Make a new profile":
*https://support.mozilla.com/kb/Basic+Troubleshooting#w_8-make-a-new-profile
There may be extensions and plugins installed by default in a new profile, so check that in "Tools > Add-ons > Extensions & Plugins" in case there are still problems.
If that new profile works then you can transfer some files from the old profile to that new profile, but be careful not to copy corrupted files.
See:
*http://kb.mozillazine.org/Transferring_data_to_a_new_profile_-_Firefox -
I updated imovie, which is now totally different. I tried to import videos from iphotos, but the system is now stuck on "import" and it doesn't include iphotos. Clicking on "close" doesn't work. How do I get out of "import" and then how do I access iphoto?
I have asked a moderator to provide assistance, they will post an invite on this thread.
Once you get a reply, if you click on their name, you will see a screen like this. Click on the link as shown below.
Please do not send them a personal message, as they may not be on duty for a long time, and your message will not be tracked properly.
There are some useful help pages here, for BT Broadband customers only, on my personal website.
BT Broadband customers - help with broadband, WiFi, networking, e-mail and phones. -
I've been trying to connect iphone to mac but the mac does'nt reponed
i've been trying to connect iphone to mac but the mac dose not respond. i upgraded to snow leopard how do i fix the porblem with my mac
Hi,
You may also refer to the help document below:
Creative Cloud Connection Update
Regards,
Sheena -
I'm trying to Authorize my new laptop but the icon fro Authorization is not there
I'm trying to Authorize my new laptop but the icon fro Authorization is not there
There is no icon. Use the iTunes Store menu > Authorize this computer.
If you need iTunes, get it here: http://support.apple.com/kb/DL1426 -
I was trying to download cs2 free version but, the serial number given comes back invalid. Is there a new one?
Item 2, on the download page
'You can use the serial numbers provided as a part of the download only if you legitimately purchased CS2, CS2 applications, Acrobat 7, or Audition 3.'
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.
-
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
-
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(>=)