View Single Post
  #5 (permalink)  
Old 03-24-2008, 05:14 AM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
private void jButton1MouseReleased(MouseEvent evt) { String path = "/home/birkoff/wallpapers/rei_forever.jpg"; // Using getGraphics is not the way to draw. // Your graphics will not persist with this method. // Use a proper JPanel and override the paintComponent // method as shown above. Graphics g = jScrollPane1.getGraphics(); Graphics2D g2D = (Graphics2D) g; // It is unwise to load images like this. You should load // them at startup and save a reference to them in a variable // as shown above. try { BufferedImage image = ImageIO.read(new File(path)); ImageIcon icon = new ImageIcon(image); // Scrollbar is an AWT component. It is best to not mix // AWT heavyweight components with Swing lightweight // components untill you've had more experience. Scrollbar columnView = new Scrollbar(Scrollbar.HORIZONTAL); Scrollbar rowView = new Scrollbar(Scrollbar.VERTICAL); // ColumnHeader and RowHeader are special component areas // in a JScrollPane. They are not designed to accept // scrollbars. See JScrollPane api for details and the // tutorial page shown below. jScrollPane1.setColumnHeaderView(columnView); jScrollPane1.setRowHeaderView(rowView); // The viewPort is a better choice for background colors. jScrollPane1.setBackground(Color.BLACK); g2D.drawImage(image, (jScrollPane1.getWidth() - image.getWidth())/2, (jScrollPane1.getHeight() - image.getHeight())/2, this); g2D.setPaint(Color.BLACK); // JScrollPane is opaque by default. jScrollPane1.setOpaque(true); // Instead of calling this method we override it in // a JComponent such as JPanel. The example below // shows how to set up a graphic component that does // all the work for you. jScrollPane1.paintComponents(g2D); } catch ( Exception e ) { } finally { } }
It's okay to show an image in event code such as in the mouseReleased method but try to avoid loading an image in event code. You only need to load an image one time. Loading is best done at construction or via a JFileChooser.
For more about JScrollPane see How to Use Scroll Panes.
Code:
import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import javax.imageio.ImageIO; import javax.swing.*; public class AnotherGo { private JScrollPane getContent() { // This image/graphics component knows // how to draw itself and how to report // its display size needs/requirements to // its parent scrollPane/container. AnImagePanel panel = new AnImagePanel(); JScrollPane scrollPane = new JScrollPane(panel); // If you want to configure/customize this // scrollPane this is the place to do so. return scrollPane; } public static void main(String[] args) { AnotherGo test = new AnotherGo(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(test.getContent()); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } } class AnImagePanel extends JPanel { BufferedImage image; Dimension size = new Dimension(); public AnImagePanel() { image = loadImage(); size.setSize(image.getWidth(), image.getHeight()); } protected void paintComponent(Graphics g) { // Center image in this component. int x = (getWidth() - size.width)/2; int y = (getHeight() - size.height)/2; g.drawImage(image, x, y, this); } /** * This method handles the communication of * size requirements with the parent JScrollPane. */ public Dimension getPreferredSize() { return size; } private BufferedImage loadImage() { String path = "images/hawk.jpg"; BufferedImage image = null; try { image = ImageIO.read(new File(path)); } catch(IOException e) { // May as well use what is given... System.out.println("read error:" + e.getMessage()); } return image; } }
Reply With Quote