Hi - what im trying to do is to create a way of centering an image on a frame no matter what the size of the frame. I have two classes: one called "Picture_Frame.java"; the other "MyImage.java" In the latter I get the image and paint it then call on using the other class; thus adding it to my frame.
When I specify the location of the image in this line:
g2d.drawImage(up, 10, 10, null);
thats all good. But when I do this:
g2d.drawImage(up, ( (frameProperties.getX())/2 ), (frameProperties.getY())/2, null);
I get alot of errors. The complete codes are:
public class myImage extends JPanel
{
// Name of image
Image up;
// The intention of the instance below is to position the picture created in the center of the frame; no matter what the size of the frame
Picture_Frame frameProperties = new Picture_Frame();
public myImage()
{
// Location of the image
ImageIcon ii = new ImageIcon( this.getClass().getResource("up.jpg") );
up = ii.getImage();
}
public void paint(Graphics g)
{
// Paint the image and call it
Graphics2D g2d = (Graphics2D) g;
// drawImage( image, x position on frame, y position on frame, observer)
g2d.drawImage(up, ( (frameProperties.getX())/2 ), (frameProperties.getY())/2, null);
}
}
And the other class is:
import java.awt.Color;
import javax.swing.*;
public class Picture_Frame extends JFrame
{
final int x = 250; final int y = 200;
// Constructor to create the frame
public Picture_Frame()
{
// Add the image to the frame
add( new myImage() );
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(x, y);
setLocationRelativeTo(null);
setTitle("Images");
setVisible(true);
setBackground(Color.RED);
}
public static void main(String[] args)
{
new Picture_Frame();
}
// Return the x size of the frame
public int getX()
{
return x;
}
// Return the y size of the frame
public int getY()
{
return y;
}
}
Whats the problem anyone?