-
paint with JScrollBar
Hi, Im new to this forum and i need to use a jscrollbar on an image painted in a frame using paint (Graphics g) method. Here is my code:
Code:
public class Warping extends javax.swing.JFrame {
private BufferedImage image;
public Warping() {
initComponents();
}
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("IMAGE WARPING");
setBackground(new java.awt.Color(255, 255, 255));
setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
menuLoad = new javax.swing.JMenuItem();
menuExit = new javax.swing.JMenuItem();
jMenu1.setText("File");
menuLoad.setText("Load");
menuLoad.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
menuLoadActionPerformed(evt);
}
});
jMenu1.add(menuLoad);
menuExit.setText("Exit");
menuExit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
menuExitActionPerformed(evt);
}
});
jMenu1.add(menuExit);
jMenuBar1.add(jMenu1);
private void menuExitActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void menuLoadActionPerformed(java.awt.event.ActionEvent evt) {
openFile();
}
public void openFile()
{
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
String[] extensions = ImageIO.getReaderFileSuffixes();
chooser.setFileFilter(new FileNameExtensionFilter("Image files", extensions));
int r = chooser.showOpenDialog(this);
if (r != JFileChooser.APPROVE_OPTION) return;
try
{
Image img = ImageIO.read(chooser.getSelectedFile());
image = new BufferedImage(img.getWidth(null), img.getHeight(null),BufferedImage.TYPE_INT_RGB);
image.getGraphics().drawImage(img, 0, 0,null);
this.setBounds(0, 0, image.getWidth()+25, image.getHeight()+63);
repaint();
}
catch (IOException e)
{
JOptionPane.showMessageDialog(this, e);
}
repaint();
}
private void filter(BufferedImageOp op)
{
if (image == null){
return;
}
else{
image = op.filter(image, null);
repaint();
}
}
public void paint(Graphics g)
{
super.paintComponents(g);
if (image != null){
image.flush();
Dimension d = getSize();
g.drawImage(image, 0, 0, this);
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Warping().setVisible(true);
}
});
}
-
Did you forget to ask a question, or did you just want to share your code?
db
-
-
paint with Jscrollbar
Oops my bad.. My question is that how to include a Jscrollbar so that when the image size is greater than the frame size, theJscrollbar allows me to scroll down for the remaining part of the picture?
-
Place the Image into an ImageIcon and the ImageIcon into a JLabel. Add the JLabel to the JScrollPane's viewport and it should add scrollbars automatically if the image is larger than the viewport.
-
I tried it and it works perfectly. Thanks a lot for your advice. :)