Help with drawing strings on JFrame?!?!
Hey guys, I'm really new to Java (just started AP Comp Sci last month), and we had a project to build a Mastermind application using numbers, which I did. However, I'm trying to learn more on my own, and was hoping to use the example code my teacher gave me to output what I want to say to a JFrame instead of just the command prompt.
Here is the code for my Mastermind class:
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Mastermind extends JFrame
{
public static void main(String args[])
{
int master[] = new int[4];
int win = 0;
for(int x = 0; x<4; x++)
{
master[x] = (int)(Math.random()*10);
}
System.out.println();
do {
int guess[] = new int[4];
int countMaster[] = new int[4];
String numguess = JOptionPane.showInputDialog("Enter your guess (4 digits, please)");
for (int x = 0;x<4;x++)
{
guess[x] = (numguess.charAt(x)-48);
}
int correctlyPlaced = 0;
int correct = 0;
for (int x = 0;x<4;x++)
{
if(master[x] == guess[x])
{
correctlyPlaced += 1;
}
}
for (int x = 0; x<4;x++)
{
for (int y = 0; y<4;y++)
{
if((guess[x]==master[y]) && (countMaster[y]==0)) {
correct++;
countMaster[y]=1;
y=5;
}
}
}
System.out.print("Guess:\t\t\t");
for (int x = 0;x<4;x++) {
System.out.print(guess[x]);
}
System.out.println();
System.out.println("Correct:\t\t"+correct);
System.out.println("Correctly Placed:\t"+correctlyPlaced);
if (correctlyPlaced==4) {
win=1;
}
} while(win<1);
System.out.println("You win!");
System.exit(0);
}
}
And here is the example code that my teacher gave me for how to draw a string on a JFrame:
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Fonts extends JFrame {
public Fonts()
{
super("Using Fonts");
setSize(420,125);
show();
}
public void paint(Graphics g)
{
super.paint(g);
g.setFont(new Font("Serif", Font.BOLD, 12));
g.drawString("Serif 12 point bold.",20,50);
}
public static void main(String args[])
{
Fonts application = new Fonts();
application.setDefaultCloseOperation (
JFrame.EXIT_ON_CLOSE);
}
}
I would like to be able to put "Guess," "Correct," and "Correctly Placed," on a JFrame, with their respective variables. Any ideas? Thank you!!!