Re: program works but wont display in browser
- From: "hiwa" <HGA03630@xxxxxxxxxxx>
- Date: 27 Feb 2006 21:41:25 -0800
Using JApplet is a good idea, but it is
a little bit different from good old
java.applet.Applet. Read the API
documentation of JApplet class and
the tutorial linked from there.
----------------------------------------------------------------------
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
public class WebApplet extends JApplet{
private JButton previous,next;
private JPanel buttonPanel;
Font title, documentText;
int i = 0;
Container con;
ImagePanel ip;
String[] names
= {"Pic1.jpg", "Pic2.jpg", "Pic3.jpg", "Pic4.jpg", "Pic5.jpg" };
Image[] coolImage;
ImageIcon[] iia;
public void init(){
con = getContentPane();
// you should use MediaTracker, or, use ImageIcon that uses
// MediaTracker internally
coolImage = new Image[names.length];
iia = new ImageIcon[names.length];
for (int i = 0; i < names.length; ++i){
iia[i] = new ImageIcon(getImage(getCodeBase(),names[i]));
coolImage[i] = iia[i].getImage();
}
previous = new JButton("Previous");
next = new JButton("Next");
ButtonListener listener = new ButtonListener();
previous.addActionListener(listener);
next.addActionListener(listener);
ip = new ImagePanel();
buttonPanel = new JPanel();
buttonPanel.add(previous);
buttonPanel.add(next);
add(buttonPanel);
con.add(ip, BorderLayout.CENTER);
con.add(buttonPanel, BorderLayout.SOUTH);
}
class ImagePanel extends JPanel{
public ImagePanel(){
setBackground(Color.black);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
title = new Font("Jokerman",Font.BOLD,(int)(getHeight()*0.02) );
documentText
= new Font("Courier New",Font.BOLD,(int)(getHeight()*0.02) );
g.setColor(Color.white);
g.setFont(title);
g.setFont(documentText);
g.drawImage(coolImage[i],30,60,this);
}
} // class ImagePanel
private class ButtonListener implements ActionListener{
public void actionPerformed (ActionEvent event){
if(event.getSource() == next){
i++;
if (i >= names.length){
i = 0;
}
}
else if(event.getSource() == previous){
i--;
if (i < 0){
i = names.length - 1;
}
}
ip.repaint();
}
}
} // class webApplet
------------------------------------------------------------------
.
- Follow-Ups:
- Re: program works but wont display in browser
- From: andrewtitus
- Re: program works but wont display in browser
- From: hiwa
- Re: program works but wont display in browser
- References:
- program works but wont display in browser
- From: andrewtitus
- program works but wont display in browser
- Prev by Date: Re: Java Hangs up
- Next by Date: Re: program works but wont display in browser
- Previous by thread: program works but wont display in browser
- Next by thread: Re: program works but wont display in browser
- Index(es):