Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Image not responding to keyboard?

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Image not responding to keyboard?

    So as an elective, I took Game design in Java in my school and I have to make a really simple game for it. Basically, what I'm trying to do is get the bullets (titled as 'bullet) to shoot out from the character. This is what I have and I'm having trouble seeing what's wrong with it. Thanks in advanced!

    Code is below, here's a link to the project file: http://mediafire.com/?q6ysy1hbkyasnqt

    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;

    public class MyGame extends Applet implements Runnable {
    Image background;
    Image figure;
    AudioClip Ninja;
    AudioClip bang;

    // Variables for application
    int width = 700;
    int height = 300;
    int tilesx = 1;
    int tilesy = 1;
    int lives = 3;

    // Variables for ninja;
    int Ninja_pos = 20;
    int NinjaY = 150;

    // Variables for bullet:
    boolean bullet = false;
    int bullet_x = Ninja_pos;
    int bullet_y = NinjaY + 50;
    int bullet_speed = 2;


    public void init() {
    bang = getAudioClip(getCodeBase(), "bang.au");
    Ninja = getAudioClip(getCodeBase(), "Ninja.au");
    Ninja.play();
    for (int y = 0; y < tilesy; y++) {

    for (int x = 0; x < tilesx; x++) {
    }
    background = getImage(getCodeBase(), "Background.gif");
    figure = getImage(getCodeBase(), "Figure.png");
    setSize(700, 300);
    }

    }

    public void start() {
    Thread th = new Thread(this);
    th.start();
    }

    public void run() {
    Thread.currentThread().setPriority(Thread.MIN_PRIO RITY);


    while(true)
    {
    draw_bullet();

    try
    {
    Thread.sleep(5);
    }
    catch(InterruptedException x)
    {}

    Thread.currentThread().setPriority(Thread.MAX_PRIO RITY);
    }
    }

    private void draw_bullet()
    {
    if(bullet)
    {
    bullet_y = bullet_y - bullet_speed;
    bullet_x = Ninja_pos;
    }
    }
    public void paint(Graphics g) {
    g.drawImage(background, 0, 0, this);
    g.drawImage(figure, Ninja_pos, NinjaY, this);
    g.setFont(new Font("TimesRoman", Font.PLAIN, 18));
    g.setColor(Color.WHITE);
    g.drawString("Lives: " + lives, 25, 25);

    if(bullet)
    {
    g.setColor(Color.BLACK);
    g.fillRect(bullet_x, bullet_y, 150, 150);
    }

    }

    // Keyboard
    // ************************ KeyDown *************************************
    public boolean keyDown(Event evt, int key) {
    if (key == Event.LEFT) {
    figure = getImage(getCodeBase(), "Figure2.png");
    Ninja_pos = Ninja_pos - 5;
    if(Ninja_pos<=30){
    figure = getImage(getCodeBase(), "Figure.png");
    Ninja_pos=30;
    }
    repaint();
    }
    if (Ninja_pos <= 0) {
    Ninja_pos = 0;
    }

    if (key == Event.RIGHT) {
    figure = getImage(getCodeBase(), "Figure.png");
    Ninja_pos += 5;
    if (Ninja_pos >= 640) {
    figure = getImage(getCodeBase(), "Figure2.png");
    Ninja_pos = 640;
    }
    repaint();
    }

    if (key == 32){
    bang.play();
    }

    return false;

    }

    // Double buffering
    private Image dbImage;
    private Graphics dbg;

    /** Update - Method, implements double buffering */
    public void update(Graphics g) {
    // initialize buffer
    if (dbImage == null) {
    dbImage = createImage(this.getSize().width, this.getSize().height);
    dbg = dbImage.getGraphics();

    }

    // clear screen in background
    dbg.setColor(getBackground());
    dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);


    // draw elements in background
    dbg.setColor(getForeground());
    paint(dbg);

    // draw image on the screen
    g.drawImage(dbImage, 0, 0, this);

    } }
    Last edited by uhKenKaniff; December 11th, 2011 at 07:51 PM.


  2. #2
    Junior Member
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Image not responding to keyboard?

    I don't mean to be a nuisance, but can anyone help?

    If it makes it any easier, I"ll be uploading the rpoject file.

    Edit: Here it is http://mediafire.com/?q6ysy1hbkyasnqt I'll be adding it to the opening post as well
    Last edited by uhKenKaniff; December 11th, 2011 at 06:16 PM.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,296
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Image not responding to keyboard?

    This is what I have and I'm having trouble seeing what's wrong with it.
    We have trouble as well, given you haven't said anything about what is wrong (does it compile? exception? behavior? what exactly about the behavior do you expect, and what do you get?)

    Please see the link in my signature entitled 'Getting Help' - it has a lot of advice that, if used, will help you phrase you question and make it much more answerable

  4. #4
    Junior Member
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Image not responding to keyboard?

    Quote Originally Posted by copeg View Post
    We have trouble as well, given you haven't said anything about what is wrong (does it compile? exception? behavior? what exactly about the behavior do you expect, and what do you get?)
    The forum itself is titled "What's wrong with my code?" so I imagined that the more experienced java users were to aid me in finding the errors; afterall, if I knew what was wrong, given the simplicity of the project I don't think I'd be seeking help.

    But being honest, I don't know how much more specific I can get given my limited java experience, but I'll try. The problem isn't getting input from the keyboard, as in the code above I tell it to play a clip whenever I press space and it does that properly, but more so actually painting the bullet and getting it to move across the screen when I press the space bar. Our teacher gave us a sample code with a bullet moving across the screen, and I believe that I copied it very well and made changes where it was necessary to. If it's of any help,here's an example of the working bullet coding.

    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;

    public class shoot extends Applet implements Runnable {
    // Variables for application
    int appwidth = 700;
    int appheight = 400;
    int skyheight = 300;

    // Variables for gun
    int width = 6;
    int height = 50;
    int top_x = 350;
    int top_y = 290;
    int speed = 5;

    // gun base
    int base_width = 36;
    int base_height = 50;
    int base_x = top_x - 15;
    int base_y = top_y + (height / 2);

    // bullet
    boolean bullet = false;
    int bullet_x = top_x;
    int bullet_y = top_y;
    int bullet_speed = 2;

    AudioClip bang;

    public void init() {
    setSize(appwidth, appheight);

    bang = getAudioClip(getCodeBase(), "explosion.au");
    }

    public void start() {
    Thread th = new Thread(this);
    th.start();
    }

    public void run() {

    Thread.currentThread().setPriority(Thread.MIN_PRIO RITY);

    while (true) {
    draw_bullet();

    repaint();

    try {
    Thread.sleep(5);
    } catch (InterruptedException x) {

    }

    Thread.currentThread().setPriority(Thread.MAX_PRIO RITY);

    }

    }

    private void draw_bullet() {
    if (bullet) {
    bullet_y = bullet_y - bullet_speed;
    bullet_x = top_x;
    if (bullet_y < 0) {
    bullet_y = top_y;
    bullet = false;
    }
    }
    }

    public void paint(Graphics g) {
    // Draw sky
    g.setColor(Color.CYAN);
    g.fillRect(0, 0, appwidth, skyheight);

    // Draw grass
    g.setColor(Color.GREEN);
    g.fillRect(0, skyheight, appwidth, appheight - skyheight);

    // Draw gun
    g.setColor(new Color(126, 34, 23));
    g.fillRect(top_x, top_y, width, height);

    // gun base
    g.fillArc(base_x, base_y, base_width, base_height, 0, 180);

    // Draw bullet
    if (bullet) {
    g.setColor(Color.BLACK);
    g.fillRect(bullet_x, bullet_y, width, width);
    }

    }

    // Keyboard
    // ************************ KeyDown *************************************
    public boolean keyDown(Event evt, int key) {
    if (key == Event.LEFT) {
    top_x = top_x - speed;

    if (top_x <= 0)
    top_x = 0;
    }

    if (key == Event.RIGHT) {
    top_x = top_x + speed;

    if (top_x >= appwidth - width)
    top_x = appwidth - width;
    }

    if (key == Event.UP)
    top_x = 0;

    if (key == Event.DOWN)
    top_x = appwidth - width;

    // base position
    base_x = top_x - 15;
    base_y = top_y + (height / 2);

    if (key == 32) {
    bullet = true;
    bang.play();
    }

    return true;

    }

    // ************************ KeyUp *************************************
    public boolean keyUp(Event evt, int key) {
    return true;
    }

    // Double buffering
    private Image dbImage;
    private Graphics dbg;

    /** Update - Method, implements double buffering */
    public void update(Graphics g) {
    // initialize buffer
    if (dbImage == null) {
    dbImage = createImage(this.getSize().width, this.getSize().height);
    dbg = dbImage.getGraphics();

    }

    // clear screen in background
    dbg.setColor(getBackground());
    dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);

    // draw elements in background
    dbg.setColor(getForeground());
    paint(dbg);

    // draw image on the screen
    g.drawImage(dbImage, 0, 0, this);

    }
    }

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,296
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Image not responding to keyboard?

    Did you get to the part in the link I provided that mentions to use the code tags? You can edit your post and wrap your code within - this preserves indenting and highlights and makes code readable

    The problem isn't getting input from the keyboard, as in the code above I tell it to play a clip whenever I press space and it does that properly, but more so actually painting the bullet and getting it to move across the screen when I press the space bar. Our teacher gave us a sample code with a bullet moving across the screen, and I believe that I copied it very well and made changes where it was necessary to. If it's of any help,here's an example of the working bullet coding.
    From what I can gather from your code I will give 2 pieces of advice which are major but necessary changes:
    1) keyDown/keyUp methods have been deprecated since java 1.1 - I highly recommend using a KeyListener ( see How to Write a Key Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners) )
    2) You should be very careful when threading and accessing values from multiple threads. The event listener model works from one thread, and you update from another - which can cause severe problems when each tries to access/set/get the same value at the same time. You can overcome this by synchronizing the changes. See Lesson: Concurrency (The Java™ Tutorials > Essential Classes)

  6. #6
    Junior Member
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Image not responding to keyboard?

    I managed to fix the problem by playing around a little, so this thread can be closed.

    I appreciate your input, copeg, and will be sure to use your advice next time I post in this forum.

Similar Threads

  1. Create image Jpeg from an object of Image class.
    By Ramandeep in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: December 31st, 2011, 10:34 PM
  2. Replies: 2
    Last Post: February 14th, 2011, 04:36 PM
  3. How to connect keyboard through COM?
    By yogesh01 in forum Java Theory & Questions
    Replies: 0
    Last Post: July 6th, 2010, 05:00 AM
  4. Pixel Alteration in an image/image rotation
    By bguy in forum Java Theory & Questions
    Replies: 3
    Last Post: November 1st, 2009, 09:50 AM
  5. how to know user pressed a key in the keyboard
    By cilang in forum File I/O & Other I/O Streams
    Replies: 16
    Last Post: September 11th, 2009, 10:08 AM