gpt4 book ai didi

java - 尝试绘制图像时来源未知

转载 作者:行者123 更新时间:2023-12-01 07:33:59 25 4
gpt4 key购买 nike

我尝试将一些内容绘制到 JGlassPane 。对于矩形等基本形状,效果非常好。但是当我尝试绘制图像时,它总是显示未知来源的错误。我不知道这意味着什么,但我尝试了一切来修复它:尝试了相对/绝对路径,将图像添加为源,将其添加到构建路径,但没有任何效果。

package soft;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class MapObjects
{
private int TypObjektu;//1-osoba,2-vozidlo,3-budova,4-custom
private float []GPSpos;
private String nazov;
private String popis;
private int userId;
private int priority;
private BufferedImage ikona;


public MapObjects(String nazov,String popis,int typ)
{
nazov=this.nazov;
popis=this.popis;
TypObjektu=typ;
File file=new File("D:/workspace/sources/iconPerson.jpg");
try {
ikona = ImageIO.read(file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public Image getImage()
{
return ikona;
}
public void setAsPerson()
{
TypObjektu=1;
}
public void setAsCar()
{
TypObjektu=2;
}
public void setAsBuilding()
{
TypObjektu=3;
}
public void setAsCustom()
{
TypObjektu=4;
}

}

错误消息:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at soft.MapDrawer.paintComponent(MapDrawer.java:34)
at soft.MapDrawer.<init>(MapDrawer.java:22)
at gui.MainWindow.paint(MainWindow.java:189)
at gui.MainWindow$2.actionPerformed(MainWindow.java:146)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

MapDrawer类

package soft;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;

import javax.swing.JInternalFrame;
import javax.swing.JPanel;

public class MapDrawer extends JPanel
{
Graphics testGrafika;
DrawerThread drawingThread;
MapObjects objekt;

public MapDrawer(JPanel drawPanel)
{
drawPanel.add(this);
testGrafika=drawPanel.getGraphics();
paintComponent(testGrafika);
objekt=new MapObjects("tada","dada",1);
drawingThread=new DrawerThread();
drawingThread.start();

}

@Override
public void paintComponent(Graphics g)
{
super.paintComponents(g);
this.setBackground(Color.WHITE);
g.drawImage(objekt.getImage(), 50, 50, null);
}

public Graphics getGraphics()
{
return testGrafika;

}

public class DrawerThread extends Thread implements Runnable
{


@Override
public void run()
{
while(true)
{
paintComponent(testGrafika);
try {
DrawerThread.sleep(30);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}

最佳答案

你的绘画代码完全错误。您几乎从不直接调用paintComponent,在这种情况下当然不会。您的 Graphics 对象可能为 null,因为您是通过在组件上调用 getGraphics() 来获取它的,这是您不应该做的事情,因为该对象不会持久存在。您将需要阅读绘画教程以了解如何正确执行此操作,因为需要更改很多内容。建议包括:

  • 在paintComponent方法中绘图
  • 但不直接调用它
  • 为计时器循环使用 Swing 计时器而不是 while 代码。
  • 使用 repaint() 建议 JVM 重新绘制组件。

同样,您可以通过 Google 找到的教程将解释所有这些内容以及更多内容。

关于java - 尝试绘制图像时来源未知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14735440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com