Application Paint() Help

Hi, I am making a game, but sometimes when I activate it, I will get a nullpointerexception (varies greatly). Also, when I try to have the About class paint, it only works the second time through. I would greatly appriciate any help!
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
public class RPGMain extends Frame implements ActionListener, KeyListener, WindowListener, WindowStateListener, WindowFocusListener
     Image back;
     Graphics draw;
     MenuItem New, Open, Save, Quit, ttl, Directions, About;
     String gameState;
     Toolkit toolkit = Toolkit.getDefaultToolkit();
     newGame newGame;
     loadGame loadGame;
     saveGame saveGame;
     Directions dir;
     About about;
     Chara hero = new Chara();
     public static void main(String args[])
          System.out.println(" ");
          System.out.println("|        Cowboy  Bebop        |");
          System.out.println("|            Naruto           |");
          System.out.println("|            Trigun           |");
          System.out.println("|   Neon Genesis Evangelion   |");
          System.out.println("|           Inuyasha          |");
          System.out.println(" ");
          new RPGMain();
     public RPGMain()
          setTitle("RPG Main");
          setLayout(new FlowLayout());
          setSize(500, 500);
          setResizable(false);//This makes it so that the frame is always 500 x 500 pixels
          MenuBar menu = new MenuBar();
          Menu file = new Menu("File");
          Menu help = new Menu("Help");
          New = new MenuItem("New");
          Open = new MenuItem("Open");
          Save = new MenuItem("Save");
          Quit = new MenuItem("Quit");
          Directions = new MenuItem("Directions");
          About = new MenuItem("About");
          New.addActionListener(this);
          Open.addActionListener(this);
          Save.addActionListener(this);
          Quit.addActionListener(this);
          Directions.addActionListener(this);
          About.addActionListener(this);
          addWindowListener(this);
          addWindowStateListener(this);
          addWindowFocusListener(this);
          file.add(New);
          file.add(Open);
          file.add(Save);
          file.addSeparator();
          file.add(Quit);
          help.add(Directions);
          help.add(About);
          menu.add(file);
          menu.add(help);
          setMenuBar(menu);
          setVisible(true);
          back = createImage(500, 500);
          if (back == null)
               System.out.println("\n|       Back is null...       |\n");
          draw = back.getGraphics();//The frame (or whatever) must be visible for this to work!!!
          newGame = new newGame();//creates an instance of *** and then gives it the varibles it needs
          newGame.set(back, draw, toolkit);
          loadGame = new loadGame();
          loadGame.set(back, draw, toolkit);
          saveGame = new saveGame();
          saveGame.set(back, draw, toolkit);
          about = new About();
          about.set(back, draw, toolkit);
          dir = new Directions();
          dir.set(back, draw, toolkit);
          gameState = "Title Screen";
          repaint();
     public void paint(Graphics g){}//leave empty to negate flashes
     public void update(Graphics g)
          if(gameState == "Title Screen"){draw.drawImage((toolkit.getImage("images/titleScreen.gif")), 0, 0, this);}
          else if(gameState == "New Game"){newGame.draw();}
          else if(gameState == "Load Game"){loadGame.draw();}
          else if(gameState == "Save Game"){saveGame.draw();}
          else if(gameState == "Dir"){dir.draw();}
          else if(gameState == "About"){about.draw();}
          try
               g.drawImage(back,0,0,this);
          catch(NullPointerException e)
               System.out.println("\n|NullPointerException in Paint|\n");
               repaint();
//Begin Window Event Block
     public void windowClosing(WindowEvent e)
          System.out.println("|       Window  Closing       |");
          System.exit(0);
     public void windowActivated(WindowEvent e)
          repaint();
          System.out.println("|      Window  Activated      |");
     public void windowDeactivated(WindowEvent e){System.out.println("|     Window  Deactivated     |");}
     public void windowOpened(WindowEvent e)
          repaint();
          System.out.println("|        Window Opened        |");
     public void windowClosed(WindowEvent e){System.out.println("|        Window Closed        |");}
     public void windowDeiconified(WindowEvent e)
          repaint();
          System.out.println("|     Window  Deiconified     |");
     public void windowIconified(WindowEvent e){System.out.println("|      Window  Iconified      |");}
     public void windowGainedFocus(WindowEvent e)
          repaint();
          System.out.println("|     Window Gained Focus     |");
     public void windowLostFocus(WindowEvent e){System.out.println("|      Window Lost Focus      |");}
     public void windowStateChanged(WindowEvent e)
          repaint();
          System.out.println("|     Window State Change     |");
//End Window Event Block
//Begin Action Performed Block
     public void actionPerformed(ActionEvent e)
          if(e.getSource() == New)
               gameState = "New Game";
               repaint();
          if(e.getSource() == Open)
               gameState = "Load Game";
               repaint();
          if(e.getSource() == Save)
               gameState = "Save Game";
               repaint();
          if(e.getSource() == Quit)
               System.out.println("|     User closing window     |\n");
               System.exit(0);
          if(e.getSource() == Directions)
               gameState = "Dir";
               repaint();
          if(e.getSource() == About)
               gameState = "About";
               repaint();
//End Action Performed Block
//Begin Key Input Block
     public void keyTyped(KeyEvent e)
     public void keyPressed(KeyEvent e)
     public void keyReleased(KeyEvent e)
//End Key Input Block
import java.awt.*;
import java.awt.event.*;
public class About extends Panel
     Graphics draw;
     Image back;
     Toolkit toolkit;
     public void set(Image a, Graphics b, Toolkit tool)
          back = a;
          draw = b;
          toolkit = tool;
     public void draw()
          System.out.println("|            About            |");
          draw.setColor(Color.black);
          draw.fillRect(0,0,500,500);
          draw.setColor(Color.white);
          draw.drawImage(toolkit.getImage("images/chaos.gif"), 205, 150, this);
          Font foo = new Font("Courier", Font.PLAIN, 24);
          draw.setFont(foo);
          draw.drawString("Choas Programming", 125, 300);
          draw.drawString("-----------------", 125, 320);
          draw.drawString("Head Programmer", 140, 340);
          foo = new Font("Arial", Font.PLAIN, 12);
          draw.setFont(foo);
public class Chara
     String name;//Character's name
     String property;//Character's elemental property
     String race;//Character's type
     String job;//Character's job
     String status;//Character's status
     String equipe[];//Character's equipement
     String items[][];//Character's items {Useables, Equipes, Other}
     int hp[];//Min/Max HP
     int mp[];//Min/Max SP
     int strength[];//Current/Normal/Race Bonus
     int agility[];//Current/Normal/Race Bonus
     int vitality[];//Current/Normal/Race Bonus
     int intelligence[];//Current/Normal/Race Bonus
     int dexterity[];//Current/Normal/Race Bonus
     int luck[];//Current/Normal/Race Bonus
import java.awt.*;
import java.awt.event.*;
public class Directions extends Panel
     Graphics draw;
     Image back;
     Toolkit toolkit;
     public void set(Image a, Graphics b, Toolkit tool)
          back = a;
          draw = b;
          toolkit = tool;
     public void draw()
          System.out.println("|          Direction          |");
          draw.setColor(Color.green);
          draw.fillRect(0,0,500,500);
          draw.setColor(Color.black);
          draw.drawString("|        Directions           |", 100, 150);
import java.awt.*;
import java.awt.event.*;
public class loadGame extends Panel
     Graphics draw;
     Image back;
     Toolkit toolkit;
     public void set(Image a, Graphics b, Toolkit tool)
          back = a;
          draw = b;
          toolkit = tool;
     public void draw()
          System.out.println("|     User loading a game     |");
          draw.setColor(Color.black);
          draw.fillRect(0,0,500,500);
          draw.setColor(Color.green);
          draw.drawString("|     User loading a game     |", 100, 150);
import java.awt.*;
import java.awt.event.*;
public class newGame extends Panel
     Graphics draw;
     Image back;
     Toolkit toolkit;
     public void set(Image a, Graphics b, Toolkit tool)
          back = a;
          draw = b;
          toolkit = tool;
     public void draw()
          System.out.println("|     User starting a game    |");
          draw.setColor(Color.white);
          draw.fillRect(0,0,500,500);
          draw.setColor(Color.black);
          draw.drawString("|   User starting a new game  |", 100, 150);
import java.awt.*;
import java.awt.event.*;
public class saveGame extends Panel
     Graphics draw;
     Image back;
     Toolkit toolkit;
     public void set(Image a, Graphics b, Toolkit tool)
          back = a;
          draw = b;
          toolkit = tool;
     public void draw()
          System.out.println("|      User saving a game     |");
          draw.setColor(Color.red);
          draw.fillRect(0,0,500,500);
          draw.setColor(Color.black);
          draw.drawString("|     User saving a game      |", 100, 150);
}

Just a guess: instead of storing a reference to a Graphics object and using it to paint at whatever location in the code, move painting code into an object's paint() method and use the Graphics object passed into paint().
i'm also currently writing a game, and got NullPointerExceptions galore when i tried storing a Graphics reference. Also, this approach helps separate game-state-manipulation code from display code.

Similar Messages

  • Displaying Picture in a Java APPLICATION please help!!

    I have been trying to write a method that Displays a .jpg file when called. However I have not gotten far, every book I have tell you how to display pictures in an applet but not an application. I did find some code that is supposed to be for an application, but It does not compile right. Any help would be apprecidated!
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class PictureIt{
    public void makeImage() {
    //***Image Output Stream
    String imgFile
    = "d:\\temp\\TestImage.jpeg";
    out = new FileOutputStream(imgFile);
    BufferedImage image = null;
    image = (BufferedImage)createImage
    (panelJpeg.size.width, panelJpeg.size.height);
    Graphics g = image.getGraphics();
    panelJpeg.paintAll(g);
    }

    Displaying Picture in a Java APPLICATION please help!!
    Hope this helps.There is going to be two classes compile seperatly first class is what does the drawing
    here it is
    import javax.swing.*;
    import java.awt.*;
    public class draww extends JPanel {
    Image ball;
    int width1 = 100;
    int height1 = 100;
    public draww() {
    super();
    Toolkit kit = Toolkit.getDefaultToolkit();
    ball = kit.getImage("pic1.gif");
    public void paintComponent(Graphics comp) {
    Graphics2D comp2D = (Graphics2D) comp;
    comp2D.drawImage(ball, 20, 20, width1, height1, this);
    sound class is the container JFrame here it is
    import javax.swing.*;
    import java.awt.*;
    public class drawing extends JFrame {
    public drawing() {
    super("draw");
    setSize(400,400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container pane = getContentPane();
    draww d = new draww();
    pane.add(d);
    setContentPane(pane);
    setVisible(true);
    public static void main(String[] args) {
    drawing drawing1 = new drawing();
    PS Hope this helps and see you around

  • Application message help could not be called Message in SABAPDOCU888

    Hi,                                                                      
    After the application of the Support Package 21 in the DEV, help is not functioning correctly, for example, in top of word WRITE (in a publisher ABAP), I pressure F1, opens help correctly, from there pressed in Output you the a list and I go until the end of the one in the point of Additional help.  In that pressed Creating Lists, returns me the Application message help could not be called (Message in. SABAPDOCU888).
    What I must make?                                                       
    Debtor for the attention                                                 
    Rogério Aranda

    I had the same problem when I installed Firefox 3, but I think the problem is due to my recent hard drive upgrade. Try changing the download location from "Desktop" in Microsoft Internet Explorer and all other browsers (Safari, Firefox, etc.) to whatever default location you prefer. I never use Internet Explorer, but I think somehow my settings were corrupted when I upgraded my hard drive. I found this solution on another site also.

  • I lost my ipad mini and i have no set the "Find my iphone/ipad" application. Help me plzzz!!!

    i lost my ipad mini and i have no set the "Find my iphone/ipad" application. Help me plzzz!!!

    You can find serial number in your Support Profile
    https://supportprofile.apple.com/

  • Providing application-level help within Flex apps

    What is the general guideline / approach for help (not
    embedded or field-level help but page level or application level
    help like you get when you click Help in most applications) within
    a Flex app? Is it to spawn a new Window that goes to standard
    HTML-based help engines/content or to display the content in a
    dialog within the Flex application itself? Thanks

    Slow down! Found it here:
    http://raghunathraoflexing.blogspot.com/2006/12/flex-i-frame.html

  • How to uninstall Application host helper service?

    Hi all,
    I have remove IIS features from my server 2008 R2 64bits sp1 but i still see this application host helper service. How can i remove this service from my system?
    Thanks

    HI,
    The ApplicationHost Helper Service in IIS 7.0 and above hosts IIS functionality that is needed even if IIS is not running.
    This service currently hosts the following functionality:
    l  Configuration
    history
    l  Application
    Pool account mapping
    For more information, please refer to the following Microsoft article:
    ApplicationHost Helper Service
    http://www.iis.net/learn/install/installing-iis-7/applicationhost-helper-service
    Source: http://social.technet.microsoft.com/Forums/windowsserver/en-US/33b02e01-acf6-4444-a7ed-5bebade77c10/iis-install-left-behind-application-host-helper-service?forum=winserver8gen
    Regards,
    Rafic
    If you found this post helpful, please give it a "Helpful" vote.
    If it answered your question, remember to mark it as an "Answer".
    This posting is provided "AS IS" with no warranties and confers no rights! Always test ANY suggestion in a test environment before implementing!

  • HT203175 How does one fix the  problemd.... "application itunes helper.exe - mobile me service failed to start becasue missing  MSVCR80.dll" ?  This started after the last "update" of itunes

    With a recent update of itunes, ( a couple weeks ago), when plugging in my iphone, itunes did not automatically launch...and when manually selecting itune to open I recieve the statement ...
    "application itunes helper.exe - mobile me service failed to start becasue missing  MSVCR80.dll" ? 
    I have gone to the itunes site and reloaded itunes....and did not fix the problem. ....help

    Click here and follow the instructions. You may need to completely remove and reinstall iTunes and all related components, or run the process multiple times; this won't normally affect its library, but that should be backed up anyway.
    (100109)

  • After backing up my iPod my applications will not reload and just shows waiting underneath the application any help?

    After backing up my iPod my applications will not reload and just shows waiting underneath the application, any help?

    http://lifehacker.com/5948155/fix-iphone-apps-stuck-waiting-during-installation
    http://alvinalexander.com/apple/iphone-waiting-apps-update-install-reinstall-del ete-ipod

  • I use iphone4,but can't find apple store application please help me

    I use iphone4,but can't find apple store application please help me

    Malek,
    Facetime for iphone is not available in the App Store because it is already built into any iphone that is FaceTime compatible, and I think that includes iphone 4.  You should be able to find Facetime amongst the other built in applications that came with it.  However, it is possible that if it was purchased in certain countries where Facetime is not allowed, the App was not included.

  • My iPad 4 tried to open application but repeatedly iCloud asked for login with apple id password. I put my apple id password correct,even i reset it again but still it does not accept in iPad .i am bored with this 3rd class application.please help

    My iPad 4 tried to open application but repeatedly iCloud asked for login with apple id password. I put my apple id password correctly,even i reset it again but still it does not accepted in iPad .i am bored with this  class application.please help.After every 1 second icloud logoin asked.

    Find Apple ID
    https://iforgot.apple.com/applied
    iPad: Unable to update or restore
    http://support.apple.com/kb/ht4097
    iTunes: Specific update-and-restore error messages and advanced troubleshooting
    http://support.apple.com/kb/TS3694
    If you can’t update or restore your iOS device
    http://support.apple.com/kb/ht1808
     Cheers, Tom

  • Screen Painter help reqired

    Hi I need to create screen like this in screen painter
    create consumption period
    Sales org                        u2022
    Sold to party                  
    Special stock partner      
                                                                                                       october 2008
        november2008
        december2008
    1.The months displayed should be current month minus two months.
    2.The screen should display all months for which a consumption period has not yet been created for the combination Sales org/Sold-to-party/Special-stock-partner.
    3.It should also be possible to scroll back or forward to choose months when there is no room to display on the screen.
    kindly please help in this regards

    In a SAP-training, an employee of SAP said:
    In former times, a modulpool was the only effective way to import data from a user. But now every program should be a report (type 1). A modulpool is only interesting when you desing a very huge application.
    Regards,
    Martin

  • Problem with downloading apps. "Unable to download application" error.Help please!

    I'm having a major issue with updating or downloading apps. They won't download! I try to download an app, it's says "waiting", then it looks like it's about to download. Then, suddenly, the "loading"  bar gets halway filled, then it goes back to saying "waiting". This cycles about three or so times and then I get this message: "Unable to download application. Could not be downloaded at this time" with choices to be "done" or "retry".
    I have tried EVERYTHING to fix this. I tried pausing the download and restarting, I tried soft and hard resets, I tried signing out and back into the App Store, I tried resetting my settings, I tried restoring the phone as a new iPhone, nothing works! I am at my wits end! I have an appointment at the Genius bar tomorrow, but I'm hoping someone here can help me so I won't have to keep that appointment.  I've tried searching for my problem in the forums but none of the results are like mine. Could someone please help?
    EDIT: I also tried checking the iTunes queue. No purchases are stuck in the queue.

    Just wanted to update, since I can no longer edit my original post. I tried disabling bluetooth. That didn't do any good, either.

  • Creating job application page, help.

    I have a job application document that people are able to type into the correct places.  I was wondering how I could put this into one of my webpages so people can still type into the correct places and when they click submit their filled in application is sent to me.  If there is a way to do this or if there is any alternative for a job application page please respond to this email.  Any help is greatly appreiated.

    You'll need to basically re-create the form (I'm assuming it's in Word or PDF format or something similar?) in html using the <form> tag and several <input> tags for the text fields and areas.
    Everything for creating the form will be under Insert > Form in DW
    First you go to Insert > Form > Form and place the actual <form> tagset that will hold everything. Then you use a combination of input text boxes, text areas, check boxes, radio buttons and more from Insert > Form to get the user inputs placed.
    It's relatively straight forward, though getting everything aligned the way you like takes a little more effort. If you poke around online for "Creating forms in Dreamweaver" there are tons of places that give you the basics.
    Once you have the page created, that's when you get the server side script and attach your form to it. What you choose for a processing script depends on what your server supports. Most will support PHP, which is what they use in the link I gave you.

  • I currently use a iMac OS X 10.6.8 and I cannot open iPhoto. Every time I click on iPhoto the coloured wheel comes up and lets me go no further, leaving me with no other option but to force quit the application. HELP!!

    i currently use a iMac OS X 10.6.8 and I cannot open iPhoto. Every time I click on iPhoto the coloured wheel comes up and lets me go no further, leaving me with no other option but to force quit the application.
    it is showing all my existing photos but will not let me click on any of them, nor will it allow me to import any new photos into iPhoto.
    I am really worried that I cannot access all of my photos and even more worried as I need to back them up!!
    HELP!!

    Try repair the Library:
    Option 1
    Back Up and try rebuild the library: hold down the command and option (or alt) keys while launching iPhoto. Use the resulting dialogue to rebuild. Choose to Repair Database. If that doesn't help, then try again, this time using Rebuild Database.
    If that fails:
    Option 2
    Download iPhoto Library Manager and use its rebuild function. (In early versions of Library Manager it's the File -> Rebuild command. In later versions it's under the Library menu.)
    This will create an entirely new library. It will then copy (or try to) your photos and all the associated metadata and versions to this new Library, and arrange it as close as it can to what you had in the damaged Library. It does this based on information it finds in the iPhoto sharing mechanism - but that means that things not shared won't be there, so no slideshows, books or calendars, for instance - but it should get all your events, albums and keywords, faces and places back.
    Because this process creates an entirely new library and leaves your old one untouched, it is non-destructive, and if you're not happy with the results you can simply return to your old one.  
    Regards
    TD

  • Report Painter,help!!

    Dear All,
    When i create a report in Report Painter and execute, it requires insert report in report group, i chose once of report group --> ok, but an error occurs: "Objects with leading zero may not be changed".
    So, what can i do? pls help me.
    Thanks,

    Hi Sachin,
    We are using 6.0 and new GL is active for we cant use GLPCT ( which has this characterisctic of stat key figure) but FAGLFLEXt doesnt. We need to use profit center so cant use cost center/element.
    Can some one please advice how to add a characteristic to the library when the charcteristic isnt available in the table as well. Is this possible?
    Any input will be appreciated
    Regards,
    Zaid Azam

Maybe you are looking for

  • Report on PO status

    is there any report / transaction which is used to monitor PO status thank you

  • How to create a roster sheet in sharepoint 2010

    Hi This is about training management, when an employee requests for training, it goes to approval workflow and once approved, it is like the employee is enrolled. Once it is approved I want a roster sheet to be created for that course for all the emp

  • Solaris 10 with zfs root install and VMWare-How to grow disk?

    I have a Solaris 10 instance installed on an ESX host. During the install, I selected a 20gig disk. Now, I would like to grow the disk from 20GB to 25GB, I made the change on VMWare but now the issue seems to be Solaris. I haven't seen anything on ho

  • Grand Total is not showing

    Good Day everyone, I have created a report in Discoverer Desktop Edition (Release 4.1), at first, it shows the grand total for the Budget column but when I added a calculated columns "Actual Cost" and "year-to-date", then the grand total no longer di

  • BTBBaw sim in iPhone 4

    Hello every one I have successfully installed sip and have working BBtalk making and receiving calls with great help from Malcolm's contribution But I am unable to connect via BTBBaw SIM when not in range of wifi and can not use data download functio