R_86
R_86

Reputation: 35

My code seems to be looping and I don't know why

I am writing a program that draws an object in different states. The first is a gray rectangle, the second is an image, and the third is a lighter rectangle with text in it. I wrote the code as 3 classes. I am having an issue with my component class. It keeps looping for some reason. When i enter a 1, it works fine. When I enter a 2, it ask for my input 4 times before it shows the image. When I enter a 3, It ask twice before it shows the lighter rectangle but it does not have any text.

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.util.Scanner;

import javax.swing.JComponent;

public class ImageComponent extends JComponent {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public int status;

    public int getInput(){  
    Scanner userinput = new Scanner(System.in);
    System.out.println("Select the state <1-rectangle, 2-image, 3-rectangle with text>");
    int state=userinput.nextInt();
    //userinput.close();
    return state;
    }

    public void paintComponent(Graphics g){
        Graphics2D g2 =(Graphics2D) g;

        if (getInput() ==1){
            ImageMaker rectangle = new ImageMaker(0,0,500,400);
            g2.setColor(Color.GRAY);
            rectangle.draw(g2);

        }
        else if (getInput() ==2){
            Image img1 = Toolkit.getDefaultToolkit().getImage("balloon.gif");
            ImageMaker image = new ImageMaker(img1, 0, 0, this);
            image.draw(g2);
        }
        else{
            ImageMaker rectangle = new ImageMaker(0,0,500,400);
            g2.setColor(Color.LIGHT_GRAY);
            rectangle.draw(g2);
            g2.drawString("Your Text Here", 500/2, 400/2);
        }   
    }

}

Upvotes: 1

Views: 75

Answers (3)

Thomas Kl&#228;ger
Thomas Kl&#228;ger

Reputation: 21510

First: As @Hovercraft-Full-Of-Eels already noted: paintComponent is for painting your component, not for user interaction.

This method will be called from swing anytime it thinks it's necessary to repaint your UI - for example after your frame has been obscured by some other window and is shown again.


The second point is that

if (getInput() ==1){

asks for user input. If the user enters 2 the condition is not fulfilled, and with

else if (getInput() ==2){

you are asking a second time for user input.

If you have a decision chain that depends on interactive user input it should look something like

int input = getInput();
if (input == 1) {
    //...
} else if (input == 2) {
    //...
} else {
    //...
}

or

switch (getInput()) {
case 1:
    //...
    break;
case 2:
    //...
    break;
default:
    //...
    break;
}

Upvotes: 0

user3437460
user3437460

Reputation: 17454

When i enter a 1, it works fine. When I enter a 2, it ask for my input 4 times before it shows the image. When I enter a 3, It ask twice before it shows the lighter rectangle but it does not have any text.

You are placing a method getInput() which prompts using Scanner into your paintComponent. Try not to place any other stuff in your paintComponent method other then tasks related to painting. The paint manager will decide when to call the paintComponent itself and you have no absolute control over it. (For example, it may be invoked when you resize/mouseover your frame, causing your prompts to be invoked more than what you intended).

I would advise you to separate your logic for prompting the user and painting the component. Furthermore, you should avoid mixing using Scanner with custom painting.

You may consider using Dialogs: How to make Dialogs

Upvotes: 2

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

I can't tell for sure the exact cause of your problems, since you've not posted a valid minimal example program (please check the link to see what I mean), but I have several suspicions that I'll relay to you:

  • You're mixing Scanner initialized to System.in, a tool for getting input from the console, with a Swing GUI, two things that don't mix well together and shouldn't be mixed well together. Scanner input methods block program flow, something that can completely freeze your Swing GUI.
  • You appear to be doing more things in paintComponent than you should be doing. This method is for painting and painting only, and should not be used for file input/output (such as reading in images), for program logic, or for creating GUI components.

Upvotes: 3

Related Questions