0

I want to create a 2D randomly painted array via user input from applet. User will choose the array size and colour amount. The code is working when i press 5 and then type the inputs from console. But i want it from the applet screen by keylistener or something like that.

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();
        }

    }

}
Zombo
  • 1
  • 62
  • 391
  • 407
Dekoder
  • 1
  • 1
  • 2
    I'd start by avoiding applets, they have a bunch of their own issues you really don't want to be dealing with right now. Next I'd take a look at [How to use Swing Timers](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) and [How to Use Tables](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html) (and possibly [How to Use Text Fields](http://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html)) for more details – MadProgrammer Aug 24 '15 at 00:30
  • I'd also take a look at [Painting in AWT and Swing](http://www.oracle.com/technetwork/java/painting-140037.html) and [Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/) for a better understanding of how painting works in Swing/AWT – MadProgrammer Aug 24 '15 at 00:31
  • Thank you for your help. However, I just wanted it to be like a game. Like a character jumps when you press jump button. I want user to press numbers he/she wants and press enter then screen changes with his/her parameter and then again same procedure. This code is working however, in this code user puts his/her input from Console but I want it from the game screen. – Dekoder Aug 24 '15 at 11:09
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Aug 25 '15 at 12:17

0 Answers0