我是新手,我不知道该做什么,我完全陷入困境!
我创建了亚美尼亚国旗,行很容易。
如果我想改变代码,使其处于水平状态(或者像意大利国旗的红色,白色和绿色),我将如何编码?
这就是我做亚美尼亚国旗的方式:
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;
}
}
}
}
}
答案 0 :(得分: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();
}
答案 1 :(得分:0)
我会使用继承。
创建一个类似这样的抽象Flag类。
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);
}
然后,您扩展此类,添加编码绘制方法所需的任何字段。
例如,这会画一个意大利国旗。
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);
}
}