- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个带有 JPanel 和 PaintComponent() 的类。我还有一个实现 Runnable 的类,我计划在线程启动后将图像绘制到 PaintComponent 上。我的构造函数接收 JPanel,然后从那里调用 getGraphics()。然而,通过测试和搜索似乎总是返回 null。
System.err.println("Thread Started");
isRunning = true;
System.err.println(pap.getGraphics());
Graphics g = pap.getGraphics(); //pap is the name of the JPanel
while (isRunning)
{
while(xPos <= pap.getWidth() + 1)
{
xPos+=horizontalDirection;
System.err.println(xPos);
drawImage(upImgs[1], xPos, yPos, g);
pap.repaint();
pause();
//g.drawOval(xPos, 10, 10, 10);
if(xPos >= pap.getWidth())
horizontalDirection = -horizontalDirection;
if(xPos < 1)
horizontalDirection = 1;
}
//pap.repaint();
//pause(); // let this thread sleep a bit
}
System.err.println("Thread Ended");
返回 线程已启动 2 无效的 线程“Thread-1”中的异常 java.lang.nullPointerException
我怎样才能正确地让paintComponent从这个单独的类中进行绘制?
最佳答案
你不能这么做。 Awt/Swing 不是线程安全的,因此绘图只能从 gui 线程完成。
关于java getGraphics()返回null,如何访问paintComponent?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22232507/
在 drawToScreen 方法内调用 getGraphics() 时,我的 JPanel 预览返回 null。 Test 类也扩展了 JPanel,因为它被保存在 TabbedPane 中。该类还
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我正在尝试在扩展了 JComponent 类的地方呈现自定义 Swing 组件。 为了简化组件需求,让我们将我的组件总结为需要渲染几个字符串,每个字符串都有自己的字体。 我需要我的组件的大小完全符合我
我的应用程序看起来像这样,我在 draw() 方法中遇到空指针异常,准确地说是在 g.drawImage(img, 0, 0, null) 处 package com.ochs.game; impor
我正在做一个选择工具,我已经提供了这些解决方案,在第一个使用 JPanel getGraphics 方法中,我在鼠标拖动的地方绘制一个椭圆形,在第二个中,我重写 PaintComponent 方法来绘
我有一个带有 JPanel 和 PaintComponent() 的类。我还有一个实现 Runnable 的类,我计划在线程启动后将图像绘制到 PaintComponent 上。我的构造函数接收 JP
嗨,我是 java GUI 编程新手。我创建了 Jframe(MainFrame) 并添加了 JPanel(OutPanel),其中还有另一个 Jpanel(InnerPanel)。我尝试在 Inne
我开始学习用 Java 创建游戏,我使用的其中一种方法包括 BufferedImage。这是我得到的错误: "Exception in thread "main" java.lang.NullPoin
由于 Java 只支持单继承,我希望直接在 JPanel 的实例上绘制,它是类 面板。我从成员那里获取了一个 Graphics 的实例,然后在上面绘制我想要的任何东西。 我怎么能不继承 JCompon
我有底部导航,我也替换了 标记为 FragmentContainerView它给了我关于它没有设置 NavController 的错误,然后我被发布 this问题并解决了问题,但是当方向更改并在底部导
我想知道出了什么问题。我以前曾这样做过,但由于某种原因,这次我无法让它发挥作用。 我在 Netbeans 中设计了该项目。最相关的部分是 initTiras 方法,它应该绘制一些东西,但它没有。我包含
我正在尝试一个充当按钮但使用图像进行显示的对象。我的问题是,当调用 getGraphics() 时,它返回 null。我到处找也没找到原因? 我的构造函数的代码是...... public class
我在这里遇到了一些困难。我在尝试解决这个问题时在其他地方读到,你永远不应该 getGraphics()。问题是,我无法使用 Paint()/PaintComponent() 方法提供的 Graphic
下面是代码(我在这个类中扩展了 JFrame)(Point 'size' 是屏幕的大小): setVisible(true); backBuffer = createImage(size.x, siz
public void saveImage(String path){ BufferedImage image = (BufferedImage) createImage(500, 500);
如果我正在使用 JPanel,getGraphics 返回的 Graphics 对象与在 paintComponent 方法中传递的 Graphics 对象有什么区别? 最佳答案 getGraphic
我在调用 BufferedImage.getGraphics() 方法的某些框架 API 时遇到问题,从而导致内存泄漏。这个方法的作用是它总是调用 BufferedImage.createGraphi
如何在没有类(扩展 JFrame)的情况下绘制对象?我找到了 getGraphics 方法,但它没有绘制对象。 import javax.swing.*; import java.awt.*; pub
(这是 previous 问题的后续) 将 BufferedImage 从一种类型转换为另一种类型或制作副本的标准推荐方法是使用 getGraphics().drawImage() ( example
MadProgrammer 发表了评论here声明您永远不应该使用 Component.getGraphics,我普遍同意这一点:在几乎所有情况下,它都是错误的。 但以我多年前创建的这个类为例。我正在
我是一名优秀的程序员,十分优秀!