This site requires JavaScript, please enable it in your browser!
Greenfoot back
Mitchell.School
Mitchell.School wrote ...

2023/4/20

Help with J Option pane

Mitchell.School Mitchell.School

2023/4/20

#
    import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
    import javax.swing.*;
    import java.util.Arrays;
/**
 * Write a description of class Square here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Square extends Actor
{
    int index = 0;
    
    
    static int [] squareValue = new int[81];
    /**
     * Act - do whatever the Square wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        placeNum();
        newGame();
        //checkNum();
        //getWorld().showText(index + " ", getX(), getY());
    }
    public void placeNum(){
        if(Greenfoot.mouseClicked(this)){
            JFrame num;
            num=new JFrame();
            do{
                squareValue[index] = Integer.parseInt(JOptionPane.showInputDialog(num,"What number?(1-9)"));
            }while(squareValue[index] < 1 || squareValue[index] > 9);
            getWorld().showText(squareValue[index] + "", getX(), getY());
        }
    }
    public void checkNum(){
        
        

    }
    
    public void newGame(){
     Button button = getWorld().getObjects(Button.class).get(0);
     int difficulty = 0;
     if (Greenfoot.mouseClicked(button)){
        JFrame diff;
        diff=new JFrame();
        boolean diffSet = false;
        if(diffSet == false){
        diffSet = true;
        do {
                difficulty = Integer.parseInt(JOptionPane.showInputDialog(diff,"What Difficulty? (1-3)"));
                if(difficulty < 1 || difficulty > 3){
                    break;
                }
                break;
            }while(true);
        }

            if(difficulty == 1){
            int easy = Greenfoot.getRandomNumber(10);
            if(easy == 1){
                getWorld().showText("7", getX(), getY()); 
            }
            else if(easy == 2){
                getWorld().showText("2 works", 20, 20);
            }
            else if(easy == 3){
                getWorld().showText("3 works", 20, 20);
            }
            else if(easy == 4){
                getWorld().showText("4 works", 20, 20);
            }
            else if(easy == 5){
                getWorld().showText("5 works", 20, 20);
            }
            else if(easy == 6){
                getWorld().showText("6 works", 20, 20);
            }
            else if(easy == 7){
                getWorld().showText("7 works", 20, 20);
            }
            else if(easy == 8){
                getWorld().showText("8 works", 20, 20);
            }
            else if(easy == 9){
                getWorld().showText("9 works", 20, 20);
            }
            else if(easy == 10){
                getWorld().showText("10 works", 20, 20);
            }
        }
        else if(difficulty == 2){
            int med = Greenfoot.getRandomNumber(10);
            if(med == 1){
                    
            } 
            else if(med == 2){
                    
            }
            else if(med == 3){
                    
            }
            else if(med == 4){
                     
            } 
            else if(med == 5){
                    
            }
            else if(med == 6){
                    
            }
            else if(med == 7){
                    
            }
            else if(med == 8){
                    
            }
            else if(med == 9){
                    
            }
            else if(med == 10){
                    
            }
        }
        else if(difficulty == 3){
            int hard = Greenfoot.getRandomNumber(10);
            if(hard == 1){
                    
            }
            else if(hard == 2){
                    
            }
            else if(hard == 3){
                    
            }
            else if(hard == 4){
                    
            }
            else if(hard == 5){
                    
            }
            else if(hard == 6){
                    
            }
            else if(hard == 7){
                    
            }
            else if(hard == 8){
                    
            }
            else if(hard == 9){
                    
            }
            else if(hard == 10){
                    
            }
        }
        
        
     }
 }
}

My J option pane continues to repeat even when a one to three is entered but works if I trigger with isKeyDown
Mitchell.School Mitchell.School

2023/4/20

#
if(difficulty < 1 || difficulty > 3){ break; } this doesn't break even if I enter something over 3
danpost danpost

2023/4/20

#
Mitchell.School wrote...
if(difficulty < 1 || difficulty > 3){ break; } this doesn't break even if I enter something over 3
Line 57 kind of negates the whole do loop altogether. However, there are still other issues you need to be aware of. You are parsing input that is not being checked. Issues could be (1) the input returned was null (happens when the user closes the frame by clicking its Close button, the red 'x' button at top right corner of the frame or the user presses the Escape key); or (2) the input is not able to be parsed (happens with invalid input -- any alpha character or any punctuation that is not a plus ('+') or minus ('-') sign at the first character position). Errors from these are the NullPointerException and NumberFormatException exceptions. For lines 52 to 58, try something like this:
while (true) {
    String s = JOptionPane.showInputDialog(diff, "What Difficulty? (1-3)");
    if (s != null && s.length() == 1 && "123".indexOf(s) > -1) {
        difficulty = Integer.parseInt(s);
        break;
    }
}
The squareValue input can be coded similarly. Be aware that swing components will not run on any site (including this one) using HTML/javascript. You may want to either use the Greenfoot.ask method to get the input or not use an input box at all and just except a keystroke after prompting for it. It can still be displayed after the input is made.
Mitchell.School Mitchell.School

2023/4/21

#
still repeats itself even if i enter 1-3 which is weird because the squareValue input works and this one works if i have it be triggered by pressing a key if (Greenfoot.isKeyDown("n")){ JFrame diff; diff=new JFrame(); boolean diffSet = false; if(diffSet == false){ diffSet = true; do { difficulty = Integer.parseInt(JOptionPane.showInputDialog(diff,"What Difficulty? (1-3)")); if(difficulty < 1 || difficulty > 3){ break; } break; }while(true); } because that works but the other one doesn't
danpost danpost

2023/4/21

#
Mitchell.School wrote...
still repeats itself even if i enter 1-3 which is weird because the squareValue input works and this one works if i have it be triggered by pressing a key << Code Omitted >> because that works but the other one doesn't
The code provided by myself was tested before posting. I am quite sure it, by itself, is not the problem. Please show your updated code.
You need to login to post a reply.