views:

104

answers:

3

Hey guys I've got a nice text based java program that I'd like to add a GUI to.

I have dabbled with Netbeans and shouldn't have too much of a problem getting the components in place.

However, the program will have to dynamically update images within the interface window, and I'm not sure what the best way to go about doing it is.

Are there any common practices for moving images (PNG's, JPEG's...) around in the interface? Are there any good resources for getting very basic information on this?

Thanks

+1  A: 

A little more information might be helpful.

If you want to re-lay-out the images, have a look at the Swing LayoutManager class. You can put the image in an ImageIcon, the ImageIcon in a JLabel, and the layout manager can position and re-position the JLabels on a JPanel.

If you want images to be draggable, convention is to put the JLabel into another container component (as labels do not usually support mouse or tab events).

You can look those classes up in the Swing tutorial.

There are also plenty of additional frameworks for making Swing work better for you -- some listed here

Alternatively, JavaFX also provides a quick way for doing Java GUIs that can be quicker and easier to write (if you don't mind learning a new scripting language). For example, it makes adding animation (the images swooshing into place) easier.

William Billingsley
+1  A: 

Java has a nice and powerful API for drawing graphics and images. Have a look at the 2D Graphics tutorial from Sun's set of Java tutorials.

If you want to do animation, you'll find the timing framework useful.

A nice book about making great-looking, animated GUIs in Java is Filthy Rich Clients by Chet Haase and Romain Guy.

Jesper
A: 

Do you mean having images fly around on top of buttons and stuff? Or just within a specific region?

If it's within a specific region, and you don't need to move buttons and other GUI items around, then the simplest way to do it would be to create a Canvas (or a swing component and overload the paint function) and then use the Graphics2D API to draw stuff. The basic outline would be:

public class MyClass extends JComponent{

    Image myPicture;
    int x, y; //location to draw the picture.
    ...
    public void paint(Graphics g) {
          Graphics2D gx = (Graphics2D)g; //cast to graphics 2D
          g.drawImage(myPicture,x,y,this);
    }
    ...
}

Then just change the values of x and y to move the image.

Chad Okere
The graphics will not overlap any of the GUI elements. I'm just a little overwhelmed by all the terminology, and am not sure what the best way to do things is... I just want to keep it simple.I have the netbeans code, but there is no paint() method in the code that it has generated. Shall I just add in the method myself and handle the updating of the graphics withing it?
Allen
yeah, just add a paint function. Make sure the function signature is "public void paint(Graphics g)" and your function should run.
Chad Okere