Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Jumei

[java] Graphics Again : |

This topic is 5271 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Alright this is what ive come up with so far... but my images doesnt appear and my key thing doesnt work either... Why is this?

import javax.swing.JFrame;
   import java.awt.Color;
   import java.awt.Font;
   import java.awt.Graphics;
   import javax.swing.ImageIcon;
   import java.awt.event.*;


   public class Game01 extends JFrame

   {
      private Graphics g;
      private KeyListener myKeyListener;

      int myX = 250;
      int myY = 250;
      ImageIcon myPerson = new ImageIcon("myPerson.jpg");

      public static void main(String[] args)
      {
         Game01 main = new Game01();
         
         main.mainloop();
         
      }// End main


      public Game01()

      {
         this.setTitle("GameA01");
         this.setSize(500, 500);
         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      // Display graphics window

         this.setVisible(true);

      }// End GameA01



      public void update(Graphics g)


      {
         paint(g);
      }// End update



      public void paint(Graphics g)

      {
         setBackground(Color.white);
         g.drawImage(myPerson.getImage(), myX, myY, null);
      }// End paint


      public void mainloop()


      {
         while(true)
         {
            repaint();
         }// End while


      }// End mainloop

      
      class myKeyListener implements KeyListener


      {


         public void keyPressed(java.awt.event.KeyEvent myKeyEvent)


         {
            int myKeyCode = myKeyEvent.getKeyCode();
            switch (myKeyCode)
            {
               case KeyEvent.VK_UP:
                  myY = myY + 1;
                  g.drawImage(myPerson.getImage(), myX, myY, null);
                  repaint();
                  break;
            }// End switch


         }// End keyPressed


         public void keyReleased(java.awt.event.KeyEvent mykeyEvent)


         {
         }// End keyReleased



         public void keyTyped(java.awt.event.KeyEvent mykeyEvent)


         {
         }// End keyTyped


      }// End myKeyListener


   }// End Game01


Share this post


Link to post
Share on other sites
Advertisement
your image doesn't appear because it hasn't loaded until after the window paints for the first time. You will have to implement an ImageListener to recieve the image loading event, and then call repaint() once the image is loaded.

Your code, I believe, is in error. You have

private KeyListener myKeyListener;

and then later

class myKeyListener implements KeyListener{
}


you should change the first line to

private myKeyListener someKeyListener;

and then add in the constructor

someKeyListener=new myKeyListener();

Your key listener doesn't work because you haven't registered a key listener in the constructor. Somewhere in the constructor, you should have a line

this.addKeyListener(myKeyListener);

finally, you should probably remove the g.drawImage() from the keyboard event, because directly after that you call repaint() which will eventually draw the image a second time, for one keyboard event. Also, get rid of the class level data member Graphics g (first line of the class). You can't store references to graphics objects, it creates some really funky results (i.e. NOT what you would ever want). I can see you are doing this so that you can draw in the key event, but again, I don't think you should be drawing in the key event in the first place.

In your main method, you name the reference to your JFrame as main. I didn't think that was possible, and I highly discourage it if it is.


capn_midnight | Captain Midnight | deviantArt
ACM | SIGGRAPH | Generation5
"I'm tired of all this nonsense about beauty being only skin-deep. That's deep enough. What do you want - an adorable pancreas?" -Jean Kerr

[edited by - capn_midnight on June 8, 2004 10:55:51 AM]

Share this post


Link to post
Share on other sites
Alright, i see what you mean... thanks... ok my idea was to basically have an image in a window and use the arrow keys to move it around... how else could i do this? Is there a better, yet simple way to do this with something other than the lame graphics2d window thing?

Could you show an example or something for a refrence? examples are good :D

Thanks for the help


... Also i dont quite understand why my image wont load... what did you mean?


Haha whoa yeah i wasnt even thinking about the main... i was just trying to keep it organized... hahah thanks

[edited by - Jumei on June 8, 2004 11:13:25 AM]

Share this post


Link to post
Share on other sites
In your paint segment where you paint the image , change the component to this(ie g.drawImage(myPicture , myX , myY , this) ) instead of null. the null means you aren''t painting anywhere.

Share this post


Link to post
Share on other sites
quote:
Original post by Jumei
Alright, i see what you mean... thanks... ok my idea was to basically have an image in a window and use the arrow keys to move it around... how else could i do this? Is there a better, yet simple way to do this with something other than the lame graphics2d window thing?
Could you show an example or something for a refrence? examples are good :D

Thanks for the help



I'll work something up later today.
quote:

... Also i dont quite understand why my image wont load... what did you mean?


It's not that the image isn't loading (well, it's impossible for me to say, you could have the image in the wrong directory, I can't tell). The image is indeed loading, it's just that it takes a certain amount of time to load, and that time is usually AFTER the first paint call. with repaint() I believe it fires update() and not paint() specifically, so if the display hasn't updated (i.e., the image reference hasn't changed or changed locations) then it won't redraw anything.

make sure the image is in the same directory as the program, though you've probably already done this.



capn_midnight | Captain Midnight | deviantArt
ACM | SIGGRAPH | Generation5
"I'm tired of all this nonsense about beauty being only skin-deep. That's deep enough. What do you want - an adorable pancreas?" -Jean Kerr

[edited by - capn_midnight on June 8, 2004 12:58:52 PM]

Share this post


Link to post
Share on other sites
In case it was missed earlier, I believe you need to import something like "javax.imageio.plugins.jpeg", if you use a jpeg, and something else for a bmp.

Share this post


Link to post
Share on other sites
quote:
Original post by ManiacMac
In case it was missed earlier, I believe you need to import something like "javax.imageio.plugins.jpeg", if you use a jpeg, and something else for a bmp.


really? I''ve never done it.



capn_midnight | Captain Midnight | deviantArt
ACM | SIGGRAPH | Generation5
"I''m tired of all this nonsense about beauty being only skin-deep. That''s deep enough. What do you want - an adorable pancreas?" -Jean Kerr

Share this post


Link to post
Share on other sites
quote:
Original post by Sir Sapo
In your paint segment where you paint the image , change the component to this(ie g.drawImage(myPicture , myX , myY , this) ) instead of null. the null means you aren''t painting anywhere.

Since when? The 4th argument points to an ImageConsumer. If you set it to null, it just means that no one will be notified when the image is painted. It will still get painted to the Graphics context.

quote:
Original post by ManiacMac
In case it was missed earlier, I believe you need to import something like "javax.imageio.plugins.jpeg", if you use a jpeg, and something else for a bmp.


This is not required. You can load JPEG, GIF and PNG using just the standard loaders.

Jumei:
I ran your program and it worked fine, except for the KeyListener. I modified it to get the KeyListener to work.
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import java.awt.event.*;

public class Game01 extends JFrame implements KeyListener {
private Graphics g;
private KeyListener myKeyListener;

int myX = 250;
int myY = 250;
ImageIcon myPerson = new ImageIcon("myPerson.jpg");

public static void main(String[] args) {
Game01 main = new Game01();
main.mainloop();

}// End main


public Game01() {
this.setTitle("GameA01");
this.setSize(500, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addKeyListener(this);

// Display graphics window

this.setVisible(true);

}// End GameA01



public void update(Graphics g) {
paint(g);
}// End update



public void paint(Graphics g) {
setBackground(Color.white);
g.drawImage(myPerson.getImage(), myX, myY, null);
}// End paint


public void mainloop() {
while(true) {
repaint();
}// End while

}// End mainloop


public void keyPressed(java.awt.event.KeyEvent myKeyEvent) {
int myKeyCode = myKeyEvent.getKeyCode();
switch (myKeyCode) {
case KeyEvent.VK_UP:
myY = myY - 1;
repaint();
break;
}// End switch


}// End keyPressed


public void keyReleased(java.awt.event.KeyEvent mykeyEvent) {}// End keyReleased



public void keyTyped(java.awt.event.KeyEvent mykeyEvent) {}// End keyTyped


}// End Game01





First make it work, then make it fast. --Brian Kernighan

The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities. We need men and women who can dream of things that never were. - John Fitzgerald Kennedy(35th US President)

Do not interrupt your enemy when he is making a mistake. - Napolean Bonaparte

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!