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

标签 java swing jpanel bufferedimage paintcomponent

基本上,我在缓冲图像中有一个(宽度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/

相关文章:

java - 创建类的新对象后获取 NPE

java - 使用java运行风扇

java - 如何使我的 MouseListeners 在反序列化的 JPanel 中工作?

java - 如何过滤映射并返回值列表

java - 如何添加线程 MVC 模式 theView 以避免 JFrame 卡住

java - 如何获取我当前位置的街道的车道数?

Java如何制作Keylistener?

java - 内部 jpanel 上的鼠标事件放置在另一个 jpanel 上

java - Java 中带有三位数字 key 的凯撒密码

java - Android:自定义对话框的大小不准确