How do I tie my image to my panel

I

Iain

Hi All

A very new newbie to Java so please be gentle with me.

I have some code that I have cobbled together from various sources on
the web (Thanks to all those whose source has helped me get this far).
I am attempting to write a small test application using MVC which will
open a small window and display an image as a background and then
allow me to move an object around using the keyboard..

I have the code below which opens a window in the middle of the
screen. However, I do not know how to attach the image (img) to the
panel (jpanel).

Can anyone please advise me.

Many thanks in advance for any help offered.

Iain

+++++++++++++++++++++++++++++++++++++++++++++++++++

The relevent code.

/* Variables required. */
private Model model; // The Model to register with
and
// receive updates from
private Controller controller; // The controller for events
private Canvas canvas; // The canvas to be drawn on

Image img;

public ViewGraphical(Model m)
{
super("Test Application Using MVC");
JPanel p = new JPanel();

try
{
String filePath = "C:\\Documents and Settings..............
\
\ThePen.jpg";
img = ImageIO.read(new File(filePath));
}
catch (IOException e)
{
String lerror = e.getMessage();
}

// this.setBackground(new Color(124, 100, 95));

this.setContentPane(p);

this.pack();
this.setVisible(true);
this.setSize(500, 500);

// This will set the panel in the middle of the screen
// First get the actual screen size
Dimension screenSize = Toolkit.getDefaultToolkit
().getScreenSize();

// Get the size and height of the frame object
Dimension size = this.getSize();
// assign the height and width of the screen as half of the
actual
size
screenSize.height = screenSize.height/2;
screenSize.width = screenSize.width/2;
// Assign the frame height and size as half of the current
frame
size
size.height = size.height/2;
size.width = size.width/2;
// Now set the co-ordinates of the frame
int y = screenSize.height - size.height;
int x = screenSize.width - size.width;
// Set the location of the panel
this.setLocation(x, y);

// Make sure that the frame is on top
this.setAlwaysOnTop(true);

model = m;
model.addObserver(this); // Register this class with the
Model
controller = new Controller(model); // Make the Controller
using
the same Model
}
 
A

Andrew Thompson


Please see my answer on usenet newsgroup comp.lang.java.gui.

And please refrain from multi-posting, in future. It is a
fast way to lose friends and disinfluence people (to help).
 
I

Iain

Please see my answer on usenet newsgroup comp.lang.java.gui.

And please refrain from multi-posting, in future.  It is a
fast way to lose friends and disinfluence people (to help).

Apologies for the cross post.
I will refrain in the future

Thanks

Iain
 
A

Andrew Thompson

..
Apologies for the cross post.

Actually, cross-posting between usenet newsgroups is OK.
That is a different thing to multi-posting*.
I will refrain in the future

Now that you are set not to multi-post, all is
'forgiven'*. (* As if it were my place to offer
'forgiveness' ;)

* Do you see the 'Newsgroups:' field above the reply
form? It will read 'comp.lang.java.programmer'. If it
read 'comp.lang.java.programmer,comp.lang.java.gui',
that would be a cross-post.

A cross-post means the message (as well as any replies)
would go to both groups. It is generally considered
good manners to mention that you are cross-posting,
and set a follow-up to just one group (and I usually
invite the repliers to set follow-ups as they see
fit).

____

Please have a look into the SSCCE. I think it will
result in better attention to the technical problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top