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