修改一些java applet代码

时间:2014-05-05 23:54:41

标签: java graphics

我在下面有以下applet代码,我想修改它,以便每行中的第一个和最后一个点为红色。我真的很难开始这个。

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class ChineseCheckerboardApplet extends Applet {
    public void init() {
        add(new ChineseCheckerboard(this));
    }
}

class ChineseCheckerboard extends Canvas {
    public ChineseCheckerboard(Applet parentApplet) {
        setSize(400, 400);
    }


    public void paint(Graphics g) {
        for (int i = 0; i < holesInRow.length; i++)
            drawRow(g, i);
    }


    void drawRow(Graphics g, int row) {
    int centerX = getWidth() / 2;           // figure out center of display
    int startY = 0;
    int numHoles = holesInRow[row];
    int rowWidth = numHoles * holeDiam + (numHoles-1) * holeGap;    //row width is        number of holes + appropriate number of spacings
    int startX = centerX - rowWidth / 2;            // Starting horizontal position is centerX less half the row width
    int y = startY + row * (holeDiam + holeGap);    // Vertically position based upon which row you're displaying
    g.setColor(Color.BLACK);
    for (int i = 0; i < numHoles; i++) {            // Draw each hole
        g.fillOval(startX + i * (holeDiam + holeGap), y, holeDiam, holeDiam);
        g.drawOval(startX + i * (holeDiam + holeGap), y, holeDiam, holeDiam);
    }
}

final int holeDiam = 15;
final int holeGap = (int)(holeDiam * 0.25);

// Here's the array containing the number of holes per row

int [] holesInRow = {1, 2, 3, 4, 13, 12, 11, 10, 9, 10, 11, 12, 13, 4, 3, 2, 1};

}

1 个答案:

答案 0 :(得分:3)

你的意思是......

Star

只需要一个简单的if语句来改变行上每个点的迭代点的条件......

for (int i = 0; i < numHoles; i++) {            // Draw each hole
    if (i == 0 || i == numHoles - 1) {
        g.setColor(Color.RED);
    } else {
        g.setColor(Color.BLACK);
    }
    g.fillOval(startX + i * (holeDiam + holeGap), y, holeDiam, holeDiam);
    g.drawOval(startX + i * (holeDiam + holeGap), y, holeDiam, holeDiam);
}