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);
}
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With