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.
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.