Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8771627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:50:15+00:00 2026-06-13T17:50:15+00:00

This is my code for a simple arrow sequencing application. It generates a random

  • 0

This is my code for a simple arrow sequencing application.

It generates a random sequence of size 4 which comprise of UP, DOWN, LEFT and/or RIGHT arrow keys and displays them one at a time. If they user repeats the sequence correctly, it displays another sequence of 5 keys. The size of the sequence keeps incrementing as long as the user enters the correct sequence but decrements if an invalid sequence has been entered.

The problem I’m encountering is, the first execution is flawless, it even displays the sequence the second time, but it doesn’t accept my keyboard inputs during the second iteration.

For a better understanding of the problem I have broken it down into the main action performing blocks and the full code as well at the end.

Sequence Generation

for(int flag=1;flag<size;flag++)
    {
    random = randomGenerator.nextInt(4);
    generated[flag]=random;
    setVisible(true);
    switch(random)
        {
            case 0:
            left();
            break;
            case 1:
            up();
            break;
            case 2:
            right();
            break;
            case 3:
            down();
            break;
        }
        delaybig();     
    }

User Input Sequence

public void keyPressed(KeyEvent e)
{
    if(cflag<=size) 
    {
        if(e.getKeyCode()==37)
            {
                entered[cflag]=0;
                cflag++;
                Keys.setText("LEFT");
                left();
            }
        else if(e.getKeyCode()==38)
            {
                entered[cflag]=1;
                cflag++;
                Keys.setText("UP");
                up();
            }
        else if(e.getKeyCode()==39)
            {
                entered[cflag]=2;
                cflag++;
                Keys.setText("RIGHT");
                right();
            }
        else if(e.getKeyCode()==40)
            {
                entered[cflag]=3;
                cflag++;
                Keys.setText("DOWN");
                down();
            }
    else
        {
            Keys.setText("INVALID");
        }
    }

Sequence Comparision using Arrays.equals

if(cflag==size)
    {
        boolean check = Arrays.equals(generated, entered);
        if(check)
            {
                delaysmall();
                Keys.setText("PERFECT");
                size++;
                cflag=1;
                delaysmall();
                Keys.setText("GO AGAIN");
                delaybig();
                restart();  
            }
        else
            {
                delaysmall();
                Keys.setText("FAILED");
                if(size>5)
                    {
                        delaybig();
                        restart();
                        size--;
                        cflag=1;
                    }
            }
    }

Looping Thread

public void restart() 
{ 
    Thread goagain =  new Thread() 
    {
        @Override
        public void run()
             {
                launchframe();
             }
        };
        goagain.start();
}

I’ve spent quite some time on this to no avail so here is the full code just incase you suspect the error likes elsewhere.

import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
import java.util.Arrays;

class ArrowSorrow extends Frame implements KeyListener{

int flag;
Image img;
int random;
int cflag=1,size=5;
boolean ShowImage=true;
int entered[]=new int[50];
int generated[]=new int[50];
TextField Keys=new TextField(8);
Random randomGenerator = new Random();
MediaTracker mt = new MediaTracker(this);

public ArrowSorrow(String title)
    {
        super(title);
    }

public void restart() 
    { 
        // Create a new thread
    Thread goagain =  new Thread() 
    {
        // Override run() to provide the running behavior of this thread.
        @Override
        public void run()
             {
                launchframe();
             }
        };
        goagain.start();
}
public void launchframe()
{
    for(int flag=1;flag<1;flag++)
        generated[flag]=0;
    for(int flag=1;flag<1;flag++)
        entered[flag]=0;
            splash();
    add(Keys);
            delaybig();
    setSize(400,400);
    Keys.setEditable(false);
    Keys.setText("MEMORIZE");
    Keys.addKeyListener(this);      
    setBackground(Color.black);
    setLayout(new FlowLayout());
for(int flag=1;flag<size;flag++)
        {
        random = randomGenerator.nextInt(4);
        generated[flag]=random;
        setVisible(true);
        switch(random)
            {
                case 0:
                left();
                break;
                case 1:
                up();
                break;
                case 2:
                right();
                break;
                case 3:
                down();
                break;
            }
            delaybig();     
        }
        String sequence=new String("");
        for(flag=1;flag<size;flag++)
            {
                sequence=sequence+(Integer.toString(generated[flag]));
            }
            Keys.setText(sequence);
            delaysmall();
            Keys.setText("REPEAT");
            delaysmall();
            setVisible(true);

            addWindowListener(new WindowAdapter()
                {
                    public void windowClosing(WindowEvent we)
                        {
                            dispose();
                        }
                });
    }
public void splash()
    {
    img = ToolKit.getDefaultToolkit().getImage("image address for splashscreen.jpg");
    mt.addImage(img,0);
    repaint();
    setVisible(true);
}

public void left()
{
    img = Toolkit.getDefaultToolkit().getImage("image address for left.jpg");
    mt.addImage(img,0);
    repaint();
    setVisible(true);
}

public void right()
{
    img = Toolkit.getDefaultToolkit().getImage("image address for right.jpg");
    mt.addImage(img,0);
    repaint();
    setVisible(true);
}

public void up()
{
    img = Toolkit.getDefaultToolkit().getImage("image address for up.jpg");
    mt.addImage(img,0);
    repaint();
    setVisible(true);
}

public void down()
{
    img = Toolkit.getDefaultToolkit().getImage("image address down.jpg");
    mt.addImage(img,0);
    repaint();
    setVisible(true);
}
//minor delay
public void delaysmall()
{
    try 
        {
            Thread.sleep(600);
        } 
    catch(InterruptedException ex) 
        {
            Thread.currentThread().interrupt();
        }   
}
//major delay
public void delaybig()
{
    try 
        {
            Thread.sleep(1200);
        } 
    catch(InterruptedException ex) 
        {
            Thread.currentThread().interrupt();
        }   
}

public void paint(Graphics g)
{
super.paint(g); 
if(img != null)
    g.drawImage(img,70,70, this);
else
    g.clearRect(0, 0, getSize().width, getSize().height);
}

public void keyPressed(KeyEvent e)
{
    if(cflag<size)  
    {
        if(e.getKeyCode()==37)
            {
                entered[cflag]=0;
                cflag++;
                Keys.setText("LEFT");
                left();
            }
        else if(e.getKeyCode()==38)
            {
                entered[cflag]=1;
                cflag++;
                Keys.setText("UP");
                up();
            }
        else if(e.getKeyCode()==39)
            {
                entered[cflag]=2;
                cflag++;
                Keys.setText("RIGHT");
                right();
            }
        else if(e.getKeyCode()==40)
            {
                entered[cflag]=3;
                cflag++;
                Keys.setText("DOWN");
                down();
            }
    else
        {
            Keys.setText("INVALID");
        }
    }
//comparing generated sequence and user input sequence      
if(cflag==size)
    {
        boolean check = Arrays.equals(generated, entered);
        if(check)
            {
                delaysmall();
                Keys.setText("PERFECT");
                size++;
                cflag=1;
                delaysmall();
                Keys.setText("GO AGAIN");
                delaybig();
                restart();  
            }
        else
            {
                delaysmall();
                Keys.setText("FAILED");
                if(size>5)
                    {
                        delaybig();
                        restart();
                        size--;
                        cflag=1;
                    }
            }
    }
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
}

class ArrowSorrowLaunch{
public static void main(String args[])
    {
        ArrowSorrow instance=new ArrowSorrow("Arrow Sorrow");
        instance.launchframe();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T17:50:18+00:00Added an answer on June 13, 2026 at 5:50 pm

    You call launchFrame to start each iteration of the game. However, you are doing a lot of work in launchFrame that should be done only once. You should move that initialization code out of launchFrame and do it only once. In particular you should not repeatedly call Keys.addKeyListener(this); or add multiple window listeners.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this simple code in a WPF application: ThreadStart start = delegate() {
take this simple code: class A{ public: virtual void foo() = 0; void x(){
I have this simple code (for making things shorted, the important bits are probably
I have this simple code, using Joda-time. Works fine, but I have a problem.
I have this simple code where I am sending http request and reading all
I have this simple code that speaks for itself. <script language='javascript"> function check() {}
I have this simple code in my user_controller.rb file #listing all users def index
i have this simple code to load data from other php page using get
I have this simple code public String toString() { **Iterator it = list.iterator();** String
I am testing this simple code below and it works... $(document).ready( function() { $('.show-modal').click(

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.