我需要创建一个矩形对象,然后使用paint() 将其绘制到小程序上。我试过
Rectangle r = new Rectangle(arg,arg1,arg2,arg3);
然后尝试使用将其绘制到小程序
g.draw(r);
它没有用。有没有办法在java中做到这一点?我已经在谷歌的生命中寻找答案,但我一直无法找到答案。请帮忙!
我需要创建一个矩形对象,然后使用paint() 将其绘制到小程序上。我试过
Rectangle r = new Rectangle(arg,arg1,arg2,arg3);
然后尝试使用将其绘制到小程序
g.draw(r);
它没有用。有没有办法在java中做到这一点?我已经在谷歌的生命中寻找答案,但我一直无法找到答案。请帮忙!
试试这个:
public void paint (Graphics g) {
Rectangle r = new Rectangle(xPos,yPos,width,height);
g.fillRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
}
[编辑]
// With explicit casting
public void paint (Graphics g) {
Rectangle r = new Rectangle(xPos, yPos, width, height);
g.fillRect(
(int)r.getX(),
(int)r.getY(),
(int)r.getWidth(),
(int)r.getHeight()
);
}
你可以这样尝试:
import java.applet.Applet;
import java.awt.*;
public class Rect1 extends Applet {
public void paint (Graphics g) {
g.drawRect (x, y, width, height); //can use either of the two//
g.fillRect (x, y, width, height);
g.setColor(color);
}
}
其中 x 是 x 坐标 y 是 y 坐标 color=您要使用的颜色,例如 Color.blue
如果你想使用矩形对象,你可以这样做:
import java.applet.Applet;
import java.awt.*;
public class Rect1 extends Applet {
public void paint (Graphics g) {
Rectangle r = new Rectangle(arg,arg1,arg2,arg3);
g.fillRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
g.setColor(color);
}
}