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

296 views Asked by At

Basically I have a (width x height) size of pixels in a buffered image and would like to draw simple horizontal BLACK lines, one after another into the Buffered Image / JPanel. Each click of the button should create a new horizontal line.

Although the button event reaches the updateImage() function, AND the Log output indicates that paintComponent(Graphics g) is called by the system. There is no update happening. The only time a Line is drawn is on the constructor initialisation. Nothing is being updated to the screen on each button click; only the initial one line is drawn from a test call in the constructor.

The end product is to simulate a printer.

Thank You.

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

}
1

There are 1 answers

3
Alex On BEST ANSWER

You almost answered your own question but forgot to include actual button. Here is how it should be (almost - no static should be used -it's just simple example/improvement of your code):

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

}