I have following code to display and draw JPanel
with buffered image.
I am setting its size to size of buffered image, but it is not getting set actually.
It shows scroll bars when I resize, but when I resize JFrame
greater than size of panel or BufferedImage
, I am still getting mouse events when I click outside of panel's size.
I have omitted extra code.
public class PaintFrame extends JFrame{
private JScrollPane paintScrollPane;
private Painter painter;
private JPanel paintPanel;
private BufferedImage paintImage;
private Color forgroundColor;
public PaintFrame(){
super("Paint");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setPreferredSize(new Dimension(800, 600));
setLocationByPlatform(true);
getContentPane().setBackground(Color.black);
paintImage = new BufferedImage(700, 500, BufferedImage.TYPE_3BYTE_BGR);
paintPanel = new JPanel(){
@Override
public void paint(Graphics g){
if(paintImage != null){
g.drawImage(paintImage, 0, 0, paintImage.getWidth(), paintImage.getHeight(), null);
}
}
};
paintPanel.setBackground(Color.white);
paintPanel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent me){
mouseClickedOnPaint(me);
}
});
paintPanel.setPreferredSize(new Dimension(paintImage.getWidth(), paintImage.getHeight()));
paintScrollPane = new JScrollPane(paintPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
getContentPane().add(paintScrollPane);
pack();
}
}