Java 一个JFrame中的项目可以使用多种颜色吗?

Java 一个JFrame中的项目可以使用多种颜色吗?,java,swing,java.util.scanner,paintcomponent,Java,Swing,Java.util.scanner,Paintcomponent,我正在学习使用swing创建用户界面。目前,我有这个JFrame,我需要在它里面放置一个形状,并提供移动形状的方法。我称之为形状物体机器人 我想画一些比一个红方块更有创意的东西。我已经想出了如何添加多个形状,但它们仍然是相同的颜色。如何在单个JFrame上使用多种颜色 public class SwingBot { public static void main(String[] args) { JFrame frame = new JFrame(); fr

我正在学习使用swing创建用户界面。目前,我有这个JFrame,我需要在它里面放置一个形状,并提供移动形状的方法。我称之为形状物体机器人

我想画一些比一个红方块更有创意的东西。我已经想出了如何添加多个形状,但它们仍然是相同的颜色。如何在单个JFrame上使用多种颜色

public class SwingBot 
{
    public static void main(String[] args) 
    {
    JFrame frame = new JFrame();

    frame.setSize(400,400);
    frame.setTitle("SwingBot");

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Robot r = new Robot();

    frame.add(r);

    frame.setVisible(true);

    Scanner in = new Scanner(System.in);
    boolean repeat = true;
    System.out.println();
    while (repeat)
    {
        String str = in.next();
        String direc = str.toLowerCase();
        if (direc.equals("right"))
        {
            r.moveBot(10,0);
        }
        else if (direc.equals("left"))
        {
            r.moveBot(-10,0);
        }
        else if (direc.equals("up"))
        {
            r.moveBot(0,-10);
        }
        else if (direc.equals("down"))
        {
            r.moveBot(0,10);
        }
        else if (direc.equals("exit"))
        {
            repeat = false;
        }
    }

}


public static class Robot extends JComponent
{
    private Rectangle rect = new Rectangle(10,10);

    public void paintComponent(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;

        g2.setColor(Color.RED);

        g2.fill(rect);
    }

    public void moveBot(int x, int y)
    {
        rect.translate(x,y);

        repaint();
    }

}

}

您需要的是在每次绘制新组件时使用
图形对象的
颜色
调用
setColor
方法

public void paintComponent(Graphics g)
{
    Graphics2D g2 = (Graphics2D) g;

    g2.setColor(Color.RED);
    g2.fillRect(...);

    g2.setColor(Color.BLACK);
    g2.fillOval(...)
}

您需要的是在每次绘制新组件时使用
Graphics
对象的新
Color
调用
setColor
方法

public void paintComponent(Graphics g)
{
    Graphics2D g2 = (Graphics2D) g;

    g2.setColor(Color.RED);
    g2.fillRect(...);

    g2.setColor(Color.BLACK);
    g2.fillOval(...)
}
你可以致电:

g.setColor(your color here);
在使用其他颜色绘制形状之前

示例

@Override
protected void paintComponent(Graphics g){
    super.paintComponent(g);
    g.setColor(Color.RED);
    g.fillRect(0, 0, 100, 100);   //Fill a Red Rectangle

    g.setColor(Color.YELLOW);
    g.fillOval(20, 20, 50, 50);   //Fill a Yellow Circle
}
您可能还希望在paintComponent()方法中调用super.paintComponent(g)
,以防止出现视觉瑕疵。

您可以调用:

g.setColor(your color here);
在使用其他颜色绘制形状之前

示例

@Override
protected void paintComponent(Graphics g){
    super.paintComponent(g);
    g.setColor(Color.RED);
    g.fillRect(0, 0, 100, 100);   //Fill a Red Rectangle

    g.setColor(Color.YELLOW);
    g.fillOval(20, 20, 50, 50);   //Fill a Yellow Circle
}

您可能还想在paintComponent()方法中调用
super.paintComponent(g)
,以防止视觉瑕疵。

您可以为Robot类提供一个颜色属性,在调用moveBot之前可以更改该属性

比如:

public static class Robot extends JComponent{
    private Rectangle rect = new Rectangle(10,10);
    private Color col = Color.RED;

    public void paintComponent(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;

        g2.setColor(col);
        g2.fill(rect);
    }

    public void setColor(Color c){
       col = c;
    }

    public void moveBot(int x, int y)
    {
        rect.translate(x,y);

        repaint();
    }
}

您可以为Robot类提供一个颜色属性,在调用moveBot之前可以更改该属性

比如:

public static class Robot extends JComponent{
    private Rectangle rect = new Rectangle(10,10);
    private Color col = Color.RED;

    public void paintComponent(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;

        g2.setColor(col);
        g2.fill(rect);
    }

    public void setColor(Color c){
       col = c;
    }

    public void moveBot(int x, int y)
    {
        rect.translate(x,y);

        repaint();
    }
}

在绘制每个形状之前,您只需使用不同的颜色调用
setColor
。如果我向paintComponent方法添加另一个setColor,则该对象中的所有形状都将设置为序列中的最后一种颜色,而不管列出了其他颜色。我建议发布失败的代码,这样人们就可以看到您尝试的内容。在绘制每个形状之前,只需使用不同的颜色调用
setColor
。如果我向paintComponent方法添加另一个setColor,则该对象中的所有形状都将设置为序列中的最后一种颜色,而不管列出了其他任何颜色。我建议发布失败的代码,备注:由于您正在将图形向下转换为Graphics2D,因此同样适用。仍然可以调用
g2.setColor(您的颜色)
;备注:由于您将图形向下转换为Graphics2D,因此同样适用。仍然可以调用
g2.setColor(您的颜色)
;可以我做得很不正确。谢谢,好的。我做得很不正确。非常感谢。