using log4j in an applet

J

julia

Hi,

I wrote an applet which gives log output using log4j. The log output
shall be written to a file. I signed the applet, but I still get an
security exception.
Could anyone help me.
I wrote some posted messages about the same problem, but I couldn't
find an answer to it.

Thanks in advance.

Here the code of my applet:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.apache.log4j.Logger;

public class WorkingLetterAnimation extends JApplet {
final Logger log = Logger.getLogger(WorkingLetterAnimation.class);
JFrame f = null;

public void init() {
f = new AnimationFrame(this);
f.addKeyListener(new KeyAdapter() {

public void keyPressed(KeyEvent e) {
}

public void keyReleased(KeyEvent e) {
}

public void keyTyped(KeyEvent e) {
log.info("User has pressed a key for confirmation");
}
});
f.show();
}
}

class AnimationFrame extends JFrame implements Runnable {
final Logger log = Logger.getLogger(AnimationFrame.class);
private JButton start;
private JLabel label;
static final int MAX = 30;
private ArrayList letterList;
private Thread runner;
private String characters = "PQ"; //2 characters
private Font font = new Font("Arial", Font.PLAIN, 80);
int x = 10;
int y = font.getSize() - 10;
private boolean started = false;
private int randomNumber;

WorkingLetterAnimation applet = null;

AnimationFrame(WorkingLetterAnimation applet) {
super("Letter Animation");
this.applet = applet;
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
setResizable(false);
setState(JFrame.NORMAL);
addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent e) {
setState(JFrame.NORMAL);
// if
(e.getPropertyName().equals(JFrame.ICONIFIED)) {
// setState(JFrame.NORMAL);
// }
}
// Boolean b = (Boolean) pce.getNewValue();
// if (b.equals(Boolean.TRUE)) {
// maximizeFrames();
// }

});
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
start = new JButton("Start animation");
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
started = true;
start.setEnabled(false);
log.info("Animation started");
requestFocus(); // only the key events are recorded
start();
}
});
contentPane.add(start, BorderLayout.SOUTH);
DrawingPanel drawingPanel = new DrawingPanel();
contentPane.add(drawingPanel);
drawingPanel.setBackground(Color.white);
contentPane.add(drawingPanel, BorderLayout.CENTER);
setSize(500, 470);
Dimension dim = getToolkit().getScreenSize();
setLocation(
dim.width / 2 - getWidth() / 2,
dim.height / 2 - getHeight() / 2);
}

class DrawingPanel extends JPanel {
Image img = null;

DrawingPanel() {
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
letterList = new ArrayList();
for (int i = 0; i < MAX; i++) {
label = drawLetter(g);
letterList.add(label);
}

}
}

JLabel drawLetter(Graphics g) {
String s = "";
label = new JLabel();
label.setFont(font);
g.setFont(font);

//pause after the last word
if (y < 400) {
int l = (int) (Math.random() * 2);
s = characters.substring(l, l + 1);
label = new JLabel();
label.setLocation(x, y);
label.setSize(x, y);
g.setColor(Color.black);
g.drawString(s, x, y);
label.setText(s);

}
y += font.getSize();
if (y >= 400) {
y = font.getSize() - 10;
x += font.getSize();
}
return label;
}

public void start() {
if (started) {
if (runner == null) {
runner = new Thread(this);
runner.start();
}
}
}

public void stop() {
if (runner != null) {
runner.stop();
runner = null;
}
}

public void run() {
while (started) {
try {
Thread.sleep(6000);
} catch (Exception e) {
}
randomNumber = (int) (Math.random() * letterList.size());
repaint();
}
}

String randomLetter(String string) {
String letter = null;
if (string.equals("R")) {
letter = "P";
}
if (string.equals("P")) {
letter = "R";
}
if (string.equals("O")) {
letter = "Q";
}
if (string.equals("Q")) {
letter = "O";
}
return letter;
}

public void update(Graphics g) {
if (started) {
g.setFont(font);
g.setColor(Color.white);
JLabel l = (JLabel) letterList.get(randomNumber);
g.fillRect(
l.getX(),
l.getY() - 40,
font.getSize() - 10,
font.getSize() - 10);
String s = randomLetter(l.getText());
log.info(
" letter changed from '" + l.getText() + "' to '" + s
+ "'");
g.setColor(Color.black);
g.drawString(s, l.getX() + 4, l.getY() + 23); //!!!
}
}
}
 
I

Ike

Do you have your applet digitally signed? If your applet uses other jars,
very likely, these other jars must be signed (and with the same digital
signature as your applet is). -Ike
 
J

julia

Yes, I signed a jar. In this jar I put the log4j-1.2.8.jar, the
log4j.properties and all the class-files the applet used. I did the
signing with the help of the Go to Java 2 book. There is a chapter
which explains the signing of applets. I think it has something to do
with log4j, because the log4j.properties file writes in a log file and
not the applet itself. But therefore I signed the
log4j.properties,too.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top