Results 1 to 5 of 5
  1. #1
    egehring is offline Member
    Join Date
    Oct 2010
    Posts
    1
    Rep Power
    0

    Default 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:

    Java 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:

    Java 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!!!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your teacher's example is not too good as you really shouldn't paint directly on a JFrame but rather in a JPanel's paintComponent method, and then place that JPanel into the JFrame.

    But having said that, if all you want to do is to output sentences, then you're far better off still using a JTextArea that can display multiple lines of text. Just place it into a JScrollPane and add that to your JFrame's contentPane. For details on how to do these sorts of things, have a look at the Oracle Swing tutorials.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Well, since it's cross-posted at Oracle and who knows where else, we can assume that the question has been answered elsewhere and can ignore this thread.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

Similar Threads

  1. Replies: 5
    Last Post: 06-14-2010, 06:10 PM
  2. Drawing strings in Java
    By LyraM in forum Java 2D
    Replies: 4
    Last Post: 05-11-2010, 11:15 PM
  3. Replies: 1
    Last Post: 07-16-2009, 02:15 PM
  4. Replies: 4
    Last Post: 04-22-2009, 11:02 PM
  5. Help with drawing strings!
    By JavaInLove in forum AWT / Swing
    Replies: 1
    Last Post: 02-05-2008, 03:39 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •