我正在通过drawFace函数绘制脸部1次。我想在 Paint 方法中调用该函数,以便它将在我指定的 x/y 坐标处多次绘制脸部。
public class smilingfacefunction extends Canvas
{
public void paint(Graphics g)
{
drawFace(g, 500, 300);
drawFace(g, 100, 100);
}
public void drawFace (Graphics g, int x, int y)
{
g.setColor(Color.YELLOW);
g.fillOval(100, 100, 100, 100);
g.setColor(Color.BLUE);
g.fillOval(125, 125, 20, 20);
g.fillOval(155, 125, 20, 20);
g.setColor(Color.red);
g.drawArc(125,150,40,40,0,-180);
}
public static void main(String[] args)
{
JFrame win = new JFrame("Smile");
win.setSize(700, 700);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.add(new smilingfacefunction());
win.setVisible(true);
}
}
最佳答案
您的drawFace
方法必须使用x
和y
参数作为开始绘制的引用点。像这样的事情:
public void drawFace (Graphics g, int x, int y)
{
g.setColor(Color.YELLOW);
g.fillOval(x, y, 100, 100);
g.setColor(Color.BLUE);
g.fillOval(x + 25, y + 25, 20, 20);
g.fillOval(x + 55, y + 25, 20, 20);
g.setColor(Color.red);
g.drawArc(x + 25, y + 50, 40, 40, 0, -180);
}
您现在可以微调绘图,以便将每个面的元素放置在您想要的位置。
关于java - 如何使用方法绘制脸部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44987402/