[Cialug] GUI Programming

Todd Walton tdwalton at gmail.com
Thu Mar 7 21:25:54 CST 2013


Thanks, Don, for trying to clarify this.  But let me make this real.
I've pasted in below some Java that I've composed to show a little
spaceship on the screen that floats down with time-incremented
"gravity", or moves up or sideways based on key presses.  The
keyPressed() and timeStep() methods are event driven, the first one by
key presses, obviously, and the second one by ticks of the clock.  I
understand (now) that that's what they're doing.

But look at those methods with the eye of a novice and tell me one way
in which they differ from any other method that isn't event driven,
that are explicitly called by some other bit of code?  There isn't a
difference.  The syntax is exactly the same.

*That's* what is confusing.  How am I supposed to know what's going on
with this if there's nothing to indicate what's going on??  I have
more learning to do, and I have a feeling that an essential component
of it will involve simple rote recognition/memorization, versus logic.
 That's fine.  We are dealing with machines here, after all.  But I
have yet to have an instructor or a book that explained it in a way
that conveyed what I need to know.  So if the language isn't going to
indicate it, and the books don't indicate it, it just means the going
is difficult.

For my mind anyway.

============================

package dayX;

import day8.CheesyGraphicsLibrary.*;
import java.lang.Math;

public class Lander implements GameItem, KeyAction, TimerAction
{
	private double INITGRAVITY = 0.9;
	private double gravity = INITGRAVITY;
	private int x;
	private int y;

	public Lander(int newX) {
		x = newX;
		y = 1;
	}

	public void draw(GameScreen gs) {
		gs.updateScreen(x,y,GameScreen.GREEN);
		gs.updateScreen(x+1,y,GameScreen.GREEN);
		gs.updateScreen(x-1,y,GameScreen.GREEN);
		gs.updateScreen(x,y-1,GameScreen.GREEN);
	}
	
	public void keyPressed(int key) {
		switch (key)
		{
		case KeyAction.KEY_LEFT:
			x--; break;
		case KeyAction.KEY_RIGHT:
			x++; break;
		case KeyAction.KEY_UP:
			if (y < 2) {
				y++;
			};
			gravity = INITGRAVITY;
			y--; break;
		}
	}
	
	public void timeStep() {
		gravity *= 1.09;
		y += Math.floor(gravity);
	}

}

============================

--
Todd


More information about the Cialug mailing list