- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我一直在尝试重写并使用 paint 组件方法而不是 paint 方法,因为我已经看到这里的多个问题都建议使用它。
我已经查看了很多问题,但我似乎仍然无法让它发挥作用。我正在发布用于呈现屏幕的原始代码。我认为扩展 JFrame 不是正确的方法,而是我需要扩展 JPanel,并从那里使用绘画组件。我有另一个对象,我实际上在其中扩展了 JPanel,并添加了 JFrame(用于渲染)。
这是我用来渲染的对象,顺便说一下,这个对象可以完美地覆盖 paint 方法。
package render;
import java.util.Arrays;
import javax.swing.*;
import java.awt.*; //Graphics, Graphics2D, Image
import sprites.Picture;
public class Window extends JFrame{
private static final long serialVersionUID = 1L;
public static Picture[] image_list = new Picture[0]; // All the images I want to render
private static String win_title = "Window"; // The name of the window
private static CustomComponents cc = new CustomComponents();
public Window(){
setTitle(win_title); // set my title
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // close when you hit x button
setUndecorated(true); // window has nothing on it
}
// paints all the images in image list according to their priority
public void display(){
add(cc);
CustomComponents.updateImageList(image_list);
pack();
setMinimumSize(getSize());
setLocationRelativeTo(null); // puts the screen in the middle
setResizable(false); // can't resize the window
setVisible(true); // to see the window
}
public double[] getWinSize(){
double[] size = {this.getSize().getWidth(),this.getSize().getWidth()};
return size;
}
public static void endAll(){
for (Picture i:image_list){
i = null;
}
image_list = new Picture[0];
CustomComponents.updateImageList(image_list);
}
// close the window (seen in the game object)
public static void close(){
System.exit(0);
}
// called when adding a new sprite to the image_list array
public static void addPicture(Picture element){
Picture[] result = Arrays.copyOf(image_list, image_list.length +1); // does the same thing as the addObject method in objects
result[image_list.length] = element;
image_list = result;
Arrays.sort(image_list);
CustomComponents.updateImageList(image_list);
}
// updates the screen... Just repaints everything
public void update() {
cc.repaint();
}
}
class CustomComponents extends JComponent {
private static final long serialVersionUID = 1L;
private static Picture[] image_list;
public static void updateImageList(Picture[] image_list){
CustomComponents.image_list = image_list;
}
@Override
public Dimension getMinimumSize() {
return new Dimension(640,360);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(640,360);
}
@Override
public Dimension getMaximumSize() {
return new Dimension(640,360);
}
@Override
public void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
Graphics2D g2d = (Graphics2D) graphics;
for (Picture i:image_list){
if (i.getVisibility()){
g2d.drawImage(i.getPic(), i.getXY()[0], i.getXY()[1], this);
}
}
Toolkit.getDefaultToolkit().sync(); // this is for linux machines
graphics.dispose(); // just good programming practice to collect the garbage
}
}
我会发布实际添加到窗口中的对象,但现在它太复杂了,只发生了几件事。在构造函数中我在上面添加了 JFrame 窗口,然后我使用定时器调用 JFrame 对象上的更新方法。
如果你们真的需要代码,我可以发布,但希望这就足够了。
最佳答案
不画画
JFrame
直接使用 paintComponent()
,JFrame
不是个好主意,JFrame
不是这项工作的合适容器JFrame
使用 paint()
而不是 paintComponent()
, 在您认为应该覆盖父方法的任何方法之前添加@Override
,这里是paintComponent(...)
方法。这样,如果您实际上没有覆盖您认为的内容,编译器会警告您。
将 JComponent/JPanel
放入 JFrame
为 JComponent/JPanel
覆盖 getPreferredSize
(您的硬编码 //窗口大小句柄
),otherwise JComponent / JPanel
returns zero dimension , 屏幕上什么都看不到
关于java - paintComponent 不工作(Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15042185/
我是 Java 的新手。下面是两个类,一个类扩展JComponent,需要重写paintComponent方法。另一个类扩展了JPanel,同样需要重写paintComponent方法。 为什么Dra
我想知道 paintcomponent() 和 paintcomponents() 有什么不同? 当我从 paintcomponent 使用它时,它显示了我们想要的一切,但是 paintcompone
public void paintComponent(Graphics g){ super.paintComponent(g); Graphics2D g2 = (Gr
我稍后在我的主方法中使用这样的调用: public static void main(String[] args) { SwingUtilities.invokeLater(new Runna
所以我正在开发一款由两个网格组成的游戏。每个网格都是一个 JPanel,我在其中(可能)在网格中的每个空间上绘制一个对象。我发现绘制对象正在减慢应用程序的速度,我想知道是否有办法防止这种情况发生。 详
这个问题已经有答案了: paintComponent is not being called in JPanel (1 个回答) 已关闭 4 年前。 很抱歉,这个问题可能已经解决了,但我已经到处搜索但
我的问题非常简单:我有这个小程序,它只是没有显示我正在尝试制作的红框: 主要 public class Main { public static void main(String[] args
我正在尝试编写一个类似这样的应用程序: - 显示对话框 - 当用户单击“确定”时,关闭对话框,转到主应用程序 以下是相关代码片段: public class Owari extends JPanel
这应该是一个抽象类,可以扩展以绘制连接 JPanel 上某些坐标点的线。 但是,永远不会调用专门的 PaintComponents() 方法。我已经看到其他答案并尝试使用他们的解决方案,例如检查我是否
我有以下结构: method1 method2 ... methodn methodX 方法x包含: JFrame frame = new JFrame("Sample"); frame.setDef
我在使用矩形时遇到一些问题 这是我的代码: import java.awt.*; import javax.swing.*; import java.util.*; public class squa
我正在尝试制作西洋跳棋游戏,但以下架构没有在 JFrame 上显示我做错了什么 //基类 import java.awt.event.MouseEvent; import java.awt.event
问题涉及我项目中的 2 个类:主类和绘图类。主类创建 JFrame 并在其中放置 2 个 JPanel。第一个 JPanel 包含用于输入数字的 JTextFields 和用于选择不同选项的 JBut
我已经处理学校作业有一段时间了。但我无法真正理解我应该做什么。作业明天就要交了,我感觉压力很大。 任务是,我将获取一些图片,将它们放在窗口中,然后能够在它们周围移动并能够旋转。 最大的问题是我不知道如
我在使用 PaintComponent 绘制形状时遇到不清楚的问题。 public class Shape extends JPanel { private String shape; private
据我了解,Swing 将决定何时需要重新绘制,这可以解释为什么 paintComponent() 执行两次。但我制作了一个 hibernate 16 毫秒、重绘、 hibernate 16 毫秒、重绘
这是我的代码,它工作完美,并在 JFrame 中绘制形状。每个方法都通过其名称进行调用,在我的程序中我没有调用任何 paintComponent() 方法。那么paintComponent()方法的调
我正在尝试在 JPanel 上绘制一些内容,但似乎我遇到了 paint 和 paintComponent 方法的问题。可能与双缓冲有关,但我不确定。 public void PaintComponen
我是 Swing 新手,正在尝试将图像背景添加到我的 JFrame 中。但是我的 paintComponent 方法不起作用。您能否就如何修复我的代码以便在背景中绘制图像提供一些建议? 代码如下: /
我正在尝试编写简单的程序,当鼠标拖动时绘制圆圈,但paintComponent方法仅被调用一次(启动后)。 在这个类中,我有一些在拖动鼠标时绘制圆圈的方法。 import javax.swing.*;
我是一名优秀的程序员,十分优秀!