1

我试图找到类似的问题,但我认为我在这里有一个独特的情况。

我正在用java编写游戏,我的主类创建了一个框架,它添加了一个组件类,它是JPanel的扩展。基本上,我画了这些椭圆,它们四处移动并做不同的事情,我想在我的一个类中实现一个方法,该方法将使用图像而不是椭圆。但是,每当我尝试从文件创建图像时,程序都不会运行被覆盖的“paintComponent(Graphics g)”方法。

我的主要:

package mikeengine;

import javax.swing.JFrame;

public class MikeEngine {

static final int fx = 1300;
static final int fy = 800;
static final int px = 1292;
static final int py = 767;

static interactivePanel levelPanel;

public static void pause() {
    try {
        Thread.sleep(10); // wait 10ms
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    JFrame frame = new JFrame("MEngine");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(fx, fy);
    frame.setResizable(false);
    frame.setVisible(true);
    levelPanel = new interactivePanel();
    addship();
    frame.getContentPane().add(levelPanel);
    levelPanel.requestFocusInWindow();
    while (true) {
        pause();
        levelPanel.move();
        levelPanel.repaint();
    }
}

static void addship() {
    PlayerShip ship = new PlayerShip(100, 25, 25, px, 0, py, 0);
    ship.setGraphic("C:/Users/asdf/Documents/NetBeansProjects/mikeEngine/src/mikeengine/res/right-arrow.jpg");
    levelPanel.addObject(ship);
    for (int i = 0; i < 5; i++) {
        PlayerShip ship2 = new PlayerShip((int) (Math.random() * 1000) + 100, (int) (Math.random() * 1000) + 100, 25, px, px / 2, py, 0);
        ship2.setMovement((int) (Math.random() * 10) / 5, (int) (Math.random() * 10) / 2);
        levelPanel.addObject(ship2);
    }
}

}

现在,如果我注释掉这一行:

ship.setGraphic("C:/Users/asdf/Documents/NetBeansProjects/mikeEngine/src/mikeengine/res/right-arrow.jpg");

一切都完美呈现。我什至不绘制将要创建的图像。图像变量只是一个类的一部分,它目前只是被创建并且什么都不做,所以我不明白它如何影响paintcomponentMethod(),如果paintComponent()甚至没有被告知尝试绘制图像曾经。

“PlayerShip”类扩展了抽象的“OnScreenObject”类(您真的不需要查看 PlayerShip 的代码,它只覆盖了一个不相关的方法)。

OnScreenObject 看起来像这样:

package mikeengine;

import java.awt.Color;
import java.awt.Image;
import javax.swing.ImageIcon;

public abstract class OnScreenObject {

private ImageIcon graphic;
Image g;
protected int xmin;
protected int ymin;
protected int xsize;
protected int ysize;
protected int rise;
protected int run;
protected int containerYMax;
protected int containerYMin;
protected int containerXMax;
protected int containerXMin;

protected boolean visible;
protected boolean allowedOffscreen;
protected boolean isSelected;

protected Color color;

OnScreenObject(int x, int y, int sizeX, int sizeY, int cxMax, int cxMin, int cyMax, int cyMin) {
    xmin = x;
    ymin = y;
    xsize = sizeX;
    ysize = sizeY;
    containerXMax = cxMax;
    containerXMin = cxMin;
    containerYMax = cyMax;
    containerYMin = cyMin;

    //
    rise = 0;
    run = 0;

    visible = true;
    allowedOffscreen = false;
    isSelected = false;
}

public int getXMin() {
    return xmin;
}

public int getYMin() {
    return ymin;
}

public int getXMax() {
    return xmin + xsize;
}

public int getYMax() {
    return ymin + ysize;
}

public int getXSize() {
    return xsize;
}

public int getYSize() {
    return ysize;
}

public Color getColor() {
    return color;
}

public boolean getAllowedOffscreen() {
    return allowedOffscreen;
}

public boolean getVisible() {
    return visible;
}

public int getRun() {
    return run;
}

public ImageIcon getGraphic() {
    return graphic;
}

public boolean isWithin(int x, int y) {
    return x >= xmin && x <= getXMax() && y >= ymin && y <= getYMax();
}

public void setXMin(int x) {
    xmin = x;
}

public void setYMin(int y) {
    ymin = y;
}

public void setXSize(int x) {
    xsize = x;
}

public void setYSize(int y) {
    ysize = y;
}

public void setGraphic(String setto) {
    try{
    graphic = new ImageIcon("setto");
    g=graphic.getImage();
        System.out.println("tried");
    } catch(Exception e){
        System.out.println("caught:" + e);
    }

}

public void setAllowedOffscreen(boolean allowed) {
    allowedOffscreen = allowed;
}

public void setMovement(int riseM, int runM) {
    rise = riseM * -1;//rise means to go up, and negative will move it up
    run = runM;
}

public void nudge(boolean horizontal, int amount) {
    if (horizontal) {
        run += amount;
    } else {
        rise += amount;
    }
}

public void setVisible(boolean vis) {
    visible = vis;
}

public void setColor(Color c) {
    color = c;
}

public boolean checkCollide(OnScreenObject other) {
    if (other.getYMax() < getYMin()) { //if other object is above this
        return false;
    }
    if (other.getYMin() > getYMax()) {//if other object is below this
        return false;
    }
    if (other.getXMax() < getXMin()) {//if other is to the left
        return false;
    }
    if (other.getXMin() > getXMax()) {//if other is to the right
        return false;
    }
    return true;
}

public void move() {
    if (!allowedOffscreen) {
        checkEdge();
    }
    xmin += run;
    ymin += rise;
}

protected abstract void checkEdge();

}

interactivePanel 类是扩展 JPanel 的类:

package mikeengine;

import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import javax.swing.JPanel;

public class interactivePanel extends JPanel {

ArrayList<OnScreenObject> objects;
IClick myClick;
Ipress myType;
PlayerShip currentShip;

interactivePanel() {
    objects = new ArrayList<>();
    myClick = new IClick();
    myType = new Ipress();
    this.addMouseListener(myClick);
    this.addKeyListener(myType);

}

@Override
public void paintComponent(Graphics g) {
    System.out.println("painting");
    paintBackground(g);
    paintObjects(g);
    checkClick();
    checkPress();
}

public void move() {
    System.out.println("here2");
    checkDeadShip();//also sets current ship
    moveObjects();//also removes invisible
    checkCollisions();

} // end method move

private void checkClick() {
    if (!myClick.getClicked()) {
        return;
    }
    for (int i = 0; i < objects.size(); i++) {
        OnScreenObject current = objects.get(i);
        if (current.isWithin(myClick.getX(), myClick.getY())) {
            System.out.println("CLICKED");
        }
    }
}

private void paintBackground(Graphics g) {
    g.setColor(Color.black);
    g.fillRect(0, 0, getWidth(), getHeight());
}

private void paintObjects(Graphics g) {
    for (int i = 0; i < objects.size(); i++) {
        OnScreenObject current = objects.get(i);
        g.setColor(current.getColor());
        g.fillOval(current.getXMin(), current.getYMin(), current.getXSize(), current.getYSize());
    }
}

public void addObject(OnScreenObject toAdd) {
    objects.add(toAdd);
}

private void checkPress() {
    if (myType.getDown()) {
        objects.get(0).nudge(false, 3);
    }
    if (myType.getUp()) {
        objects.get(0).nudge(false, -3);
    }
    if (myType.getLeft()) {
        objects.get(0).nudge(true, -3);
    }
    if (myType.getRight()) {
        objects.get(0).nudge(true, 3);
    }
    if (myType.getSpace()) {
        fire();
    }
}

private void fire() {
    OnScreenObject shotBullet = new Bullet(currentShip.getXMax(), ((currentShip.getYMax() - currentShip.getYMin()) / 2) + currentShip.getYMin(), 5, getWidth(), 0, getHeight(), 0);
    int shipBonus = 0;
    if (currentShip.getRun() > 0) {
        shipBonus = currentShip.getRun();
    }
    shotBullet.setMovement(0, shipBonus + 5);
    addObject(shotBullet);
}

private void checkCollisions() {
    for (int i = 0; i < objects.size() - 1; i++) {
        for (int j = 0; j < objects.size(); j++) {
            if (j != i) {
                OnScreenObject current = objects.get(i);
                OnScreenObject next = objects.get(j);
                if (current.checkCollide(next)) {
                    objects.remove(i);
                    if (i < j) {
                        objects.remove(j - 1);
                    } else {
                        objects.remove(j);
                    }
                }
            }
        }
    }
}

private void checkDeadShip() {
    if (objects.size() > 0) {
        try {
            currentShip = (PlayerShip) objects.get(0);
        } catch (Exception e) {
            System.out.println("GAME OVER!");
        }
    }
}

private void moveObjects() {
    for (int i = 0; i < objects.size(); i++) {
        OnScreenObject current = objects.get(i);
        current.move();
        if (!current.getVisible()) {
            objects.remove(i);
        }
    }
}
}

如果我没有注释掉该行,则

System.out.println("painting");public void paintComponent(Graphics g)的内部永远不会运行,这就是为什么我认为paintComponent 没有运行。(我不能发布图片,因为我没有 10 个代表,但它只是一个带有米色空面板外观的 JFrame,当它没有被注释掉时)。

4

1 回答 1

2

编辑:

在您的 setGraphic() 方法中,替换该行

graphic = new ImageIcon("setto");

有了这个:

graphic = new ImageIcon(this.getClass()
                .getResource(setto));  
于 2013-10-31T20:09:36.937 回答