1

I need help because I am trying to code a game in Java. I was stopped in my tracks when I found out that it would not draw a string to the JFrame. I have tried several methods of getting around this and done lots of research but found nothing. This is the code:- Oregon (Main Class):

package com.lojana.oregon.client;

import java.awt.*;

import javax.swing.*;

import com.lojana.oregon.src.Desktop;
import com.lojana.oregon.src.Keyboard;
import com.lojana.oregon.src.Mouse;
import com.lojana.oregon.src.Paint;

public class Oregon extends JFrame {
    private static final long serialVersionUID = 1L;

    // Currently unused but there will be a use for it in the future
    public Desktop desktop;

    public String TITLE = "Oregon";

    public Oregon() {
        /* Window code */
        setTitle(TITLE);
        setSize(640, 640);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        /* Extra code for Window */
        addKeyListener(new Keyboard());
        addMouseListener(new Mouse());
    }

    public void paint(Graphics g) {
        Paint.paint(g);
    }
}

GuiButton (Painting Class):

package com.lojana.oregon.src;

import java.awt.*;

public class GuiButton {
    public GuiButton(Graphics g, String text, Font font, int coordX, int coordY,
            int textX, int textY, int width, int height) {
        Color border = Color.gray;
        Color fill = Color.white;
        Color textColor = Color.black;

        Stroke borderSize = new BasicStroke(8);

        g.setColor(border);
        ((Graphics2D) g).setStroke(borderSize);
        g.drawRect(coordX, coordY, width, height);
        g.setColor(fill);
        g.fillRect(coordX, coordY, width, height);
        g.setColor(textColor);
        g.setFont(font);
        g.drawString(text, textX, textY);
    }
}

GuiMainMenu (The file that uses the GuiButton file):

package com.lojana.oregon.src;

import java.awt.*;

public class GuiMainMenu {
    public static void paint(Graphics g) {
        new GuiButton(g, "Start Game", new Font("Arial", Font.BOLD, 20), 60, 80, 20, 20, 240, 40);
    }
}

If you know how to fix it, please comment. Thank you so much :)

Taylor Golden
  • 39
  • 1
  • 7
  • What do you mean it won't draw the `String`? Are you getting an exception? Does everything except `drawString` work? – Jeffrey Jun 10 '12 at 17:57
  • I have tried to make it draw a String to the JFrame using the paint(Graphics g) method. The text I have tried to draw will not show up. When I have tried using just the drawString() method in the main file, it shows the text but shows the program behind it. – Taylor Golden Jun 10 '12 at 18:00
  • Where the heck is the Paint class? this whole approach seems farked including your trying to draw directly on a JFrame. But also there's a lot of code that you're not showing us including critical code. I urge you to go through the Swing drawing tutorials which you can find at the main Java tutorial page to see how to draw correctly. – Hovercraft Full Of Eels Jun 10 '12 at 18:05
  • The main paint() method is in the Main Class (Oregon.java). I have been able to drawString() many times but that was when I was doing it with an Applet. – Taylor Golden Jun 10 '12 at 18:07
  • I was asking about the Paint class, not so much the paint() method. Also, this is not a java.awt.Applet, but a javax.swing.JFrame, and graphics are done differently. Again, please read the API. – Hovercraft Full Of Eels Jun 10 '12 at 18:09
  • How can I read the API? Where is it? Sorry :P – Taylor Golden Jun 10 '12 at 18:10
  • Save the [Java API](http://docs.oracle.com/javase/7/docs/api/) link and refer to it frequently. – Hovercraft Full Of Eels Jun 10 '12 at 18:10
  • For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Jun 10 '12 at 19:04
  • Please do have a look at this [example](http://stackoverflow.com/questions/10809514/using-the-coordinate-plane-in-the-jframe/10811315#10811315), as to How to draw String on JPanel. – nIcE cOw Jun 11 '12 at 01:46

1 Answers1

5

Swing programs are supposed to override paintComponent(Graphics g) instead of paint(Graphics g) and directly to the JFrame. See this article for details: http://java.sun.com/products/jfc/tsc/articles/painting/

In addition it would be better to override the paintComponent of a JPanel that is added to the (content pane of) JFrame instead of the JFrame itself, because you want to draw into this content pane. See this tutorial for details: http://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html

Vincent Zoonekynd
  • 31,893
  • 5
  • 69
  • 78
lbalazscs
  • 17,474
  • 7
  • 42
  • 50