Jump to content

Learning Java - Input Boxes?

Arcapse

I'm trying to learn Java and I'm creating a game where text falls from the top of the screen to the bottom and you need to type the word before it reaches the bottom to win. So I'm curious, how would I go about setting up an input box that constantly checks to see if it could potentially become the word on screen ie. the word on screen is "Hello", "Hel" would be correct, "Helo" would be false as with "Hel" need "lo" to be added at the end.

 

I've found this code for an inputbox but I'm unsure where to put this in my code.

 

This is what the game looks like right now.

KHCGUnw.jpg

 

This is the code.

 

Game Class

import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage; 
import javax.swing.JFrame; 
public class Game extends Canvas implements Runnable{
    private static final long serialVersionUID = 1L;
    public static final int WIDTH = 640;
    public static final int HEIGHT = 480;
    public final String TITLE = "Learning Java";
    
    private boolean running = false;
    private Thread thread;
    
    private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
    
    private Word w;
    
    private void init(){
        w = new Word(200, 20, this);
    }
    
    private synchronized void start(){
        if(running)
            return;
        running = true;
        thread = new Thread(this);
        thread.start();
    }
    
    private synchronized void stop(){
        if(!running)
            return;
        
        running = false;
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.exit(1);
    }
    
    
    public void run(){
        init();
        long lastTime = System.nanoTime();
        final double amountOfTicks = 60.0;
        double ns = 1000000000 / amountOfTicks;
        double delta = 0;
        int updates = 0;
        int frames = 0;
        long timer = System.currentTimeMillis();
        while(running){
            long now = System.nanoTime();
            delta += (now - lastTime) / ns;
            lastTime = now;
            if (delta >= 1){
                tick();
                updates++;
                delta--;
            }
            render();
            frames++;
            
            if(System.currentTimeMillis() - timer > 1000){
                timer +=1000;
                System.out.println(updates + " Ticks, FPS " + frames);
                updates = 0;
                frames = 0;
            }
            
        }
        stop();
    }
    
    private void tick(){
        w.tick();
    }
    
    private void render(){
        BufferStrategy bs = this.getBufferStrategy();
        if(bs == null){
            createBufferStrategy(2);
            return;
        }
        Graphics g = bs.getDrawGraphics();
        ////
        g.fillRect(0,400,getWidth(),20);
        w.render(g);
        ////
        g.dispose();
        bs.show();
    }
    public static void main(String args[]){
        Game game = new Game();
        
        game.setPreferredSize(new Dimension(WIDTH, HEIGHT));
        game.setMaximumSize(new Dimension(WIDTH, HEIGHT));
        game.setMinimumSize(new Dimension(WIDTH, HEIGHT)); 
        
        JFrame frame = new JFrame(game.TITLE);
        
        frame.add(game);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);;
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        
        game.start();
    }
} 
  

World Class

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics; 
public class Word { 
    private double x;
    private double y;
    
    public Word(double x, double y, Game game){
        this.x = x;
        this.y = y;
    }
    
    public void tick(){
        y++;
    }
    
    public void render(Graphics g){
        //set up text style
        Font font = new Font("Serif", Font.BOLD, 36);
        g.setFont(font);
        g.setColor(Color.black);
        
        g.drawString("Hello", (int)x, (int)y);
    }
} 

Cheers & Best Regards,

Arcapse

Link to comment
Share on other sites

Link to post
Share on other sites

well, of the top of my head the solution would be to cut the word on screen to the length of the input and check if the input equals the cut word

The best way to measure the quality of a piece of code is "Oh F*** "s per line

Link to comment
Share on other sites

Link to post
Share on other sites

Override Swing's built-in InputVerifier

public class TextVerifier extends InputVerifier {
    
    private final String ACCEPTED_TEXT = "Hello"

    @Override
    public boolean verify(JComponent input) {
       String text = ((JTextField) input).getText();
	    if (text.length() == 0) {
            return true;
        } else {
            return (ACCEPTED_TEXT.indexOf(text) == 0);
        }
    }
}

//Later...

InputVerifier textVerifier = new TextVerifier()
myTextField.setInputVerifier(textVerifier);

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×