Results 1 to 6 of 6
  1. #1
    shakeel is offline Member
    Join Date
    Dec 2010
    Posts
    20
    Rep Power
    0

    Default 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:

    XML 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);
    
                }
            });
        }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    Did you forget to ask a question, or did you just want to share your code?

    db

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    But after a glance at the code, you most definitely need to go through Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing).

    db

  4. #4
    shakeel is offline Member
    Join Date
    Dec 2010
    Posts
    20
    Rep Power
    0

    Default 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?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  6. #6
    shakeel is offline Member
    Join Date
    Dec 2010
    Posts
    20
    Rep Power
    0

    Default

    I tried it and it works perfectly. Thanks a lot for your advice. :)

Similar Threads

  1. Resize JPanel in JScrollBar
    By javawriter in forum AWT / Swing
    Replies: 5
    Last Post: 10-18-2010, 02:38 PM
  2. Problem with JScrollBar
    By Arthur in forum AWT / Swing
    Replies: 3
    Last Post: 02-19-2010, 02:59 AM
  3. A problem with JScrollBar and the Jbutton
    By cowboy in forum New To Java
    Replies: 5
    Last Post: 12-19-2009, 09:17 PM
  4. JScrollBar
    By solomon_13000 in forum AWT / Swing
    Replies: 1
    Last Post: 07-01-2009, 07:46 AM
  5. problem with jscrollbar in my applet
    By leonard in forum Java Applets
    Replies: 1
    Last Post: 08-03-2007, 11:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •