在Eclipse中通过屏幕获取用户输入而不是通过控制台

时间:2015-08-24 00:26:41

标签: java applet user-input keylistener

我想通过applet的用户输入创建一个2D随机绘制的数组。用户将选择数组大小和颜色数量。当我按下5然后从控制台输入输入时,代码正在工作。但我想通过keylistener或类似的东西从小程序屏幕中获取它。

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.URL;
import java.util.Random;
import java.util.Scanner;

public class Renderer2 extends Applet implements KeyListener, Runnable {
    int[][] tilemap;
    int rows, columns;

    public void init() {

        setSize(800, 480);
        setBackground(new Color(Integer.parseInt("FF9933", 16)));
        addKeyListener(this);
        try {
            URL base = getDocumentBase();
        } catch (Exception e) {
            // TODO: handle exception
        }

    }

    public void start() {

        Thread thread = new Thread(this);
        thread.start();
    }

    private void createTilemap() {

        System.out.println("enter");
        Scanner user_input = new Scanner(System.in);

        final int z = user_input.nextInt();
        System.out.println("enter2");
        Scanner user_inputt = new Scanner(System.in);
        final int a = user_inputt.nextInt();
        System.out.println("enter3");
        Scanner user_inputtt = new Scanner(System.in);
        final int b = user_inputtt.nextInt();

        tilemap = new int[a][b];

        rows = tilemap.length;
        columns = tilemap[a - 1].length;
        setSize(25 * a, 25 * b);
        setBackground(new Color(Integer.parseInt("FF9933", 16)));

        Random r = new Random();

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                tilemap[i][j] = r.nextInt(z + 1);
            }
        }
    }

    public void paint(Graphics g) {
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {

                int mod_i = 25 * i;
                int mod_j = 25 * j;

                switch (tilemap[i][j]) {
                case 0:
                    g.setColor(new Color(Integer.parseInt("663300", 16)));
                    g.fillRect(mod_i, mod_j, 15, 15);
                    break;
                case 1:
                    g.setColor(new Color(Integer.parseInt("00CCCC", 16)));
                    g.fillRect(mod_i, mod_j, 25, 25);
                    break;
                case 2:
                    g.setColor(new Color(Integer.parseInt("FF9999", 16)));
                    g.fillRect(mod_i, mod_j, 30, 30);
                    break;
                case 3:
                    g.setColor(new Color(Integer.parseInt("660033", 16)));
                    g.fillRect(mod_i, mod_j, 20, 20);
                    break;
                case 4:
                    g.setColor(new Color(Integer.parseInt("FFE5CC", 16)));
                    g.fillRect(mod_i, mod_j, 35, 35);
                    break;

                }

            }

        }

    }

    @Override
    public void keyPressed(KeyEvent e) {
        switch (e.getKeyCode()) {
        case KeyEvent.VK_5:
            System.out.println("5");
            createTilemap();
            break;

        case KeyEvent.VK_6:
            System.out.println("6");
            break;
        }

    }

    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void run() {
        try {
            Thread.sleep(17);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

}

0 个答案:

没有答案