Let’s finally post some coding. Java coding.

4 Jul

Odd, isn’t it? You would think that since I am seriously focused on ActionScript 2 or ActionScript 3 that I would post code of AS2/AS3. No, this is Java. Slightly different from AS3, but still similar.

Added how to make a slightly better-than-average first applet for java. Sure, it isn’t swing… but it is a first step in making GUIs. Found it in my old abandoned Java tests folder. Enjoy 😀

//To run this, just right-click it, view it on "appletviewer" (applet viewer)

import java.applet.*;//to be able to declare that it extends Applet.

import java.awt.event.*;//for events! in this case, "KeyEvents" or if you know AS3, it's basically "KeyboardEvents"

import java.awt.*;//awt is for drawing objects and such, but we will really be looking at
//this more and more

//import what you need.
public class MovingSquare extends Applet implements KeyListener{
    //set the initial values
    String message = "";
    int speed = 5;
    int x = 225;
    int y = 225;
    public void init(){
        //init = the initial settings of your project. Just going to set the Background and addKeyListeners
        setBackground(Color.black);
        this.addKeyListener(this);
        //for those of you who use AS3 (or other languages as well), this.addKeyListener(this) may seem redundant at first, 
        //but it is saying to add a keyListener to the applet... init()
    }
    public void keyPressed(KeyEvent e){
        //switch(case) is a statement that goes to whatever case is inputted. in this case... e.getKeyCode()
        switch(e.getKeyCode()){
            case KeyEvent.VK_LEFT: x -= speed;
            break;
            case KeyEvent.VK_RIGHT: x += speed;
            break;
            case KeyEvent.VK_UP: y -= speed;
            break;
            case KeyEvent.VK_DOWN: y += speed;
        }
        //move at the rate of speed in whichever direction you choose. Also, repaint() is needed for updating graphics.
        this.repaint();
    }
    public void keyReleased(KeyEvent e){}
    //if you mention keyPressed, you HAVE TO mention keyReleased and keyTyped...
    public void keyTyped(KeyEvent e){
        char c = e.getKeyChar();
        //getKeyChar = "get key that was inputted on the keyboard"
        if (c == '\b') {
            //if the character inputted was a backspace (known as \b)
            if (message.length() > 0) {
                message = message.substring(0, message.length()-1);
                //if it was a backspace, subtract the last message's character.
            }
        } else {
            message += c;
            //otherwise, just add the character to the end of the message.
        }
        this.repaint();
        //repaint! See above example.
    }
    public void paint(Graphics g){
        g.setColor(Color.green);
        g.drawString(message, 225, 35);
        g.drawString("Type Here:", 160, 35);
        g.setColor(Color.lightGray);
        g.fillRect(x, y, 50, 50);
        //draw everything here.
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: