如何在从Java 7升级到Java 14后绘制图像

huangapple go评论74阅读模式
英文:

How to draw images after upgrade from java 7 to java 14

问题

因此,我编写了一个在JDK 7下运行的游戏,导入和绘制图形如下所示:
ImageReader.java:

public static BufferedImage button_quit;

public void fetchGraphics() {
   try {
    	button_quit = ImageIO.read(new File("rsc/client/gui/button_quit.jpeg"));
    } catch(Exception e) {
    	e.printStackTrace();
    	System.out.println("在导入覆盖图形时,ClientVariables.class中抛出异常。");
    }
}

DrawClient.java:

protected void paintComponent(Graphics g) {
	super.paintComponent(g);
	Graphics2D g2d = (Graphics2D) g;
	g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
	g.drawImage(ClientVariables.button_quit, ClientVariables.quitButtonX, ClientVariables.quitButtonY, null);
	repaint();
}

问题是,在JDK 7中,它工作得很好,但自从我升级到JDK 14后,没有一个图形被导入,它只绘制了所有硬编码的线条和其他内容。我应该如何更改代码以使其再次工作,我毫无头绪(关于如何在Java中绘制图像的教程都是几年前的,因此也是在JDK 7或8中)。

编辑:这是相同的代码,同一个IDE,甚至没有其他存放东西的文件夹。图形的位置应该是正确的。我之前没有提到的是:图形没有被加载。我反复得到堆栈跟踪和我的错误消息。堆栈跟踪:

javax.imageio.IIOException: 无法读取输入文件!
	at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1308)
	at GUI.ClientVariables.<init>(ClientVariables.java:100)
	at MAIN.CodeEntry.main(CodeEntry.java:70)

我怀疑可能的问题是,新版本的代码不认为“rsc/”文件夹位于与“src/”文件夹相同的位置,而可能位于其他地方。

英文:

So i wrote a game under jdk 7 which imported and drew graphics like that:<br>
ImageReader.java:

public static BufferedImage button_quit;

public void fetchGraphics() {
   try {
    	button_quit = ImageIO.read(new File(&quot;rsc/client/gui/button_quit.jpeg&quot;));
    } catch(Exception e) {
    	e.printStackTrace();
    	System.out.println(&quot;Exception thrown in ClientVariables.class while importing overlay graphics.&quot;);
    }
}

DrawClient.java:

protected void paintComponent(Graphics g) {
	super.paintComponent(g);
	Graphics2D g2d = (Graphics2D) g;
	g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
	g.drawImage(ClientVariables.button_quit, ClientVariables.quitButtonX, ClientVariables.quitButtonY, null);
	repaint();
}

The problem - in jdk 7 it worked just fine, but since i upgraded to jdk 14 not a single graphic is imported, it only draws all the lines and stuff that is hardcoded. How do i have to change the code to make it work again, i have no clue (tutorials on how to draw images in java are from years ago therefore also in jdk 7 or 8)

Edit: Its the same code, the same ide not even another folder where stuff is located in. The positions of the graphics are as they should be. What i didn't mention before: The graphics are not loaded. i repeatedly get the stack trace and my error message. Stack Trace:<br>
javax.imageio.IIOException: Can't read input file!<br>
at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1308)<br>
at GUI.ClientVariables.<init>(ClientVariables.java:100)<br>
at MAIN.CodeEntry.main(CodeEntry.java:70)<br><br>
What i suspect to maybe be the problem is that the code in the newer version doesn't expect the "rsc/" folder to be in the same folder as the "src/" folder but maybe somewhere else.

答案1

得分: 0

我自己解决了,可能是IDE出了点问题。基本上,在旧程序中,rsc/文件夹位于与包含此应用程序包的src/文件夹相同的文件夹中。现在程序在上一级文件夹中搜索rsc/文件夹。为什么?不清楚。但问题已解决。

英文:

Well i solved it myself, might be some fuckup by the IDE. Basically, in the old program, the rsc/ folder was located in the same folder as the src/ folder which contains the packages of this application. Now the program searched for the rsc/ folder one folder above. Why? No clue. But it's solved.

huangapple
  • 本文由 发表于 2020年7月22日 03:21:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63021663.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定