imu*_*ion 9 java graphics applet drawing drawrectangle
我需要创建一个矩形对象,然后使用paint()将其绘制到applet.我试过了
Rectangle r = new Rectangle(arg,arg1,arg2,arg3);
Run Code Online (Sandbox Code Playgroud)
然后尝试使用将其绘制到applet
g.draw(r);
Run Code Online (Sandbox Code Playgroud)
它没用.有没有办法在java中这样做?我已经将谷歌搜索到其生命的一寸之内以获得答案,但我一直无法找到答案.请帮忙!
Zac*_*Zac 15
试试这个:
public void paint (Graphics g) {
Rectangle r = new Rectangle(xPos,yPos,width,height);
g.fillRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
}
Run Code Online (Sandbox Code Playgroud)
[编辑]
// 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()
);
}
Run Code Online (Sandbox Code Playgroud)
你可以尝试这样:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
其中x是x坐标y是y cordinate 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);
}
}
Run Code Online (Sandbox Code Playgroud)