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

Java BufferedImage / JPanel not updating new black pixel rows using JButton

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

虽然按钮事件到达了updateImage()函数,但是Log输出表明系统调用了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.");
    }

}

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

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.");
    }

}