By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,470 Members | 1,907 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,470 IT Pros & Developers. It's quick & easy.

applet paint? not sure why this doesn't work...

P: n/a
hi all

im not why this code doesn't work:
/////////

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

public class TestApplet extends Applet {
public void init() {
setLayout(new GridLayout(5, 1));

add(new Label("hello this is label"));
add(new TextField());
add(new Button("a button to press"));
add(new TextArea());
add(new Checkbox("a checkbox to check"));
}

public void paint(Graphics poGraphics) {
super.paint(poGraphics);

System.out.println("here");

Color oForegroundColor = poGraphics.getColor();
poGraphics.setColor(getBackground());
poGraphics.fill3DRect(20, 20, 100, 100, true);
poGraphics.setColor(oForegroundColor);
}
}

////////

what i am trying to achieve is a option pane like popup in the applet
(yes, no, ok, etc) that draws on top of the current components.

i have tried overidding update, paintAll, paintComponents, nothing
draws on top of the current components.

i am writting for 1.1 but using 1.4, dont know if that is relevant.

thanks in adavance
evan
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On 22 Apr 2004 06:43:36 -0700, eh*******@yahoo.com (Evan Dekker) wrote
or quoted :
public void paint(Graphics poGraphics) {
super.paint(poGraphics);

System.out.println("here");

Color oForegroundColor = poGraphics.getColor();
poGraphics.setColor(getBackground());
poGraphics.fill3DRect(20, 20, 100, 100, true);
poGraphics.setColor(oForegroundColor);


Normally you would create a custom Component based on Canvas for AWT
or JPanel for Swing and add that component to your Applet. With Swing
you are supposed to override paintComponent, not paint.

For your experiment, I would use explicit and unique colours e.g.
Color.MAGENTA not getColor and getBackground to eliminate the
possibility that is where your problem lies.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Jul 17 '05 #2

P: n/a
On Thu, 22 Apr 2004 17:07:16 GMT, Roedy Green wrote:
For your experiment, I would use explicit and unique colours e.g.
Color.MAGENTA not getColor and getBackground to eliminate the
possibility that is where your problem lies.


Not the upper case constants,
not for this case!

The OP stated..
"i am writting for 1.1 but using 1.4,
dont know if that is relevant."

The upper case color constants were
only defined in 1.4, you can check it
on the on-line compiler..
<http://www.physci.org/javac.jsp?bcp=11>

The first importable source, CardTest,
will fail for 1.1

And to the OP - the on-line compiler is
a great way to ensure you do not acidentally
introduce post 1.1 classes/methods/attributes.

[ F'Ups set to c.l.j.help ]

HTH

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
Jul 17 '05 #3

P: n/a
In article <66*************************@posting.google.com> , Evan Dekker wrote:
im not why this code doesn't work:
[helpful example program snipped]
what i am trying to achieve is a option pane like popup in the applet
(yes, no, ok, etc) that draws on top of the current components.
You cannot achieve it with heavyweigh AWT components. There is no
defined order in which the applet and the five components in it will be
painted. If you want a popup, create and show a Dialog when appropriate.
i have tried overidding update, paintAll, paintComponents, nothing
draws on top of the current components.


With some implementations of Java, the paint method of the applet may
not be invoked due to painting optimizations. Because the dimensions of
the applet are completely covered by the heavyweight components in it,
the paint method of the applet is not necessarily invoked. Anything
that the paint method of the applet would have drawn, may be drawn over
by the heavyweight components in the applet.

Followup-to set to comp.lang.java.gui.
Jul 17 '05 #4

P: n/a
thanks for the replys ppl, but i solved it myself.

for ppl in the future with the same problem, here is my solution:

////////////
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class TestApplet extends Applet implements ActionListener {
TestCanvas foTestCanvas = null;

public void init() {
setLayout(null);
foTestCanvas = new TestCanvas(this);
foTestCanvas.setLocation(20, 20);
foTestCanvas.setSize(100, 100);
foTestCanvas.setVisible(false);
add(foTestCanvas);

Label oLabel = new Label("hello this is label");
oLabel.setLocation(10, 10);
oLabel.setSize(100, 20);
add(oLabel);

TextField oTextField = new TextField();
oTextField.setLocation(10, 40);
oTextField.setSize(100, 20);
add(oTextField);

Button oButton = new Button("a button to press");
oButton.setLocation(10, 70);
oButton.setSize(100, 20);
add(oButton);

oButton.addActionListener(this);

TextArea oTextArea = new TextArea();
oTextArea.setLocation(10, 100);
oTextArea.setSize(100, 20);
add(oTextArea);

Checkbox oCheckbox = new Checkbox("a checkbox to check");
oCheckbox.setLocation(10, 130);
oCheckbox.setSize(100, 20);
add(oCheckbox);
}

public void actionPerformed(ActionEvent poActionEvent) {
foTestCanvas.setVisible(true);
repaint();
}
}

class TestCanvas extends Canvas implements MouseListener {
TestApplet foTestApplet = null;

public TestCanvas(TestApplet poTestApplet) {
foTestApplet = poTestApplet;
addMouseListener(this);
}

public void paint(Graphics poGraphics) {
Color oForegroundColor = poGraphics.getColor();
poGraphics.setColor(getBackground());
poGraphics.fill3DRect(0, 0, getWidth(), getHeight(), true);
poGraphics.setColor(oForegroundColor);
}

public void mouseClicked(MouseEvent poMouseEvent) {
}
public void mousePressed(MouseEvent poMouseEvent) {
}
public void mouseReleased(MouseEvent poMouseEvent) {
setVisible(false);
foTestApplet.repaint();
}
public void mouseEntered(MouseEvent poMouseEvent) {
}
public void mouseExited(MouseEvent poMouseEvent) {
}
public void mouseDragged(MouseEvent poMouseEvent) {
}
}

/////////////

cheers
evan
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.