Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Firing off events without user input
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
zuber




PostPosted: Tue May 27, 2008 8:10 am   Post subject: Firing off events without user input

I was looking into making an RPG template for later use, and I stumbled (rather by accident ) upon what I thought was an interesting way to avoid the whole flickering screen problem. Essentially what I'm doing is writing the program as an applet with bits of JFrame in it:
(Note that this was edited slightly to be useable by people without my pictures)
code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class MapTemplate extends JApplet
    implements KeyListener, MouseListener
{


    static final int SQUARE_SIZE = 25;

    Color squareColor;
    int squareTop, squareLeft;
    int xPos, yPos;
    char[] [] state = new char [40] [21];
    boolean focussed = false;
    boolean[] keyPressed = new boolean [4];
    DisplayPanel canvas;
    //Image grass = Toolkit.getDefaultToolkit ().getImage ("grass.JPG");
    //Image wall = Toolkit.getDefaultToolkit ().getImage ("wall.JPG");
    public void init ()
    {
        //***************************************************************************************************
        for (int j = 0 ; j < 40 ; j++)
            for (int h = 0 ; h < 21 ; h++)
                state [j] [h] = 'e';
        for (int j = 0 ; j < 10 ; j++)
            state [20] [j] = 'f';
        for (int j = 0 ; j < 10 ; j++)
            state [j] [15] = 'f';
        for (int k = 0 ; k < 3 ; k++)
            // DL [k] = Toolkit.getDefaultToolkit ().getImage ("dl" + (k + 1) + ".jpg");
            //***************************************************************************************************
            squareTop = 0;
        squareLeft = 0;
        squareColor = Color.red;

        canvas = new DisplayPanel ();
        setContentPane (canvas);
        canvas.setBackground (Color.white);

        canvas.addKeyListener (this);
        canvas.addMouseListener (this);
        xPos = 0;
        yPos = 0;
    }


    class DisplayPanel extends JPanel
    {
        //***************************************************************************************************

        public void paintComponent (Graphics g)
        {

            super.paintComponent (g);


            int width = getSize ().width;
            int height = getSize ().height;
            drawGrid (g);
            g.setColor (squareColor);

            g.fillRect (xPos * SQUARE_SIZE, yPos * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE);

        }

    }


    //***************************************************************************************************

    public void drawGrid (Graphics g)
    {

        for (int x = 0 ; x < 40 ; x++)
            for (int y = 0 ; y < 20 ; y++)
                if (state [x] [y] != 'e')
                {
                    g.setColor (Color.black);
                    g.fillRect (x * 25, y * 25, 25, 25);
                }
        //  g.drawImage (wall, x * 25, y * 25, this);
        //  else
        // g.drawImage (grass, x * 25, y * 25, this);
        //  g.drawImage (DL [0], xPos * 25, yPos * 25, this);
    }


    //***************************************************************************************************
    public void keyTyped (KeyEvent evt)
    {

        char ch = evt.getKeyChar (); // The character typed.

        if (ch == 'B' || ch == 'b')
        {
            squareColor = Color.blue;
            canvas.repaint ();
        }
        else if (ch == 'G' || ch == 'g')
        {
            squareColor = Color.green;
            canvas.repaint ();
        }
        else if (ch == 'R' || ch == 'r')
        {
            squareColor = Color.red;
            canvas.repaint ();
        }
        else if (ch == 'K' || ch == 'k')
        {
            squareColor = Color.black;
            canvas.repaint ();
        }

    }


    //***************************************************************************************************

    public void keyPressed (KeyEvent evt)
    {

        int key = evt.getKeyCode (); // keyboard code for the key that was pressed

        if (key == KeyEvent.VK_LEFT)
        {
            keyPressed [0] = true;
        }
        else if (key == KeyEvent.VK_RIGHT)
        {
            keyPressed [1] = true;
        }
        else if (key == KeyEvent.VK_UP)
        {
            keyPressed [2] = true;
        }
        else if (key == KeyEvent.VK_DOWN)
        {
            keyPressed [3] = true;
        }
        if (keyPressed [0] == true && xPos > 0 && state [xPos - 1] [yPos] == 'e')
        {
            xPos--;
            canvas.repaint ();
        }
        else if (keyPressed [1] == true && xPos < 39 && state [xPos + 1] [yPos] == 'e')
        {
            xPos++;
            canvas.repaint ();
        }

        if (keyPressed [2] == true && yPos > 0 && state [xPos] [yPos - 1] == 'e')
        {
            yPos--;
            canvas.repaint ();
        }
        else if (keyPressed [3] == true && yPos < 19 && state [xPos] [yPos + 1] == 'e')
        {
            yPos++;
            canvas.repaint ();
        }
    }


    //***************************************************************************************************
    public void keyReleased (KeyEvent evt)
    {
        int key = evt.getKeyCode ();

        if (key == KeyEvent.VK_LEFT)
        {
            keyPressed [0] = false;
        }
        else if (key == KeyEvent.VK_RIGHT)
        {
            keyPressed [1] = false;
        }
        else if (key == KeyEvent.VK_UP)
        {
            keyPressed [2] = false;
        }
        else if (key == KeyEvent.VK_DOWN)
        {
            keyPressed [3] = false;
        }
    }


    //***************************************************************************************************

    public void mousePressed (MouseEvent evt)
    {
        canvas.requestFocus ();
    }


    public void mouseEntered (MouseEvent evt)
    {
    } // Required by the


    public void mouseExited (MouseEvent evt)
    {
    } //    MouseListener


    public void mouseReleased (MouseEvent evt)
    {
    } //       interface.


    public void mouseClicked (MouseEvent evt)
    {
    }
} // end class MapTemplate



Now thus far it all works fairly well, the collision/input/movement all work as intended. My problem is that if I want to put other things onto the screen I need to wait for user input, which is a pain. What I want to know is whether or not I can put in some code to fire off an event (which triggers the redrawing of the screen) without any sort of user input (So no keylistenner,mouselistener etc. and timers are impractical for what I'm trying to do, due to the ideally dynamic nature of the motion that will be on-screen).

So, can an event be fired without using a timer or human input?
Sponsor
Sponsor
Sponsor
sponsor
Reality Check




PostPosted: Tue May 27, 2008 8:15 pm   Post subject: Re: Firing off events without user input

Hey zuber we don't have to do an RPG anymore. He wants us to do something simpler. If you want to do the RPG for personal fulfillment go ahead but you know Cuyugan, you'll probably get 100 no matter what you hand in. But anyways, what Mike and I did was fire off dummy events through method calls that did nothing. I'm sure there are better ways to do it though.
shadowman544




PostPosted: Tue May 27, 2008 8:28 pm   Post subject: Re: Firing off events without user input

i think that last post would be better suited for a pm rather than posting as an answer to a question
zuber




PostPosted: Wed May 28, 2008 7:51 am   Post subject: Re: Firing off events without user input

Forget a PM, he could have just told me yesterday in class, or today... in the same time it took him to tell me that he had responded to my topic =/
Assuming this works I guess I won't have to reach across the table to smack you Mr. Green
Reality Check




PostPosted: Wed May 28, 2008 7:55 am   Post subject: Re: Firing off events without user input

It's how I roll Cool
shadowman544




PostPosted: Wed May 28, 2008 5:53 pm   Post subject: RE:Firing off events without user input

lol ok
Nick




PostPosted: Wed May 28, 2008 6:00 pm   Post subject: Re: Firing off events without user input

zuber @ Tue May 27, 2008 8:10 am wrote:
way to avoid the whole flickering screen problem.


Just double buffer
shadowman544




PostPosted: Wed May 28, 2008 7:00 pm   Post subject: RE:Firing off events without user input

greesy I think you should explain that more
Sponsor
Sponsor
Sponsor
sponsor
Nick




PostPosted: Wed May 28, 2008 8:41 pm   Post subject: RE:Firing off events without user input

well the flickering occurs when indvidually drawing each object onto the screen, double buffering is basacally drawing everything off the screen, then moving it all onto the screen at once
shadowman544




PostPosted: Mon Jun 02, 2008 8:26 am   Post subject: Re: Firing off events without user input

thank you, a code example would have been nice bot thank you.
by your definition can we assume that this is like the view. update in turing.
Hendo




PostPosted: Sat Jun 07, 2008 5:21 pm   Post subject: Re: Firing off events without user input

I do flicker free animation using offscreen graphics. Something like in your method where you draw everything have
code:
public void drawScene (Graphics g)
    {
        if (offScrImg == null)
            offScrImg = createImage (size ().width, size ().height);

        Graphics og = offScrImg.getGraphics ();
        og.drawImage (mycoolpic, 0, 0, this);
        og.drawImage (othercoolpic, 0, 0, this);
        g.drawImage (offScrImg, 0, 0, this);

    }


Just make sure to make a new image variable like so
code:

private Image offScrImg;
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: