i've seen a similar question, but i didn't get a proper solution, so here's my problem: (Coloring an area of BufferedImage)
i'm creating a BufferedImage, then i grab the graphics from this image, i'll paint a green rectangle on it and let it show within a JPanel... but surprise - it's not green, wo where is my error??
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ViewPortTest {
public static void main(String[] args) {
new ViewPortTest().startUp();
}
private void startUp() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(new TestPanel() );
frame.pack();
frame.setVisible(true);
}
private class TestPanel extends JPanel{
private static final long serialVersionUID = 1L;
private int myWidth = 256;
private int myHeight = 156;
private BufferedImage img;
public TestPanel() {
super();
setPreferredSize(new Dimension(myWidth, myHeight) );
img = new BufferedImage(myWidth, myHeight, BufferedImage.TYPE_4BYTE_ABGR);
img.createGraphics();
img.getGraphics().setColor(Color.GREEN);
img.getGraphics().fillRect(0, 0, 256, 256);
img.getGraphics().dispose();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (img != null ){
g.drawImage(img, 0, 0, null);
}
}
}
}
what comes out is this:
so - where is my green rectangle???