Reputation: 65
In JFrame i have some buttons and BufferedImgae which will be used to draw on it some shapes and pictures using my own methods ( drawLine, drawing raster picture pixel by pisel and so on). Here is how i add things to JFrame
public class Main extends javax.swing.JPanel {
JPanel panel;
JFrame fr;
Graphics2D g2;
ImageIcon icon;
BufferedImage img;
public void init()
{
fr = new JFrame("Lab 2");
fr.setMinimumSize(new Dimension(1350, 650));
fr.setMaximumSize(fr.getMinimumSize());
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel glowny = new JPanel(new BorderLayout());
glowny.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
glowny.add(getBorderCenter(), BorderLayout.CENTER);
fr.add(glowny);
fr.pack();
fr.setVisible(true);
}
private JScrollPane getBorderCenter()
{
img = new BufferedImage( fr.getWidth()-40, fr.getHeight()-40-50, BufferedImage.TYPE_INT_RGB); //20+20 odstępy w glowny, 50 - szerokość paska z guzikami
icon = new ImageIcon(img);
return new JScrollPane(new JLabel(icon));
}
public static void main(String [] args)
{
Main m = new Main();
m.init();
}
}
Then i try to draw on BufferedImage using double buffering. The following example change BufferedImage color from black (current) to white.
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("paintComponent");
WritableRaster raster = img.getRaster();
DataBuffer db = raster.getDataBuffer();
int[] pixels = ((DataBufferInt)db).getData();
int adres = 0;
for (int y = 0; y < img.getHeight(); y++)
{
adres = y * img.getWidth();
for (int x = 0; x < img.getWidth(); x++)
{
pixels[adres] = 16777215;
adres += 1;
}
}
Graphics2D g2dComponent = (Graphics2D) g;
g2dComponent.drawImage(img, null, 0, 0); // draw buffer on screen
}
As i understand repaint() force calling paintComponent(). The problem is that it doesnt matter how i call repaint()
fr.repaint();
glowny.repaint();
repaint();
its never called.
Upvotes: 1
Views: 55
Reputation: 285405
You never add Main
to the GUI. You need to add the JPanel that overrides the paintComponent, the this
, to the GUI somewhere and you don't. For example, you would need somewhere something like:
someComponentShownInGui.add(this);
I would get rid of the glowny variable and instead use this
.
i.e.,
public void init()
{
fr = new JFrame("Lab 2");
fr.setMinimumSize(new Dimension(1350, 650));
fr.setMaximumSize(fr.getMinimumSize());
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// JPanel glowny = new JPanel(new BorderLayout());
// glowny.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
// glowny.add(getBorderCenter(), BorderLayout.CENTER);
// fr.add(glowny);
setLayout(new BorderLayout();
setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
add(getBorderCenter(), BorderLayout.CENTER);
fr.add(this);
fr.pack();
fr.setVisible(true);
}
Note that anything components added to the drawing JPanel will cover up its image, and so you may need to make some non-opaque.
Upvotes: 3
Reputation: 2091
You should add all you components inside a JPanel then override paintComponent method of the panel. (JFrame doesn't have paintComponet but paint method and it's not recommanded to override it)
Upvotes: 1