wenda
wenda

Reputation: 1

Make random character letters and display them one by one in the java swing

I tried to make random letters but only with three letters, and I want to display them one by one in the window I made when the "random" button was pressed, but when I pressed the random button, the loop didn't stop. and in the same way I tried with the CLI it gave me the results I expected. I've tried it many times but it doesn't work. Please help?

Below is the complete source code of my program:

package testGrafik;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestRandom extends JFrame {

public TestRandom() {
    setTitle("Test Game");
    setSize(500, 500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);

    Board board = new Board();
    getContentPane().add(board);
}

public static void main(String[] args) {
    new TestRandom();
}

public class Board extends JPanel implements MouseListener, 
MouseMotionListener {
    int x;
    int y;
    public boolean isMouseMoving = false;
    public boolean isClicked = false;

    public char[] character = {'S', 'O', 'I'};
    public Random rand = new Random();

    public Board() {
        x = 0;
        y = 0;

        addMouseListener(this);
        addMouseMotionListener(this);
    }

    @Override
    public void paintComponent(Graphics g) {
        g.setColor(Color.DARK_GRAY);
        g.fillRect(x, y, 500, 500);

        // draw box
        g.setColor(Color.LIGHT_GRAY);
        g.fillRect(x + 10, y + 10, 70, 40);

        if (isMouseMoving == true) {
            g.setColor(Color.cyan);
            g.fillRect(x + 10, y + 10, 70, 40);
        }

        //draw string
        g.setColor(Color.BLACK);
        g.setFont(new Font("Tahoma", Font.BOLD, 15));
        g.drawString("Click", x + 25, y + 35);

        if (isClicked == true) {
            char c = character[rand.nextInt(character.length)];
            String convertString = String.valueOf(c);

            g.setColor(Color.WHITE); // border color
                g.setFont(new Font("Tahoma", Font.PLAIN, 30)); // 
    String.valueOf(hrf)
                g.drawString(convertString, x + 500 / 2, y + 500 / 2); // 
border boax t4 huruf muncul
                System.out.println("Latter: " + convertString); 
            }
            repaint();
        }

        @Override
        public void mouseClicked(MouseEvent e) {
            int mx = e.getX();
            int my = e.getY();

            if (mx > x + 10 && mx < x + 10 + 70 && my > y + 10 && my < y + 
10 + 40) {
                isClicked = true;
                System.out.println("YOU CKLIK ON THE BOX");
            } else {
                isClicked = false;
                System.out.println("You click outside the box");
            }
        }

        @Override
        public void mousePressed(MouseEvent e) {
        }

        @Override
        public void mouseReleased(MouseEvent e) {
        }

        @Override
        public void mouseEntered(MouseEvent e) {
        }

        @Override
        public void mouseExited(MouseEvent e) {
        }

        @Override
        public void mouseDragged(MouseEvent e) {
        }

        @Override
        public void mouseMoved(MouseEvent e) {
            int mx = e.getX();
            int my = e.getY();

            if (mx > x + 10 && mx < x + 10 + 70 && my > y + 10 && my < y + 
10 + 40) {
                isMouseMoving = true;
            } else {
                isMouseMoving = false;
            }
        }
    }
}

and below is the code that I tried with CLI:

package testGrafik;

import java.util.Random;

public class TestRandomCLI {

    public static void main(String[] args) {
        char[] character = {'S', 'O', 'I'};
        Random rand = new Random(); 
        char c = character[rand.nextInt(character.length)];
        String convertString = String.valueOf(c);
        System.out.println("Latter: " + convertString);
    }  
}

Upvotes: 0

Views: 235

Answers (1)

George Z.
George Z.

Reputation: 6808

This happens because you call JComponent#repaint() method inside JComponent#paintComponent(). When you call repaint(), paintComponent will be called. Then again, repaint() will be called...and you got the point.

The solution:

Call repaint() only 1 time, when the user clicks.

@Override
public void mouseClicked(MouseEvent e) {
    int mx = e.getX();
    int my = e.getY();

    if (mx > x + 10 && mx < x + 10 + 70 && my > y + 10 && my < y + 10 + 40) {
        isClicked = true;
        System.out.println("YOU CKLIK ON THE BOX");
        repaint(); //Only 1 call after call
    } else {
        isClicked = false;
        System.out.println("You click outside the box");
    }
}

and of course remove it from paintComponent()

 if (isClicked == true) {
        char c = character[rand.nextInt(character.length)];
        String convertString = String.valueOf(c);

        g.setColor(Color.WHITE); // border color
            g.setFont(new Font("Tahoma", Font.PLAIN, 30)); // 
            g.drawString(convertString, x + 500 / 2, y + 500 / 2); // 
            System.out.println("Latter: " + convertString); 
        }
        //repaint();
    }

Upvotes: 1

Related Questions