gpt4 book ai didi

Java BufferedImage/JPanel 不使用 JButton 更新新的黑色像素行

转载 作者:行者123 更新时间:2023-12-02 04:35:50 25 4
gpt4 key购买 nike

基本上,我在缓冲图像中有一个(宽度x高度)大小的像素,并且想在缓冲图像/JPanel中绘制简单的水平黑线,一条又一条。每次单击按钮都会创建一条新的水平线。

虽然按钮事件到达了 updateImage() 函数,但日志输出表明系统调用了paintComponent(Graphics g)。没有更新发生。绘制线条的唯一时间是在构造函数初始化时。每次单击按钮时屏幕上都不会更新任何内容;仅第一行是从构造函数中的测试调用中绘制的。

最终产品是模拟打印机。

谢谢。

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JPanel;

public class ViewPaper extends JPanel {

private BufferedImage buffer = null;
private final int width = 384;
private int height = 500;

private final static Logger LOG = Logger.getLogger(ViewPaper.class
.getName());

public ViewPaper() {
super();
buffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
printDotLine(50);
LOG.log(Level.INFO, "ViewPaper: Load Successful.");
}


/* Update BufferedImage then repaint to screen.
* Note. A Top Level repaint() is also called
* and according to log the paintComponent is
* called but nothing is happening!
*/
public void updateImage(int y) {
printDotLine(y);
repaint();
LOG.log(Level.INFO, "ViewPaper: updateImage(); Called.");
}


/* Update JPanel with updated buffer. */
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.drawImage(buffer, 0, 0, this);
g2d.dispose();
LOG.log(Level.INFO, "ViewPaper: paintComponent(); Called.");
}


/* Sets row of pixels in buffer to black.*/
public void printDotLine(int y) {
for (int x = 0; x < width; x++) {
buffer.setRGB(x, y, Color.BLACK.getRGB());
}
LOG.log(Level.INFO, "ViewPaper: printDotLine(); Called.");
}

}

最佳答案

您几乎回答了自己的问题,但忘记包含实际的按钮。它应该是这样的(几乎 - 不应该使用静态 - 这只是代码的简单示例/改进):

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ViewPaper extends JPanel {

static ViewPaper viewPaper;
static int line=50;
public static void main(String args[])
{
JFrame frame=new JFrame("View Paper");
viewPaper=new ViewPaper();
frame.add(viewPaper);
JButton button=new JButton("Click Me!");
frame.add(button,BorderLayout.SOUTH);
button.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
viewPaper.updateImage(line++);
}
});

frame.setSize(300,500);
frame.setVisible(true);
}
private BufferedImage buffer = null;
private final int width = 384;
private int height = 500;

private final static Logger LOG = Logger.getLogger(ViewPaper.class
.getName());

public ViewPaper() {
super();
buffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
printDotLine(50);
LOG.log(Level.INFO, "ViewPaper: Load Successful.");
}


/* Update BufferedImage then repaint to screen.
* Note. A Top Level repaint() is also called
* and according to log the paintComponent is
* called but nothing is happening!
*/
public void updateImage(int y) {
printDotLine(y);
repaint();
}


/* Update JPanel with updated buffer. */
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.drawImage(buffer, 0, 0, this);
g2d.dispose();
LOG.log(Level.INFO, "ViewPaper: paintComponent(); Called.");
}


/* Sets row of pixels in buffer to black.*/
public void printDotLine(int y) {
for (int x = 0; x < width; x++) {
buffer.setRGB(x, y, Color.BLACK.getRGB());
}
LOG.log(Level.INFO, "ViewPaper: Update Received.");
}

}

关于Java BufferedImage/JPanel 不使用 JButton 更新新的黑色像素行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30770471/

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