Buttons

I thought you were talking about on the main screen. Never mind.

is there any way to make an awt button push when enter is pressed? basically is there an equivalent to setDefaultButton(…); in awt not swing. also is there any way to make the cursor start in the text field? as is now the user has to click on the text field to start typing.

also…i am having the user enter a user name. whats the best way to check and make sure they did this. i am using string.length(); right now but if they just enter spaces length() returns a value.
how do i get around this?

You could set up an event handler to capture key presses, validate that each key pressed is within your allowed set of characters, and build the final input string as they type.

ok so im trying to figure out animation…i can get the image to move but i only want it to move when a key is pressed.

can anyone explain why this code doesnt work?

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


public class testGame extends Applet implements Runnable, KeyListener
{
    int x = 0;
    int y = 500;
    Thread th = new Thread(this);
  
public void init()
{
    setBackground(Color.white);

}


public void keyTyped(KeyEvent e) { e.consume();}
public void keyReleased(KeyEvent e) {e.consume();}
public void keyPressed(KeyEvent e)
{
    if (e.getKeyCode() == KeyEvent.VK_RIGHT)
        th.start();
        
        e.consume();
}


public void paint (Graphics g)
{
    Characters Create = new Characters();
    g.setColor(Color.red);
    g.fillRect(450,300,100,265);
    g.drawImage(Create.createBSprite(),x,y,this);
    g.setColor(Color.green);
    g.fillRect(0,565,1000,35);   
    
}

public void run()
{
    while (true)
    {
        x++;
        repaint();
        
        try
        {
            Thread.sleep(20);
        }
        catch(InterruptedException ex)
        {
        }
    }
}



}

class Characters
{
        
         BufferedImage createBSprite()
        {
            BufferedImage sprite = new BufferedImage(100,100,BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2D = sprite.createGraphics();
            g2D.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
            g2D.fillRect(0,0,100,100);
            g2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
            g2D.setColor(Color.black);
            //head
            g2D.fillOval(42,20,17,16);
            //neck
            g2D.fillRect(48,35,7,4);
            //body
            g2D.fillArc(42,36,17,16,0,180);
            g2D.fillRect(43,44,16,10);
            //left leg and foot
            g2D.fillRect(44,54,3,8);
            g2D.fillRect(44,62,6,3);
            //right leg and foot
            g2D.fillRect(55,54,3,8);
            g2D.fillRect(55,62,6,3);
            //right arm
            g2D.fillRect(58,44,10,3);
            g2D.fillOval(64,41,12,8);
            //left arm
            g2D.fillRect(36,44,8,3);
            g2D.fillOval(32,43,5,5);
            return sprite;
        
        }

        

        
         
        
        }
}

Because you start the thread when the key is pressed and the thread never stops.

I would recommend something like

public void keyReleased(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_RIGHT)
        mvRight=false;

    e.consume();
}
public void keyPressed(KeyEvent e)
{
    if (e.getKeyCode() == KeyEvent.VK_RIGHT)
        mvRight = true;
       
        e.consume();
}

Then, in your paint method, you can check the status of mvRight in order to increment x.

ok given that shouldn’t this work? and the problem isnt that the loop continues forever. it wont work at all with the key thing. the animation will work if i just put x++ in the run method but
i dont want it to run forever just run when the keys are pressed.

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


public class testGame extends Applet implements Runnable, KeyListener
{
    int x = 0;
    int y = 500;
    Thread th = new Thread(this);
    boolean right = false;
  
public void init()
{
    setBackground(Color.white);

}

public void start()
{
    th.start();
}


public void keyTyped(KeyEvent e) { e.consume();}
public void keyReleased(KeyEvent e) 
{
    if (e.getKeyCode() == KeyEvent.VK_RIGHT)
        right = false;
        
    e.consume();
}
public void keyPressed(KeyEvent e)
{
    if (e.getKeyCode() == KeyEvent.VK_RIGHT)
        right = true;
        
    e.consume();
}


public void paint (Graphics g)
{
    if(right)
        x++;
    Characters Create = new Characters();
    g.setColor(Color.red);
    g.fillRect(450,300,100,265);
    g.drawImage(Create.createBSprite(),x,y,this);
    g.setColor(Color.green);
    g.fillRect(0,565,1000,35);   
    
}

public void run()
{
    while (true)
    {
        repaint();
        
        try
        {
            Thread.sleep(20);
        }
        catch(InterruptedException ex)
        {
        }
    }
}



}

class Characters
{
     BufferedImage createBSprite()
        {
            BufferedImage sprite = new BufferedImage(100,100,BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2D = sprite.createGraphics();
            g2D.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
            g2D.fillRect(0,0,100,100);
            g2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
            //head
            g2D.fillOval(42,20,17,16);
            //neck
            g2D.fillRect(48,35,7,4);
            //body
            g2D.fillArc(42,36,17,16,0,180);
            g2D.fillRect(43,44,16,10);
            //left leg and foot
            g2D.fillRect(44,54,3,8);
            g2D.fillRect(44,62,6,3);
            //right leg and foot
            g2D.fillRect(55,54,3,8);
            g2D.fillRect(55,62,6,3);
            //right arm
            g2D.fillRect(58,44,10,3);
            g2D.fillOval(64,41,12,8);
            //left arm
            g2D.fillRect(36,44,8,3);
            g2D.fillOval(32,43,5,5);
            return sprite;
        
        }
}
        

You forgot the addKeyListener(this). You need to do things differently though.

  1. If you are doing drawing in paint, do only drawing. Do not do any game logic or loading of resources.
  2. All the resources you are going to use for a level should be loaded ahead of time.
  3. Having said #1 above, you should use active rendering anyway.
  4. Use the thread you created and use it as a game loop.
  5. Use double buffering to stop flickering.

Search on 3, 4 and 5 to find out how to use them Make sure you put Java in your search to get Java specific examples. I modified your code to get you started.


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

public class Test extends Applet implements Runnable, KeyListener {
    int x = 0;
    int y = 500;
    Thread th = new Thread(this);
    BufferedImage sprite;
    boolean keys[];
    boolean paused;

    public void init() {
        setBackground(Color.white);
        addKeyListener(this);
        keys = new boolean[65536];
        th.start();
        Characters create = new Characters();
        sprite = create.createBSprite();
        paused = false;
    }

    public void start() {
        paused = false;
    }
    
    public void stop() {
        paused = true;
    }

    public void keyTyped(KeyEvent e) {
    }

    public void keyReleased(KeyEvent e) {
        keys[e.getKeyCode()] = false;
    }

    public void keyPressed(KeyEvent e) {
        keys[e.getKeyCode()] = true;
    }

    //override paint and update to stop any system rendering
    public void update(Graphics g) {
    }
    public void paint(Graphics g) {
    }

    public void run() {
        while (true) {
            //game loop
            if(!paused) {
                Graphics2D g = (Graphics2D)getGraphics();
                //game logic
                if(keys[KeyEvent.VK_RIGHT]) {
                    x++;
                }
                if(keys[KeyEvent.VK_LEFT]) {
                    x--;
                }
                
                //rendering
                g.setColor(Color.WHITE);
                g.fillRect(0, 0, getWidth(), getHeight());
                g.drawImage(sprite, x, y, this);
                g.dispose();
                
                try {
                    Thread.sleep(20);
                }
                catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
            else {
                try {
                    Thread.yield();
                    Thread.sleep(100);
                }
                catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }

}

class Characters {
    BufferedImage createBSprite() {
        BufferedImage sprite = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2D = sprite.createGraphics();
        g2D.setColor(Color.RED);
        g2D.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
        g2D.fillRect(0, 0, 100, 100);
        g2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
        // head
        g2D.fillOval(42, 20, 17, 16);
        // neck
        g2D.fillRect(48, 35, 7, 4);
        // body
        g2D.fillArc(42, 36, 17, 16, 0, 180);
        g2D.fillRect(43, 44, 16, 10);
        // left leg and foot
        g2D.fillRect(44, 54, 3, 8);
        g2D.fillRect(44, 62, 6, 3);
        // right leg and foot
        g2D.fillRect(55, 54, 3, 8);
        g2D.fillRect(55, 62, 6, 3);
        // right arm
        g2D.fillRect(58, 44, 10, 3);
        g2D.fillOval(64, 41, 12, 8);
        // left arm
        g2D.fillRect(36, 44, 8, 3);
        g2D.fillOval(32, 43, 5, 5);
        return sprite;

    }
}

ok thanks that makes more sense. i know how to double buffer i just didnt do it yet. also why is the key boolean array so large?

All the values in KeyEvent go from 0 to 65535. So this is the lazy way of doing it.

ok here is what i have double buffering and all. when the left or right keys are pushed the character moves…done. when the up or down keys are pushed the character should raise or lower his arm…as in aiming a gun. is the easiest way to do this with an array of images as i have now? if so what is a good program to use to make my sprites?

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

public class Test extends Applet implements Runnable, KeyListener {
    int x = 0;
    int y = 500;
    int z = 0;
    Thread th = new Thread(this);
    BufferedImage offscreen = new BufferedImage(1000,600,BufferedImage.TYPE_INT_RGB);
    Graphics2D bufferGraphics = offscreen.createGraphics();
    boolean keys[];
    boolean paused;
    Image sprite[] = new Image[10];

    
    public void init() {
        setBackground(Color.white);
        addKeyListener(this);
        keys = new boolean[65536];
        th.start();
        Characters create = new Characters();
        sprite[0] = getImage(getCodeBase(),"sprite0.gif");
        sprite[1] = getImage(getCodeBase(),"sprite1.gif");
        sprite[2] = getImage(getCodeBase(),"sprite2.gif");
        sprite[3] = getImage(getCodeBase(),"sprite3.gif");
        paused = false;
    }

    public void start() {
        paused = false;
    }
    
    public void stop() {
        paused = true;
    }

    public void keyTyped(KeyEvent e) {
    }

    public void keyReleased(KeyEvent e) {
        keys[e.getKeyCode()] = false;
    }

    public void keyPressed(KeyEvent e) {
        keys[e.getKeyCode()] = true;
    }

    //override paint and update to stop any system rendering
  
    public void paint(Graphics g) {}
    public void update(Graphics g) {}

    public void run() {
        while (true) {
            //game loop
            if(!paused) {
                Graphics2D g = (Graphics2D)getGraphics();
                //game logic
                if(keys[KeyEvent.VK_RIGHT] && x < 374) {
                    x++;
                }
                if(keys[KeyEvent.VK_LEFT] && x > -3) {
                    x--;
                }
                
                if(keys[KeyEvent.VK_UP] && z < 9)  {
                    z++;
                }
                
                if(keys[KeyEvent.VK_DOWN] && z > 0) {
                    z--;
                }
                
                //rendering
                bufferGraphics.setColor(Color.WHITE);
                bufferGraphics.fillRect(0, 0, getWidth(), getHeight());
                bufferGraphics.setColor(Color.red);
                bufferGraphics.fillRect(0,300,30,300);
                bufferGraphics.fillRect(450,300,100,300);
                bufferGraphics.setColor(Color.green);
                bufferGraphics.fillRect(0,565,1000,35);
                bufferGraphics.drawImage(sprite[z], x, y, this);
                g.drawImage(offscreen,0,0,this);
                g.dispose();
                
                try {
                    Thread.sleep(20);
                }
                catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
            else {
                try {
                    Thread.yield();
                    Thread.sleep(100);
                }
                catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }

}