Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Canvas painting in Netscape

0 views
Skip to first unread message

Doug Armstrong

unread,
Mar 7, 2000, 3:00:00 AM3/7/00
to
I've written a simple, sample applet for my website which contains an
object (MyCanvas) which was subclassed from the Canvas class. The applet
displays properly when viewing it with Applet Viewer, and with IE 4.5
and 5.0 (both on Mac and PC). However the canvases do not display
properly when viewing the applet using Netscape Navigator 4.7. On the
PC, the MyCanvas objects flicker, although there is no animation within
the MyCanvas object, just static text. On the Mac, the MyCanvas objects
don't even appear!!

The code is below. Could someone please tell me what I'm doing wrong.

Snoop
sn...@mediaone.net

/*
* 1.0 version.
*/

import java.awt.*;
import java.applet.Applet;

class MyCanvas extends Canvas
{
MyCanvasApplet ParentApplet;
Dimension size;
int w, h, string_x, string_y, clickCount;
boolean trueSizeKnown;

public MyCanvas(MyCanvasApplet highestContainer, int initialWidth,
int initialHeight)
{
this.ParentApplet = highestContainer;

setBackground(Color.white);
setForeground(Color.black);

w = initialWidth;
h = initialHeight;

string_x = 1;
string_y = (h - 3);

clickCount = 0;

size = new Dimension(w,h);
}

public Dimension getPreferredSize() {
return getMinimumSize();
}

public synchronized Dimension getMinimumSize() {
return size;
}

public boolean mouseDown(Event event, int x, int y) {
clickCount += 1;
repaint();
return true;
}

public boolean mouseEnter(Event event, int x, int y) {
setForeground(Color.blue);
repaint();
return true;
}

public boolean mouseExit(Event event, int x, int y) {
setForeground(Color.black);
repaint();
return true;
}

public void paint (Graphics g) {
Color bg = getBackground();
Color fg = getForeground();

g.setColor(fg);
setFont(new Font("Helvetica",Font.PLAIN,9));

g.drawString("Clicks = " + clickCount, string_x, string_y);

g.drawLine(0, string_y + 2, w, string_y + 2);
}
}

public class MyCanvasApplet extends Applet implements Runnable {
MyCanvas[] canvasArray;
String paramValue;
int numCanvases;
String banner;
String sizeBanner;
Dimension appletDimension;
Integer W, H;
private Thread canvasThread = null;

public void init() {
GridBagLayout gridBag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();

int w, h, offset, scalar, j;

/* setLayout(gridBag); */

setBackground(Color.white);

paramValue = getParameter("width");
W = new Integer(paramValue);
w = W.intValue();
paramValue = getParameter("height");
H = new Integer(paramValue);
h = H.intValue();
appletDimension = new Dimension(W.intValue(), H.intValue());
sizeBanner = new String(appletDimension.toString());

banner = new String("This is version 0.01 of the MyCanvas applet
" + sizeBanner);

numCanvases = 3;

canvasArray = new MyCanvas[numCanvases];

for (int i = 0; i < numCanvases; i++) {
canvasArray[i] = new MyCanvas(this, 80, 10);
/*
c.weightx = 1.0;
c.weighty = 1.0;

gridBag.setConstraints(Pick[i], c);

*/
add(canvasArray[i]);
validate();
}


setFont(new Font("Helvetica",Font.PLAIN,9));

repaint();

}

public void start() {
if (canvasThread == null) {
canvasThread = new Thread(this, "Canvas Test");
canvasThread.start();
}
}

public void stop() {
canvasThread = null;
}

public void run() {
Thread myThread = Thread.currentThread();
while (canvasThread == myThread) {
repaint();
}
}

public void paint( Graphics g ) {
int banner_start;
FontMetrics fm = g.getFontMetrics(g.getFont());

banner_start = (W.intValue() / 2) - (fm.stringWidth(banner)/2);

g.drawString(banner, banner_start, H.intValue() - 5);

}

}

0 new messages