Click to See Complete Forum and Search --> : GUI file sourcing


Paddy Notemaker
02-07-2005, 02:42 AM
Dear all,

Recently I made an applet that sourced some pictures for display. I have tried doing something similar in a GUI, but it tells me that the getCodeBase() and getDocumentBase() are invalid commands. I have tried doing this with the java.applet.* imported as well. The code is below-the bit I need help with is in red.

Code
----------------------------------------------------------------------



import javax.swing.*;
import java.awt.*;
import java.applet.*;


public class ContentTest {


private void buildUI(Container container) {
container.setLayout(new BoxLayout(container,
BoxLayout.PAGE_AXIS));

CoordinateArea coordinateArea = new CoordinateArea(this);
container.add(coordinateArea);



coordinateArea.setAlignmentX(Component.LEFT_ALIGNMENT);

}

private static void createAndShowGUI() {
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);

//Create and set up the window.
JFrame frame = new JFrame("ContentTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set up the content pane.
ContentTest controller = new ContentTest();
controller.buildUI(frame.getContentPane());

//Display the window.
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}

public static class CoordinateArea extends JComponent
{

Point point = null;
ContentTest controller;
Dimension preferredSize = new Dimension(400,400);
Color gridColor;
Image picture;



public CoordinateArea(ContentTest controller) {
this.controller = controller;


//The border width definition goes right, left, bottom, then top.
//Measurement is in pixels.
setBorder(BorderFactory.createMatteBorder(5,5,5,5,
Color.black));


setBackground(Color.WHITE);
setOpaque(true);
}

public Dimension getPreferredSize() {
return preferredSize;
}




protected void paintComponent(Graphics g) {
picture=getImage(getCodeBase(), "1.gif");
//Paint background if we're opaque.
if (isOpaque()) {
g.setColor(Color.white);
g.fillRect(0, 0, getWidth(), getHeight());
}

g.setColor(Color.black);
g.drawString("Graphics are working",30,30);
g.drawImage(picture,0,0,this);

}



}

}

----------------------------------------------------------------------
End Code

Thanks to all. Any help would be appreciated.

ray326
02-07-2005, 12:17 PM
Applets expect the resources of a browser to be available. You'll have to use a more conventional technique to determine paths in an application. Oh, and getImage() doesn't exist in your class so maybe that's your whole problem.

Paddy Notemaker
02-07-2005, 09:10 PM
Thanks for that, but I am not sure about the conventional techniques to which you refer. Please elaborate.Thanks as always.

Paddy Notemaker
02-07-2005, 09:27 PM
Sorry,

Just thought that I should mention that I downloaded the Sun 'textbook' from the Sun website and it did not really explain how to do this. Could you please explain how to put an image into a GUI. Thanks.


PS-Ray, you should become an administrator. You spend so much time on this site helping unfortunates like me.

ray326
02-08-2005, 12:25 AM
Have you looked at this one? http://java.sun.com/docs/books/tutorial/uiswing/14painting/

PS-Ray, you should become an administrator. You spend so much time on this site helping unfortunates like me. I would but I couldn't take the pay cut. :D

Paddy Notemaker
02-08-2005, 07:01 PM
Thanks Ray,

But all that is covered in the free java textbook that you can download at the start of all the tutorial paths. I haven't been able to find an example that demonstrates how to put an image into a GUI in a very simplistic matter, as I have no knowledge of image placement in GUI's. Could you declare the file directory in a String variable and put it into the getImage command instead of getCodeBase()/getDocumentBase()? Help appreciated in all it's forms.

PS-Your link covers custom painting, not image insertion.

ray326
02-09-2005, 08:15 AM
PS-Your link covers custom painting, not image insertion.
See, I'm so ignorant of the subject that I don't know the difference. I do all my GUIs in a web browser. Is this better?

http://javaalmanac.com/egs/java.awt/DrawImage.html

Paddy Notemaker
02-09-2005, 05:06 PM
Thanks Ray,

That's great. I think that you have solved my problem. I will get back to you if I need further help. Thanks.

PS-Great research skills.

Paddy Notemaker
02-09-2005, 05:12 PM
Dear Ray,

Just thought I'd ask about how to do your java stuff on a web browser. Do you use a JApplet?

ray326
02-09-2005, 11:38 PM
Originally posted by Paddy Notemaker
Just thought I'd ask about how to do your java stuff on a web browser. Do you use a JApplet? HTML, CSS and the DOM on the browser. Java on the server; generally WebSphere, sometimes Tomcat.