So I tried to make a random number generator and made the basics for the whole generator.
When I set the font with frame.getGraphics().setFont(new Font("Dialog", Font.BOLD, 30));
java doesnt take the new set font for frame.getGraphics().drawString("I have to do gen still", 187, 250);
Here is the full code:
package main;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JPanel {
private String name = "Random number generator";
private Container c;
private JFrame frame;
public static JButton startButton;
boolean gameStarted = true;
public void initialise() {
frame = new JFrame("");
startButton = new JButton("Start random number generator");
startButton.setPreferredSize(new Dimension(100, 100));
startButton.setBounds(70, 180, 260, 50);
c = frame.getContentPane();
this.setLayout(null);
this.setBackground(Color.WHITE);
this.add(startButton);
c.add(this);
this.setPreferredSize(new Dimension(400, 400));
frame.setTitle(name);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
Main method;
method = new Main();
method.initialise();
method.isButtonPressed();
}
public void isButtonPressed() {
while(gameStarted) {
if(startButton.getMousePosition(true) != null) {
System.out.println("U did it xd");
gameStarted = false;
startButton.setVisible(false);
frame.getGraphics().setFont(new Font("Dialog", Font.BOLD, 30));
frame.getGraphics().drawString("I have to do gen still", 187, 250);
}
}
}
}
When I set the font with frame.getGraphics().setFont(new Font("Dialog", Font.BOLD, 30));
java doesnt take the new set font for frame.getGraphics().drawString("I have to do gen still", 187, 250);
Here is the full code:
package main;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JPanel {
private String name = "Random number generator";
private Container c;
private JFrame frame;
public static JButton startButton;
boolean gameStarted = true;
public void initialise() {
frame = new JFrame("");
startButton = new JButton("Start random number generator");
startButton.setPreferredSize(new Dimension(100, 100));
startButton.setBounds(70, 180, 260, 50);
c = frame.getContentPane();
this.setLayout(null);
this.setBackground(Color.WHITE);
this.add(startButton);
c.add(this);
this.setPreferredSize(new Dimension(400, 400));
frame.setTitle(name);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
Main method;
method = new Main();
method.initialise();
method.isButtonPressed();
}
public void isButtonPressed() {
while(gameStarted) {
if(startButton.getMousePosition(true) != null) {
System.out.println("U did it xd");
gameStarted = false;
startButton.setVisible(false);
frame.getGraphics().setFont(new Font("Dialog", Font.BOLD, 30));
frame.getGraphics().drawString("I have to do gen still", 187, 250);
}
}
}
}