Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print one random answer out of six

Tags:

java

swing

My code is printing out multiple answers when I only want one at a time, could you help me to have it only print one at a time?

I would also like help so that when it runs, there is a button you can press that will refresh the page producing a new question, out of the six anyway.

package ScienceRevision;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Image extends JFrame {


    Font font = new Font("Arial", Font.BOLD | Font.ITALIC, 30 );
    public Image(){

        //Game Properties
        setTitle("WheelOP");
        //setSize(750, 750);
        setResizable(false);

        setBackground(Color.CYAN);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }




    public void paint(Graphics g){
        g.setFont(font);
        g.setColor(Color.RED);

        Random rand = new Random();
        int result = rand.nextInt(6);
        if (result == 0){
        g.drawString("Cell Wall Definition", 100, 100 );
        }
        else if (result == 1){
        g.drawString("Vacuole Definition", 100, 100 );
        }
        else if (result == 2){
        g.drawString("Choroplast Definition", 100, 100 );
        }
        else if (result == 3){
        g.drawString("Nucleus Definition", 100, 100 );
        }
        else if (result == 4){
        g.drawString("Cell Membrane Definition", 100, 100 );
        }
        else if (result == 5){
        g.drawString("Cytoplasm Definition", 100, 100 );
        }
}





    public void paintcomponent (Graphics g){

}
    public static void main(String[] args){
        JFrame jframe = new Image();
        jframe.setSize(750,750);
        jframe.setVisible(true);
        jframe.getContentPane().setBackground(Color.CYAN);
        jframe.setVisible(true);
    }
like image 347
Devon Payne Avatar asked Oct 31 '22 14:10

Devon Payne


1 Answers

Calling JFrame.setSize will invoke the paint method. Set the size after you initialize and remove it from your constructor.

//Game Properties
setTitle("WheelOP");
//setSize(750, 750);
setResizable(false);
setVisible(true);
setBackground(Color.CYAN);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 public static void main(String[] args){
        JFrame image = new Image();
        image.setSize(750,750);
    }

Hope this helps.

Edit: You also should be setting your background color to the content pane not the actual frame. And we should set it visible after doing all of that. So it would look like this:

public static void main(String[] args){
    JFrame jframe = new Image();
    jframe.setSize(750,750);
    jframe.getContentPane().setBackground(Color.CYAN);
    jframe.setVisible(true);
}
like image 181
gonzo Avatar answered Nov 15 '22 05:11

gonzo