Results 1 to 2 of 2
  1. #1
    Floetic's Avatar
    Floetic is offline Member
    Join Date
    Feb 2008
    Location
    Belfast, Northern Ireland, UK
    Posts
    8
    Rep Power
    0

    Question Center background image

    Hi!

    The background image is showing but I would like to center it - how can this be done!?!

    Thanks.


    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import java.io.*;
    import javax.imageio.*;
    import javax.swing.*;
    
    public class NewClass10 extends JPanel {
         final BufferedImage bi;
    
         public NewClass10() throws IOException {
             bi = ImageIO.read(new File("C:\\Documents and Settings\\Flo\\My Documents\\My Pictures\\bigstockphoto_Binary_Globe_55596.jpg"));
         }
    
         public void paintComponent(Graphics g) {
              g.drawImage(bi,getWidth()-bi.getWidth(),getHeight()-bi.getHeight(),null); 
    
             }
         
         public static void main(String[] args) {
             EventQueue.invokeLater(new Runnable() {
                 public void run() {
                     try {
                         JFrame f = new JFrame();
                         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                         NewClass10 t7 = new NewClass10();
                         f.setContentPane(t7);
                         f.setSize(1025,703);
                         f.setVisible(true);
                     } catch (IOException ioe) {
                         ioe.printStackTrace();
                     }
    
                 }
             });
         }
    
    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    int x = (getWidth()-bi.getWidth())/2;
    int y = (getHeight()-bi.getHeight())/2;
    g.drawImage(bi, x, y, null);

Similar Threads

  1. Replies: 2
    Last Post: 03-24-2008, 06:29 AM
  2. center a form
    By tommy in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 09:47 PM
  3. Background image in java
    By toby in forum AWT / Swing
    Replies: 1
    Last Post: 07-29-2007, 08:15 AM
  4. Why this image background is black ?
    By samson in forum Java 2D
    Replies: 1
    Last Post: 07-17-2007, 05:24 AM
  5. Help Center Live 2.1.3
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-06-2007, 04:43 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
  •