java - 如果我设置了 ImageIcon 和 Image,为什么我的程序不会 PaintComponent() ?

标签 java image swing netbeans paintcomponent

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

我正在用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,在未注释掉时具有米色空面板外观)。

最佳答案

编辑:

在 setGraphic() 方法中,替换行

graphic = new ImageIcon("setto");

这样:

graphic = new ImageIcon(this.getClass()
                .getResource(setto));  

关于java - 如果我设置了 ImageIcon 和 Image,为什么我的程序不会 PaintComponent() ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19715175/

相关文章:

html - 仅使用 CSS 在其他元素上扩展图像填充

java - JTabbedPane:在选项卡中显示任务进度

java - 用于在 swing 应用程序中滚动 1 GB rtf 文档的 API

java - 新建maven项目

java - 服务器端的Python套接字问题

java - Kafka 日志中缺少偏移量 - 简单消费者无法继续

JavaFX HTMLEditor - 插入图片功能

Python Tkinter 从标签中删除/删除图像

java - 从PDF文档中提取文本并生成结构化数据

Java 实时 JPanel、JLabel + 图像大小调整