Results 1 to 6 of 6
Thread: paint with JScrollBar
- 12-20-2010, 10:26 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 20
- Rep Power
- 0
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); } }); }
- 12-20-2010, 08:44 PM #2
Did you forget to ask a question, or did you just want to share your code?
db
- 12-20-2010, 08:45 PM #3
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
- 12-21-2010, 04:26 AM #4
Member
- Join Date
- Dec 2010
- Posts
- 20
- Rep Power
- 0
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.
- 12-23-2010, 04:35 PM #6
Member
- Join Date
- Dec 2010
- Posts
- 20
- Rep Power
- 0
Similar Threads
-
Resize JPanel in JScrollBar
By javawriter in forum AWT / SwingReplies: 5Last Post: 10-18-2010, 02:38 PM -
Problem with JScrollBar
By Arthur in forum AWT / SwingReplies: 3Last Post: 02-19-2010, 02:59 AM -
A problem with JScrollBar and the Jbutton
By cowboy in forum New To JavaReplies: 5Last Post: 12-19-2009, 09:17 PM -
JScrollBar
By solomon_13000 in forum AWT / SwingReplies: 1Last Post: 07-01-2009, 07:46 AM -
problem with jscrollbar in my applet
By leonard in forum Java AppletsReplies: 1Last Post: 08-03-2007, 11:39 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks