Absolute position of a Canvas?
I've extended Canvas to make a custom button, but when I add() it to my applet it pops up at the top of the display area. setLocation(int,int) doesn't make a change. I've read something about Layout managers, what am I supposed to do?
CustomButton newButton =new CustomButton("petrus",81,33);
add(newButton);
class CustomButtonextends Canvas{
String label;
Dimension minSize;
int w, h;
public CustomButton(String label,int width,int height){
this.label = label;
w = width;
h = height;
minSize =new Dimension(w,h);
setBackground(Color.black);
setLocation(0,0);
setCursor(new Cursor(Cursor.HAND_CURSOR));
setVisible(false);
}
public Dimension getPreferredSize(){
return getMinimumSize();
}
publicsynchronized Dimension getMinimumSize(){
return minSize;
}
publicvoid paint (Graphics g){
g.setColor(new Color(255,204,0));
g.drawLine(0, h-1, 0, 0);
g.drawLine(0, 0, w-1, 0);
g.setColor(new Color(204,153,0));
g.drawLine(w-1, 0, w-1, h-1);
g.drawLine(w-1, h-1, 0, h-1);
g.setColor(new Color(255,204,0));
g.drawString(label,2,20);
}
}
[2246 byte] By [
Phulip] at [2007-9-26 2:15:43]

Hello, Phulip.
How about the code below?
This is the application, not Applet. but it might answer your question.
The Applet extends Panel, and Panel has FlowLayout with default setting.
When you do nothing about Layout manager, the Location of your Component is ignored, I think.
If you want to fix the Location, you may set the Layoutmaneger for null.
You can set the Location by method "setBounds", but you have to set the Component size too.
in this case, you might be better to use Rectangle, instead of Dimension.
In addition, some other Layout Manager exist.(BorderLayout, GridLayout, GridBagLayout, and so on.)
the behavior of add methods are bit difference with each other.
excuse me for my english.
bye.
import java.awt.*;
import java.awt.event.*;
class ts010730b extends Frame
{
private WindowListener wl = new WindowAdapter() {
public void windowClosing(WindowEvent we) {
we.getWindow().dispose();
System.exit(0);
}
};
ts010730b() {
this.addWindowListener(wl);
Panel p = new Panel();// Panel is super of Applet.
//p.setLayout(new FlowLayout()); // Panel's default Layout, I see.
CustomButton newButton = new CustomButton("petrus",81,33);
newButton.setVisible(true);
/*
p.setLayout(new BorderLayout());
p.add(newButton, "South");
*/
/*
p.setLayout(new GridLayout());
p.add(newButton);
*/
///*
p.setLayout(null);
//int x, y; x=100; y=100;
//newButton.setBounds(0,0,x,y);
Dimension bsize = newButton.getPreferredSize();
newButton.setBounds(0,0,bsize.width, bsize.height);
p.add(newButton);
//*/
add(p);
this.setSize(100,100);
this.setVisible(true);
}
class CustomButton extends Canvas {
String label;
Dimension minSize;
int w, h;
public CustomButton(String label, int width, int height) {
this.label = label;
w = width;
h = height;
minSize = new Dimension(w,h);
setBackground(Color.black);
setLocation(0,0);
setCursor(new Cursor(Cursor.HAND_CURSOR));
setVisible(false);
}
public Dimension getPreferredSize() {
return getMinimumSize();
}
public synchronized Dimension getMinimumSize() {
return minSize;
}
public void paint (Graphics g) {
g.setColor(new Color(255,204,0));
g.drawLine(0, h-1, 0, 0);
g.drawLine(0, 0, w-1, 0);
g.setColor(new Color(204,153,0));
g.drawLine(w-1, 0, w-1, h-1);
g.drawLine(w-1, h-1, 0, h-1);
g.setColor(new Color(255,204,0));
g.drawString(label,2,20);
}
}
public static void main(String[] args)
{ new ts010730b(); }
}