抽象类没有被调用

时间:2014-02-12 03:06:17

标签: java abstract-class

所以我有一个包含这个的抽象类:

public abstract void render(Graphics g);

我正在使用它从另一个类调用它:

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

由于某种原因,它没有被调用。这是我的课程:

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;
    }
}

这是我的另一堂课:

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");
    }

}

我一直试图找出为什么它不会被整天打电话

1 个答案:

答案 0 :(得分:1)

我建议the tutorials。他们特别好。

这是一个非常简单的程序,可以在面板上绘制平铺图像。如您所见,Graphics对象来自paintComponent,我只需将其交给render。我希望这会有所帮助。

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("http://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);
        }
    }
}

(顺便说一句,你有x = this.x; y = this.y;注意我切换了操作数。这是一个错误。)

crown tile

皇冠图片来自this helpful (though unrelated) answer

我还有其他一些绘画over here的例子。