创建一个循环,绘制人三次,移动人dX日元?

时间:2011-11-23 06:52:36

标签: java

java代码。我对这个问题感到困惑

6。现在在你的paintComponent方法中放一个循环来吸引他三次,移动他(她?) 每次150个左右的像素(你决定了多远)

我是否需要在Jframe中创建扫描仪以询问用户他或她想要移动多少人,他们只能移动该人3次? 帮助

import javax.swing.*;
import java.awt.*;
import java.applet.Applet;
public class DrawPersonPanel extends JPanel {

private final int WIDTH = 600;
private final int HEIGHT = 400;
private int headX = 60;
private int headY = 40;
private int[] hairX = {62,75,84,85,88,90,93,99,104,110};
private int[] hairY = {45,46,37,38,39,30,31,32,33,54};
private int[] shirtX = {60,0,20,60,50,130,120,160,180,120};
private int[] shirtY = {100,150,180,160,250,250,160,180,150,100};
private int[] zigzagX = {70,75,80,85,90,95,100,105,110};
private int[] zigzagY = {135,140,135,140,135,140,135,140,135};
private int[] pantsX = {50,130,150,110,90,70,30};
private int[] pantsY = {250,250,375,375,300,375,375};


//--------------------------------------
// Constructor: Set up the panel.
//--------------------------------------
public DrawPersonPanel()
{
setPreferredSize(new Dimension(WIDTH, HEIGHT));
}
//--------------------------------------
// Draw person
//--------------------------------------
public void paintComponent (Graphics page)
{
page.setColor(Color.blue);
page.fillPolygon(shirtX, shirtY, shirtX.length);

page.setColor(new Color(255, 228, 181));
page.fillOval(headX, headY, 60, 60 - 10);


page.setColor(Color.BLACK);
page.fillPolygon(hairX, hairY, hairX.length);


page.setColor(Color.WHITE);
page.drawPolyline(zigzagX, zigzagY, zigzagX.length);


page.setColor(Color.cyan);
page.fillPolygon(pantsX, pantsY, pantsX.length);



}

private void movePerson(int x, int y){
// Increment head.
        headX += x;
        headY += y;

        for (int i = 0; i < hairX.length; i++) 
        {hairX[i] += x;}

        for (int i = 0; i < hairY.length; i++) 
        {hairY[i] += y;}

        // Increment shirt.
         for (int i = 0; i < shirtX.length; i++) 
        {shirtX[i] += x;}

        for (int i = 0; i < shirtY.length; i++) 
        {shirtY[i] += y;}

        // Increment zig-zag on shirt.
        for (int i = 0; i < zigzagX.length; i++) 
        {zigzagX[i] += x;}

        for (int i = 0; i < zigzagY.length; i++) 
        {zigzagY[i] += y;}

        // Increment pants.
        for (int i = 0; i < pantsX.length; i++) 
        {pantsX[i] += x;}

        for (int i = 0; i < pantsY.length; i++) 
        {pantsY[i] += y;}


        repaint();

}



}

2 个答案:

答案 0 :(得分:0)

问题在于创建一个循环,以固定的数量调用函数movePerson 3次。他们只是说它可以是你选择的任意数量(事先,在编码时)。

答案 1 :(得分:0)

创建一个人,他们的左右位置取决于变量,比如X,然后创建一个for循环,每次将X增加预设量。

像这样:

for(int i = 0; i < 450; i+= 150){
    drawPerson(i, yValue);
}

其中drawPerson是一个在(i,yValue)处绘制人物的函数。这将吸引三个人,每人相隔150人。

您可以通过调整增量来改变它们之间的距离。

玩它直到它起作用。就像马克扎克伯格所说的那样,“快速行动并打破局面”