我创建了一个显示图片的applet,并希望将该applet加载到Frame中。我一直无法弄清楚(或在网上找到)实现AppletStub接口的正确方法,因此使用Frame并访问applet的getDocumentBase()和getCodeBase()方法。任何帮助将不胜感激。以下是三个类:" MyGame"小程序,框架I尝试将applet放入,以及AppletStub接口。我认为问题在于AppletStub接口的方法,它返回applet的getDocumentBase和getCodeBase,但我不完全确定。感谢任何能提供帮助的人。
public class MyGame extends Applet implements ActionListener, Runnable, AppletContext, AppletStub {
Button b1=new Button("HELLO");
int x, y = 0;
private Image i; // this is for double buffering
boolean changeColor=false;
URL url;
private Graphics doubleG;
Image car;
public void init(){
setSize(700,700);
setBackground(Color.BLACK);
b1.addActionListener(this);
//add(b1);
try{
url=getDocumentBase();
}catch(Exception e){
}car=getImage(url, "Resources/sprite.png");
}
public void start(){
Thread mg = new Thread(this);
mg.start();
}
public void update(Graphics g) //double buffering
{//start update
if(i == null)
{
i = createImage(this.getSize().width, this.getSize().height);
doubleG=i.getGraphics();
}
doubleG.setColor(Color.black);
doubleG.fillRect(0, 0, this.getSize().width, this.getSize().height);
doubleG.setColor(Color.black);
paint(doubleG);
g.drawImage(i, 0, 0, this);
}//end update
public void paint(Graphics g){
if(changeColor)
setBackground(Color.RED);
g.setColor(Color.white);
g.drawString("HOLA", 10, 10);
g.drawString("hi", 100, 100);
g.fillRect(x, y, 10, 10);
g.drawImage(car, 100, 100, this);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b1){
changeColor=true;
}
}
@Override
public void run() {
while(true){
x++;
y++;
repaint();
try{
Thread.sleep(20);
} catch(InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void appletResize(int arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public Applet getApplet(String name) {
// TODO Auto-generated method stub
return null;
}
@Override
public Enumeration<Applet> getApplets() {
// TODO Auto-generated method stub
return null;
}
@Override
public InputStream getStream(String key) {
// TODO Auto-generated method stub
return null;
}
@Override
public Iterator<String> getStreamKeys() {
// TODO Auto-generated method stub
return null;
}
@Override
public void setStream(String key, InputStream stream) throws IOException {
// TODO Auto-generated method stub
}
@Override
public void showDocument(URL url) {
// TODO Auto-generated method stub
}
@Override
public void showDocument(URL url, String target) {
// TODO Auto-generated method stub
}
}
和
public class HelloAppSwing {
public static void main(String args[]) {
final Applet applet = new MyGame();
Frame frame = new Frame("AppletWithAppletStub");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent event) {
applet.stop();
applet.destroy();
System.exit(0);
}
});
frame.add("Center", applet);
frame.setSize(700, 700);
// setting AppletStub
applet.setStub(new AppletStubInterface(args, applet));
frame.setVisible(true);
applet.init();
applet.start();
frame.pack();
}
}
JFrame f = new JFrame();
JPanel leftPanel=new JPanel();
JPanel rightPanel=new JPanel();
JPanel topPanel=new JPanel();
JPanel bottomPanel=new JPanel();
f.setLayout(new BorderLayout());
MyGame myGame = new MyGame();
myGame.init();
myGame.start();
f.setSize(900, 800);
f.add(myGame, BorderLayout.CENTER);
leftPanel.setBackground(Color.GRAY);
rightPanel.setBackground(Color.GRAY);
topPanel.setBackground(Color.GRAY);
bottomPanel.setBackground(Color.GRAY);
topPanel.setPreferredSize(new Dimension(900, 50));
bottomPanel.setPreferredSize(new Dimension(900, 50));
leftPanel.setPreferredSize(new Dimension(100, 800));
rightPanel.setPreferredSize(new Dimension(100, 800));
f.add(leftPanel, BorderLayout.LINE_START);
f.add(rightPanel, BorderLayout.LINE_END);
f.add(topPanel, BorderLayout.PAGE_START);
f.add(bottomPanel, BorderLayout.PAGE_END);
f.setVisible(true);
f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
和
public class AppletStubInterface implements AppletStub {
private Hashtable<String, String> hashtable;
private Applet applet;
public AppletStubInterface(String args[], Applet a) {
applet = a;
hashtable = new Hashtable<String, String>();
for (int i = 0; i < args.length; i++) {
try {
StringTokenizer parser = new StringTokenizer(args[i], "=");
String name = parser.nextToken().toString();
String value = parser.nextToken("\"").toString();
value = value.substring(1);
hashtable.put(name, value);
} catch (NoSuchElementException e) {
e.printStackTrace();
}
}
}
public void appletResize(int width, int height) {
applet.resize(width, height);
}
public AppletContext getAppletContext() {
return null;
}
public java.net.URL getCodeBase() {
String host;
if ((host = getParameter("host")) == null) {
try {
host = InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
java.net.URL url = null;
try {
url = new java.net.URL("file/" + host);
} catch (Exception e) {
}
return url;
}
public java.net.URL getDocumentBase() {
return getCodeBase();
}
public String getParameter(String param) {
return (String) hashtable.get(param);
}
public boolean isActive() {
return true;
}
}