Jump to content
  • Advertisement
Sign in to follow this  

[java] movement + OO buttons

This topic is 4720 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

programing n00b here with java so working with an applet i got 2 questions 1. being how can i add like itemlistener or some form of listner to a Image aka my own created button like menu etc... and second what am i doing wrong here this character wont move... i think its because i did not add keylistener or something, but im not sure where to add it... my code is this
// The "test" class.
import java.applet.*;
import java.awt.*;
import hsa.*;
import java.awt.event.*;

public class test extends Applet implements KeyListener
{
    Image chipp_mov;
    int movesX = 0;
    int moves;
    int charX = 0;

    public void init ()
    {

        chipp_mov = getImage (getCodeBase (), "chipp/chipp2.PNG");

    } // init method


    public void keyTyped (KeyEvent e)
    {
    }


    public void keyPressed (KeyEvent e)
    {

        if (e.getKeyCode () == Event.RIGHT)
        {
            movesX = 1;
        }
        if (e.getKeyCode () == Event.LEFT)
        {
            movesX = -1;
        }
        if (e.getKeyCode () == KeyEvent.VK_C)
        {
            moves = -1;
        }
        if (e.getKeyCode () == KeyEvent.VK_B)
        {
            moves = -1;
        }
        if (e.getKeyCode () == KeyEvent.VK_Q)
        {
            moves = -1;
        }
    }


    public void keyReleased (KeyEvent e)
    {
    }


    public void paint (Graphics g)
    {

        g.drawImage (chipp_mov, charX, 0, this);

    } // paint method


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


    public class MyListener implements ActionListener
    {
        public void actionPerformed (ActionEvent ae)
        {
            if (movesX == -1)
            {
                charX += 2;
            } //end if
            if (movesX == 1)
            {
                charX += 2;
            } //end if
            repaint ();
        } // end actionPerformed method
    } // end my Listener class
} // test class

thx a million for all the help i get :) (-- Edit: changed the quote tags to source tags -- Kylotan) [Edited by - Kylotan on June 17, 2005 5:38:06 PM]

Share this post


Link to post
Share on other sites
Advertisement
I'm gonna move this to the Java forum as the people over there are better equipped to answer this question.

You may be able to do 'addKeyListener(this)' in the init() function of your applet. That will call the relevant functions you've defined when you press a key, I think.

I don't think your 'MyListener' class serves any purpose in the code you have already, as I don't think you generate any ActionEvents, never mind the fact that you don't set anything up to listen for them.

However I am far from being a Java expert so maybe someone else has some better answers for you. :)

Share this post


Link to post
Share on other sites
hum ok i got it all fixed up for keys work finaly now :) now all i need to figure out is how i can make my image be a button like when i click on it how i can make it like work with it ItemListener i've tried that did not work... any ideas?

and this is my key event program :)

[source lang="javascript// The "test" class.

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

public class test extends Applet implements KeyListener
{
Image chipp_mov;
int direction = 0;

public void init ()
{
addKeyListener (this);
chipp_mov = getImage (getCodeBase (), "chipp/chipp2.PNG");

} // init method


public void keyTyped (KeyEvent e)
{
}


public void keyPressed (KeyEvent e)
{
int key = e.getKeyCode (); // Keyboard code for the pressed key.

if (key == KeyEvent.VK_LEFT)
{
direction = 2;
repaint ();
}
else if (key == KeyEvent.VK_RIGHT)
{
direction = 1;
repaint ();
}
else if (key == KeyEvent.VK_C)
{
direction = 1;
repaint ();
}
else if (key == KeyEvent.VK_B)
{
direction = 1;
repaint ();
}
else if (key == KeyEvent.VK_Q)
{
direction = 1;
repaint ();
}
}


public void keyReleased (KeyEvent e)
{
direction = 0;
}


public void paint (Graphics g)
{

if (direction == 1)
{
g.drawImage (chipp_mov, 0, 0, this);
}
if (direction == 2)
{
g.drawImage (chipp_mov, 0, 0, this);
}

} // paint method


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



[Edited by - trancein4l on June 17, 2005 7:39:33 PM]

Share this post


Link to post
Share on other sites
I don't think Image has any listener methods so you can't do it that way. I can't see any obvious way of achieving what you want with java.awt classes, except perhaps catching the event at the Applet level (by calling addMouseListener on it and implementing the MouseListener interface) and checking if the position of the event overlaps the Image by checking Image.getBounds().

Again, I stress I am no Java expert so maybe there's a better way. I'd hope there is, anyway...

Share this post


Link to post
Share on other sites
well that was a fail, hum but moving away from that i got 2 more qustions is there a way to make image transparent .... that way i can check for attacks easier and... as i have started using bufferGraphics.drawWatever instead of g.drawWatever it seems that it loads super slow... i have to minimize the window and maximize it again for graphics to show is there a way to load all images at once so there is like not much load necesary time... this is what i got

 
import java.applet.*;
import java.awt.*;
import hsa.*;
import java.awt.event.*;
import javax.swing.*;

public class GlobalSamurai extends Applet implements KeyListener
{
Image option;
Image charselect;
Image Main;
Image main_b;
Image start_b;
Image rules_b;

Button start;
Button rules;
Button main;

MyListener Listener = new MyListener ();

Image chipp_mov[] = new Image [10];
Image haohmaru_mov[] = new Image [10];
Image shinobi_mov[] = new Image [10];
Image silent_mov[] = new Image [10];
Image Lifebar[] = new Image [1];

int direction = 0;
int playerX = 0;
int charchoice = 0;
int clicks = 0;

Dimension dim;
Image offscreen;
Graphics bufferGraphics;


public void boundries_MOVE ()
{
playerX++;
if (playerX == -1) // edge of the screen on the left
playerX = 0;
}


public void init ()
{
// display images
Main = getImage (getCodeBase (), "misc/main.PNG");
charselect = getImage (getCodeBase (), "misc/charselect.PNG");
option = getImage (getCodeBase (), "misc/options.PNG");
main_b = getImage (getCodeBase (), "misc/bmain.PNG");
start_b = getImage (getCodeBase (), "misc/bstart.PNG");
rules_b = getImage (getCodeBase (), "misc/brules.PNG");

// character select

start = new Button ("Start");
rules = new Button ("Rules");
main = new Button ("Main Menu");

// add initials
add (start);
add (rules);
add (main);

start.addActionListener (Listener);
rules.addActionListener (Listener);
main.addActionListener (Listener);

addKeyListener (this);
dim = getSize ();
setBackground (Color.white);
offscreen = createImage (dim.width, dim.height);
bufferGraphics = offscreen.getGraphics ();

//move characters

chipp_mov [0] = getImage (getCodeBase (), "chipp/chipp1.PNG");
chipp_mov [1] = getImage (getCodeBase (), "chipp/chipp2.PNG");
chipp_mov [2] = getImage (getCodeBase (), "chipp/chipp3.PNG");
chipp_mov [3] = getImage (getCodeBase (), "chipp/chipp4.PNG");
chipp_mov [4] = getImage (getCodeBase (), "chipp/chipp5.PNG");
chipp_mov [5] = getImage (getCodeBase (), "chipp/rchipp1.PNG");
chipp_mov [6] = getImage (getCodeBase (), "chipp/rchipp2.PNG");
chipp_mov [7] = getImage (getCodeBase (), "chipp/rchipp3.PNG");
chipp_mov [8] = getImage (getCodeBase (), "chipp/rchipp4.PNG");
chipp_mov [9] = getImage (getCodeBase (), "chipp/rchipp5.PNG");
haohmaru_mov [0] = getImage (getCodeBase (), "haohmaru/haohmaru1.PNG");
haohmaru_mov [1] = getImage (getCodeBase (), "haohmaru/haohmaru2.PNG");
haohmaru_mov [2] = getImage (getCodeBase (), "haohmaru/haohmaru3.PNG");
haohmaru_mov [3] = getImage (getCodeBase (), "haohmaru/haohmaru4.PNG");
haohmaru_mov [4] = getImage (getCodeBase (), "haohmaru/haohmaru5.PNG");
haohmaru_mov [5] = getImage (getCodeBase (), "haohmaru/rhaohmaru1.PNG");
haohmaru_mov [6] = getImage (getCodeBase (), "haohmaru/rhaohmaru2.PNG");
haohmaru_mov [7] = getImage (getCodeBase (), "haohmaru/rhaohmaru3.PNG");
haohmaru_mov [8] = getImage (getCodeBase (), "haohmaru/rhaohmaru4.PNG");
haohmaru_mov [9] = getImage (getCodeBase (), "haohmaru/rhaohmaru5.PNG");
shinobi_mov [0] = getImage (getCodeBase (), "shinobi/shinobi1.PNG");
shinobi_mov [1] = getImage (getCodeBase (), "shinobi/shinobi2.PNG");
shinobi_mov [2] = getImage (getCodeBase (), "shinobi/shinobi3.PNG");
shinobi_mov [3] = getImage (getCodeBase (), "shinobi/shinobi4.PNG");
shinobi_mov [4] = getImage (getCodeBase (), "shinobi/shinobi5.PNG");
shinobi_mov [5] = getImage (getCodeBase (), "shinobi/rshinobi1.PNG");
shinobi_mov [6] = getImage (getCodeBase (), "shinobi/rshinobi2.PNG");
shinobi_mov [7] = getImage (getCodeBase (), "shinobi/rshinobi3.PNG");
shinobi_mov [8] = getImage (getCodeBase (), "shinobi/rshinobi4.PNG");
shinobi_mov [9] = getImage (getCodeBase (), "shinobi/rshinobi5.PNG");
silent_mov [0] = getImage (getCodeBase (), "silent/silent1.PNG");
silent_mov [1] = getImage (getCodeBase (), "silent/silent2.PNG");
silent_mov [2] = getImage (getCodeBase (), "silent/silent3.PNG");
silent_mov [3] = getImage (getCodeBase (), "silent/silent4.PNG");
silent_mov [4] = getImage (getCodeBase (), "silent/silent5.PNG");
silent_mov [5] = getImage (getCodeBase (), "silent/rsilent1.PNG");
silent_mov [6] = getImage (getCodeBase (), "silent/rsilent2.PNG");
silent_mov [7] = getImage (getCodeBase (), "silent/rsilent3.PNG");
silent_mov [8] = getImage (getCodeBase (), "silent/rsilent4.PNG");
silent_mov [9] = getImage (getCodeBase (), "silent/rsilent5.PNG");
Lifebar [0] = getImage (getCodeBase (), "misc/lifebar_full.PNG");

} // init method


public void keyTyped (KeyEvent e)
{
}


public void keyPressed (KeyEvent e)
{
int key = e.getKeyCode (); // Keyboard code for the pressed key.

if (key == KeyEvent.VK_LEFT)
{
direction = 2;
playerX -= 10;
repaint ();
}
else if (key == KeyEvent.VK_RIGHT)
{
direction = 1;
playerX += 10;
repaint ();
}
else if (key == KeyEvent.VK_C)
{
direction = 3;
repaint ();
}
else if (key == KeyEvent.VK_B)
{
direction = 4;
playerX = 0;
repaint ();
}
else if (key == KeyEvent.VK_Q)
{
direction = 5;
repaint ();
}
}


public void keyReleased (KeyEvent e)
{
}


public void paint (Graphics g)
{

bufferGraphics.clearRect (0, 0, dim.width, dim.height);
bufferGraphics.drawImage (Lifebar [0], 110, 10, this);

for (int x = 0 ; x < 5 ; x++)
{ // chipp moves
bufferGraphics.drawImage (chipp_mov [5], x + 1, 350, this);
if (direction == 1)
{
bufferGraphics.drawImage (chipp_mov [5], playerX, 350, this);
}
if (direction == 2)
{
bufferGraphics.drawImage (chipp_mov [5], playerX, 350, this);
}
if (direction == 3)
{
bufferGraphics.drawImage (chipp_mov [6], playerX, 350, this);
}
if (direction == 4)
{
bufferGraphics.drawImage (chipp_mov [9], playerX, 350, this);
}
} // end for


if (charchoice == 2)
{
for (int x = 0 ; x < 5 ; x++)
{ // haohmaru moves
bufferGraphics.drawImage (haohmaru_mov [5], playerX, 350, this);
if (direction == 1)
{
bufferGraphics.drawImage (haohmaru_mov [2], playerX, 350, this);
}
if (direction == 2)
{
bufferGraphics.drawImage (haohmaru_mov [3], playerX, 350, this);
}
if (direction == 3)
{
bufferGraphics.drawImage (haohmaru_mov [3], playerX, 350, this);
}
if (direction == 4)
{
bufferGraphics.drawImage (haohmaru_mov [3], playerX, 350, this);
}
} // end for
} // end charchoice


if (charchoice == 3)
{
for (int x = 0 ; x < 5 ; x++)
{ // shinobi moves
if (direction == 1)
{
bufferGraphics.drawImage (shinobi_mov [2], playerX, 350, this);
}
if (direction == 2)
{
bufferGraphics.drawImage (shinobi_mov [3], playerX, 350, this);
}
if (direction == 3)
{
bufferGraphics.drawImage (shinobi_mov [3], playerX, 350, this);
}
if (direction == 4)
{
bufferGraphics.drawImage (shinobi_mov [3], playerX, 350, this);
}
} // end for
} // end char choice


if (charchoice == 4)
{
for (int x = 0 ; x < 5 ; x++)
{ // silent moves
if (direction == 1)
{
bufferGraphics.drawImage (silent_mov [2], playerX, 350, this);
}
if (direction == 2)
{
bufferGraphics.drawImage (silent_mov [3], playerX, 350, this);
}
if (direction == 3)
{
bufferGraphics.drawImage (silent_mov [3], playerX, 350, this);
}
if (direction == 4)
{
bufferGraphics.drawImage (silent_mov [3], playerX, 350, this);
}
} // end for
} // end charchoice

if (clicks == 0)
{
bufferGraphics.drawImage (Main, 0, 0, this);
bufferGraphics.drawImage (start_b, 40, 145, this);
bufferGraphics.drawImage (rules_b, 65, 200, this);
start.setVisible (true);
rules.setVisible (true);
main.setVisible (false);
}

if (clicks == 1)
{
start.setVisible (false);
rules.setVisible (false);
main.setVisible (true);
bufferGraphics.drawImage (charselect, 0, 0, this);
bufferGraphics.drawImage (main_b, 330, 535, this);
}

if (clicks == 2)
{
start.setVisible (false);
rules.setVisible (false);
main.setVisible (true);
bufferGraphics.drawImage (option, 0, 0, this);
bufferGraphics.drawImage (main_b, 535, 525, this);
}
g.drawImage (offscreen, 0, 0, this);
} // paint method


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


public class MyListener implements ActionListener
{
public void actionPerformed (ActionEvent ae)
{
if (ae.getSource () == start)
{
clicks = 1;
} //end if
if (ae.getSource () == rules)
{
clicks = 2;
} //end if
if (ae.getSource () == main)
{
clicks = 0;
} //end if
} // end actionPerformed method
} // end my Listener class
} // Global class

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!