Java 创建彩色标志-重复

Java 创建彩色标志-重复,java,Java,我是新来的,我不知道该怎么办,我完全被卡住了 我创造了亚美尼亚国旗,这很容易与行 如果我想改变代码,使它是水平的(或者像意大利国旗一样有红色、白色和绿色),我会如何编码 我是这样做的亚美尼亚国旗: import java.awt.Color; /** * A program to draw an Armenian flag. * * @author O * @version 5 */ public class ArmenianFlag { public static vo

我是新来的,我不知道该怎么办,我完全被卡住了

我创造了亚美尼亚国旗,这很容易与行

如果我想改变代码,使它是水平的(或者像意大利国旗一样有红色、白色和绿色),我会如何编码

我是这样做的亚美尼亚国旗:

 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;
            }
        }
      }
    }
   }
导入java.awt.Color;
/**
*绘制亚美尼亚国旗的程序。
* 
*@作者O
*@version 5
*/
公共类亚美尼亚语
{
公共静态void main(字符串[]args)
{
//国旗有三条色带
//长宽比为1:2(宽是高的两倍)
整数宽度=6;
整数高度=3;
int单元大小=60;
//混合正确的颜色
Color RED=Color.RED;
蓝色=新颜色(0,0,170);
橙色=新颜色(255,153,0);
//创建要在中显示的窗口
FlagFrame=新的FlagFrame(“亚美尼亚”,高度、宽度、单元格大小);
//好的-现在我们准备在正确的位置绘制颜色
对于(int row=0;row
如果我是你,我会稍微改变一下你的结构,所以你的类看起来像这样:

public class ArmenianFlag
{
    public FlagFrame getFlag()
    {
        // ... Do logic.
        return frame;
    }
}
public class ItalianFlag extends Flag
然后可以创建另一个标志,如下所示:

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 class ArmenianFlag
{
    public FlagFrame getFlag()
    {
        // ... Do logic.
        return frame;
    }
}
public class ItalianFlag extends Flag

您可以将常见的细节(如高度和宽度)移到
标志
类中

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

      public FlagFrame getFlag();
}

我会使用继承

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

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);
    }

}

很难说,您正在使用一些API/lib,但没有告诉我们是哪个。您可以将
开关(行)
更改为
开关(列%2)
,然后按此顺序将颜色更改为绿色、白色和红色。而且,首先不使用循环,然后将其更改为使用循环可能更容易;蓝色=新颜色(0,51,160);橙色=新颜色(242、168、0)