0

我是新手,我不知道该怎么做,我完全被困住了!

我已经创建了亚美尼亚国旗,而且行很容易。

如果我想将代码更改为水平的(或类似于带有红色、白色和绿色的意大利国旗),我将如何对其进行编码?

这就是我做亚美尼亚国旗的方式:

 import java.awt.Color;

 /**
 * A program to draw an Armenian flag.
 * 
 * @author O
 * @version 5
 */
 public class ArmenianFlag
 {
 public static void main(String[] args)
 {
     // the flag has three bands of colour
    // and the aspect ratio is 1:2 (it's twice as wide as it is high)
    int WIDTH = 6;
    int HEIGHT = 3;
    int CELL_SIZE= 60;

    // Mix up the right colours
    Color RED = Color.RED;
    Color BLUE = new Color(0, 0, 170);
    Color ORANGE = new Color(255, 153, 0);

    // Create the window to display in
    FlagFrame frame = new FlagFrame("Armenia", HEIGHT, WIDTH, CELL_SIZE);

    // OK - now we are ready to paint the colours in the right places
    for(int row = 0; row < HEIGHT; row++)
    {
        for(int col = 0; col < WIDTH; col++)
        {
            switch(row)
            {
                case 0:
                    frame.selectColor(row, col, RED);
                    break;
                case 1:
                    frame.selectColor(row, col, BLUE);
                    break;
                case 2:
                    frame.selectColor(row, col, ORANGE);
                    break;
            }
        }
      }
    }
   }
4

2 回答 2

0

如果我是你,我会稍微改变你的结构,所以你的类看起来像这样:

public class ArmenianFlag
{
    public FlagFrame getFlag()
    {
        // ... Do logic.
        return frame;
    }
}

然后您可以创建另一个标志,如下所示:

public class ItalianFlag
{
    // Once again, so logic. Bare in mind this time, you want three columns and 1 row.
    int width = 3;
    int height = 3;

   public FlagFrame getFlag() {

        for(int col = 0; col < width; col ++)
        {
            // Cycle through each column.
            for(int row = 0; row < height; row ++)
            {
                // For eaach row in the column, set the appropriate color.

                // Notice the important part is you're changing the color of each column!!
            }
        }
    }
}

当你在那里时,你还不如创建一个超类。

public abstract class Flag
{
      public FlagFrame getFlag();
}

然后你的类的标题现在看起来像这样:

public class ItalianFlag extends Flag

public class ArmenianFlag extends Flag

您可以将常见的细节(例如高度和宽度)移到Flag班级..

public abstract class Flag
{
      private int height;
      private int width;

      public FlagFrame getFlag();
}
于 2013-08-29T15:09:56.160 回答
0

我会使用继承。

创建一个类似这样的抽象标志类。

package com.ggl.flag.model;

import java.awt.Dimension;
import java.awt.Graphics;

public abstract class Flag {

    protected Dimension dimension;

    protected String name;

    public Flag(String name) {
        this.name = name;
    }

    public void setDimension(Dimension dimension) {
        this.dimension = dimension;
    }

    public void setDimension(int width, int height) {
        this.dimension = new Dimension(width, height);
    }

    public Dimension getDimension() {
        return dimension;
    }

    public String getName() {
        return name;
    }

    public abstract void draw(Graphics g);

}

然后,你扩展这个类,添加你需要的任何字段来编写 draw 方法。

例如,这将绘制一面意大利国旗。

package com.ggl.flag.model;

import java.awt.Color;
import java.awt.Graphics;

public class ItalianFlag extends Flag {

    private Color   green;
    private Color   white;
    private Color   red;

    public ItalianFlag() {
        super("Italian");
        // Use new Color(r, g, b) for a more precise color match
        this.green = Color.GREEN;
        this.white = Color.WHITE;
        this.red = Color.RED;
    }

    @Override
    public void draw(Graphics g) {
        // Flag dimensions are 3 x 2
        int colorWidth = dimension.width / 3;
        g.setColor(green);
        g.fillRect(0, 0, colorWidth, dimension.height);
        g.setColor(white);
        g.fillRect(colorWidth, 0, colorWidth + colorWidth, dimension.height);
        g.setColor(red);
        g.fillRect(colorWidth + colorWidth, 0, colorWidth + colorWidth
                + colorWidth, dimension.height);
    }

}

您的 DrawingPanel 类将扩展 JPanel 并绘制您作为构造函数参数传递的任何标志。

package com.ggl.flag.view;

import java.awt.Graphics;

import javax.swing.JPanel;

import com.ggl.flag.model.Flag;

public class DrawingPanel extends JPanel {
    private static final long   serialVersionUID    = -1489106965551164891L;

    private Flag flag;

    public DrawingPanel(Flag flag) {
        this.flag = flag;
        this.setPreferredSize(flag.getDimension());
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        flag.draw(g);
    }

}
于 2013-08-29T15:11:20.907 回答