英文:
Enabling OpenGL in Java makes edges of JPanel with graphics get cut off
问题
我正在为学校作业制作一个使用Java Graphics2D的游戏。当我在主方法的开头添加以下行以提高性能时,
System.setProperty("sun.java2d.opengl", "true");
图形面板的边缘被裁剪掉了。
我编写了一个小程序来展示这个效果:
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class Main extends JPanel {
public Main() {
JFrame frame = new JFrame("OpenGL off");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(800, 600));
frame.add(this);
frame.pack();
frame.setVisible(true);
}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(Color.RED);
g2.setStroke(new BasicStroke(5.0f));
g2.draw(new Ellipse2D.Double(0, 0, 800, 600));
}
public static void main(String[] args) {
// 启用硬件加速
System.setProperty("sun.java2d.opengl", "true");
new Main();
}
}
没有OpenGL:
[![OpenGL关闭][1]][1]
启用了OpenGL:
[![OpenGL开启][2]][2]
这个问题在Windows上出现,但在Mac上没有。
Java版本:Java 11,操作系统:Windows 10
<sub>*编辑:在Java 14上也出现了这个问题*</sub>
[1]: https://i.ibb.co/GtRDpz8/Open-Gl-off.png
[2]: https://i.ibb.co/8dQvBt7/Open-Gl-on.png
英文:
I'm making a game using Java Graphics2D for a school assignment. When I add the following line to the beginning of my main method to improve performance,
System.setProperty("sun.java2d.opengl", "true");
the edges of the graphics panel get cut off.
I made a small program to show the effect:
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class Main extends JPanel {
public Main() {
JFrame frame = new JFrame("OpenGL off");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(800, 600));
frame.add(this);
frame.pack();
frame.setVisible(true);
}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(Color.RED);
g2.setStroke(new BasicStroke(5.0f));
g2.draw(new Ellipse2D.Double(0, 0, 800, 600));
}
public static void main(String[] args) {
// Enables hardware acceleration
System.setProperty("sun.java2d.opengl", "true");
new Main();
}
}
The problem shows up on Windows but not Mac.
Java Version: Java 11, OS: Windows 10
<sub>EDIT: The problem also occurs with Java 14</sub>
答案1
得分: 1
我发现了一些由Oracle提供的文档,其中提到该命令在某些硬件配置或Java版本上可能无法正常工作。正如你所说,这更多是一种便利性的东西,你可能正在使用错误版本的Java或者不支持此命令的版本。我建议你查阅这份文档: <https://docs.oracle.com/javase/7/docs/technotes/guides/2d/flags.html>
你可能需要升级你的Java版本,或者只是使用你的MAC OS,因为那似乎运行良好。我建议阅读文档,因为其中包含了一些实用的信息。长话短说,问题很可能出在你这边,你应该要么进行研究以找到替代方法,要么就接受现状。我也是一个刚入门的编程学生,我只是决定尝试解决一个问题,希望我有所帮助?
英文:
I found some documentation by Oracle saying that this command may not work with some hardware configurations or versions of java. It's more of a convenience thing, as you said and you may just be using it with the wrong version of Java or one that does not support this command. I suggest that you look at this documentation: <https://docs.oracle.com/javase/7/docs/technotes/guides/2d/flags.html>
You may need to upgrade your Java version or just use your MAC OS since that seems to be working well. I recommend reading the documentation as that has some solid information. Long story short, it's probably a problem on your end and you should either research in order to find an alternative or just settle. I am also a student who is in the beginning of my programming career and I just decided to try and solve a problem, I hope I helped?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论