1

So I have an abstract class that contains this:

public abstract void render(Graphics g);

I'm calling it from another class using this:

@Override
public void render(Graphics g) {
    g.drawImage(texture.getImage(), x, y, null);
    System.out.println("Render called"); //This is debug
}

For some reason, it's not being called. Here are my classes:

package tile;

import java.awt.Graphics;
import java.awt.Image;

public abstract class Tile {

    public int x, y;
    public Image image;
    public abstract void render(Graphics g);
    public Tile (int x, int y) {

        x = this.x;
        y = this.y;
    }

    public void setLocationPoint(int x, int y) {
        x = this.x;
        y = this.y;
    }
}

Here is my other class:

package tile;

import java.awt.Graphics;

import javax.swing.ImageIcon;

public class TileSmallCloud extends Tile {

    public ImageIcon texture;
    public int x;
    public int y;

    public TileSmallCloud(int x, int y, ImageIcon image) {
        super(x, y);
        texture = image;
    }

    @Override
    public void render(Graphics g) {
        g.drawImage(texture.getImage(), x, y, null);
        System.out.println("Rendercalled");
    }

}

I've been trying to find out why it's not being called all day

morgano
  • 17,210
  • 10
  • 45
  • 56
loafy.
  • 103
  • 10
  • 3
    how are you calling this method? – Prasad Kharkar Feb 12 '14 at 03:12
  • 1
    You've defined the render() method. Nowhere in the code you've shown us do you actually invoke render(). – keshlam Feb 12 '14 at 03:15
  • I'm calling the method by using the @Override public void render(Graphics g) { //code } – loafy. Feb 12 '14 at 03:20
  • 1
    Methods don't call themselves. – Radiodef Feb 12 '14 at 03:21
  • Keshlam - I know this is a stupid question, but how would I invoke it? - I'm kind of new to this - – loafy. Feb 12 '14 at 03:21
  • You need an instance of the class and then you call the method like `someTileSmallCloud.render(someGraphics);`. Maybe you should check out [the tutorials](http://docs.oracle.com/javase/tutorial/java/javaOO/index.html). (The tutorials are good...) – Radiodef Feb 12 '14 at 03:24
  • @Radiodef, the problem is, I can't figure out a way to make a new Graphics() outside of a class, so I won't be able to call my method. – loafy. Feb 12 '14 at 03:33
  • In general, Graphics objects either come from [paint, paintComponent](http://docs.oracle.com/javase/tutorial/uiswing/painting/) or from some kind of Image like a [BufferedImage](http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferedImage.html#createGraphics()). I don't know where yours is supposed to come from. – Radiodef Feb 12 '14 at 03:36
  • That's what I'm trying to figure out o_o – loafy. Feb 12 '14 at 03:40

1 Answers1

1

I recommend the tutorials. They are particularly good.

This is a really simple program that paints a tiled image on a panel. As you can see, the Graphics object comes from paintComponent and I just hand it in to render. I hope this helps.

import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
import java.net.*;

class TilePanel
extends JPanel {
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();

                TilePanel tp = new TilePanel(5, 4);

                frame.setContentPane(tp);
                frame.pack();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    Tile[][] tiles;

    TilePanel(int w, int h) {
        tiles = new Tile[w][h];

        for(int i = 0; i < w; i++) {
            for(int k = 0; k < h; k++) {
                tiles[i][k] = new CrownTile(i * 64, k * 64);
            }
        }

        setPreferredSize(new Dimension(w * 64, h * 64));
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        for(Tile[] t : tiles) {
            for(Tile tile : t) {
                tile.render(g);
            }
        }
    }

    static abstract class Tile {
        int x, y;

        Tile(int x, int y) {
            this.x = x;
            this.y = y;
        }

        abstract void render(Graphics g);
    }

    static class CrownTile
    extends Tile {

        static BufferedImage CROWN;

        static {
            try {
                BufferedImage imgFromStack = (
                    ImageIO.read(new URL("https://i.stack.imgur.com/memI0.png"))
                );

                CROWN = new BufferedImage(64, 64, BufferedImage.TYPE_INT_ARGB);

                Graphics2D g2d = CROWN.createGraphics();
                g2d.drawImage(imgFromStack, 0, 0, null);
                g2d.dispose();

            } catch(IOException ioe) {
                ioe.printStackTrace(System.err);
            }
        }

        CrownTile(int x, int y) {
            super(x, y);
        }

        @Override
        void render(Graphics g) {
            g.drawImage(CROWN, x, y, null);
        }
    }
}

(By the way, you had x = this.x; y = this.y; so notice I switched the operands. It was a bug.)

crown tile

The crown image comes from this helpful (though unrelated) answer.

I also have some other examples of painting over here.

Community
  • 1
  • 1
Radiodef
  • 37,180
  • 14
  • 90
  • 125