0

So I'm creating a game where I'll need multiple methods that can output text (to a JFrame) or display images. I've created a GraphicsEngine that has a paintComponent method - but the thing is, this will run through adding it to a JFrame and not through my calls, and I can't call other GraphicsEngine methods because it requires a Graphics2D object... which I won't have when I call the methods. How would I make a bunch of methods that can add stuff to the JFrame without having their own paintComponent? Please help.

Here's my GraphicsEngine, if anybody feels it's relevant.

import javax.swing.JComponent;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

public class GraphicsEngine extends JComponent
{
    public void paintComponent(Graphics g){
        Graphics2D g2 = (Graphics2D) g;
        BufferedImage img = null;
        try {
            img = ImageIO.read(new File("Splash.jpg"));
        } catch (IOException e) {
        }
        g2.drawImage(img, 0, 0, null);
    }

    public void textOut (Graphics2D g2, String text){

        for(char c : text.toCharArray()){
            System.out.print(c); //I want to be able to print this to JFrame     through g2's text printing methods.
            delay(30);
        }
    }  
}
danishanish
  • 345
  • 1
  • 3
  • 13
  • 3
    Advice about image loading: don't load an image every time you paint it, this will be extremely expensive. (`img = ImageIO.read(new File("Splash.jpg"));`) Load it once during initialization and draw it multiple times. – Erwin Bolwidt Mar 08 '15 at 05:36
  • I don't really understand your other question, and I don't think I'm alone. What are you trying to do, and what about it is not working? – Erwin Bolwidt Mar 08 '15 at 05:40
  • 2
    1) `g2.drawImage(img, 0, 0, null);` a `JComponent` **is an** `ImageObserver` so that should be `g2.drawImage(img, 0, 0, this);`. 2) Don't ignore exceptions, it will come back to bite you in the posterior. 3) Given the image might be `null` if not loaded, better to check `if (img==null) {..` prior to painting. 4) Use `@Override` notation on methods that are overidden. 5) Always call the `super.paintComponent(g);` method first. 6) Given the `Splash.jpg` is obviously an application resource, it will be inside the Jar and not available as a `File` at run-time. Load it by URL. – Andrew Thompson Mar 08 '15 at 05:41
  • 1
    Oh, and given the 7 either non-optimal or broken aspects of writing a 9 line method, it is obvious you are inexperienced. I'd recommend simpler projects first. When going to make games, consider using a game engine (where they will hopefully get the basics right). – Andrew Thompson Mar 08 '15 at 05:46
  • It's actually just adding graphics to an existing text based game which i made. yes, im not very experienced with this – danishanish Mar 08 '15 at 06:03

1 Answers1

0

You need to call getGraphics() to get the graphics from JComponent

public void textOut (String text){

        Graphics2D g2= getGraphics();
        for(char c : text.toCharArray()){
            g2.drawString("StackOverflow",40,20); //add your code with g2 to draw text
            delay(30);
        }
}