java - 为什么我的笑脸 GUI 看起来是一种颜色,而不是所有元素都有自己单独的随机颜色?

标签 java swing graphics colors java-2d

我试图在 GUI 上绘制这些笑脸,并将它们的部分(眼睛、微笑等)全部设为随机颜色。但是,当我对变量使用 setColor 方法时,它们都相互混合,我看不到眼睛等。这是我的代码:

import java.awt.*;
import javax.swing.*;
import java.util.*;

public class Smiley extends JPanel {
    //declare required variable
    //set the width of window
    public static final int w = 400;
    //set the height of window
    public static final int h = 400;
    //assign face diameter
    public static final int fd = 180;
    //initializes the face of x position
    public static final int xp = 10;
    //initializes the face of y position
    public static final int yp = 10;
    //initializes the width of eye
    public static final int we = 20;
    //initializes the height of eye
    public static final int he = 20;
    //set the Right eye's position on the x and y
    public static final int xre = xp + 40;
    public static final int yre = yp + 60;
    //set the left eye's position on the x and y
    public static final int xle = xp + 120;
    public static final int yle = yp + 60;
    //initialzes the width and height of the mouth
    public static final int mw = 80;
    public static final int mh = 50;
    //initializes the x and y position of mouth on the face
    public static final int xm = xp + 50;
    public static final int ym = yp + 90;
    //define the class variables of type Color
    public Color profile, nface, fsmile, eye;

    // Smiley constructor takes parameters for 4 colors that will be
    public Smiley(Color profile, Color nface, Color fsmile, Color eye) {
        //set the layout
        setLayout(new BorderLayout());
        //initialize the parameters
        this.profile = profile;
        this.nface = nface;
        this.fsmile = fsmile;
        this.eye = eye;
        //call paint() method
        repaint();
    }
    public void paintComponent(Graphics gr) {
        super.paintComponent(gr);
        Random random = new Random();
        Color color = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
        gr.setColor(color);
        profile = color;
        nface = color;
        fsmile = color;
        eye = color;
        //set the color of profile of the face
        gr.setColor(profile);
        //draw the face
        gr.fillOval(xp, yp, fd + 7, fd + 7);
        //fill the color of face
        gr.setColor(nface);
        gr.fillOval(xp + 3, yp + 3, fd, fd);
        //fill the eye color
        gr.setColor(eye);
        //for draw right eye
        gr.fillOval(xre, yre, we, he);
        gr.setColor(eye);
        //for draw left eye
        gr.fillOval(xle, yle, we, he);
        //for smile color
        gr.setColor(fsmile);
        gr.drawArc(xm, ym, mw, mh, 180, 180);
    }
}

我期望的是这样的:

https://imgur.com/a/7hFGzw1

我将如何实现这一目标?

这是我将实现笑脸的代码:

import java.awt.*;
import java.awt.event.*; //determined to be useless and got rid of 
actionPerformed method
import javax.swing.*;
public class SmileyGrid extends JFrame {
    //object for SmileGrid class
    static SmileyGrid gs = new SmileyGrid();
    public static void main(String[] args) {
        gs.setSize(800, 800);
        gs.setLayout(new GridLayout(3, 3));
        //call createFace function()
        gs.createGUI();
        gs.setVisible(true);
    }

    public SmileyGrid() {

    }

    private void createGUI() {
        for (int a = 0; a < 9; a++) {
            Smiley sp = new Smiley(Color.BLUE, Color.YELLOW, Color.RED, Color.black);
            gs.add(sp);
        }
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}

最佳答案

这里:

profile = color;
nface = color;
fsmile = color;

这些应该是您的 3 种不同颜色。但是您将所有三个值初始化为相同值!相反,您必须调用随机函数 3 次。或者准确地说:您应该重复调用该函数,直到您的 3 种颜色确实不同为止。

或者:您可以预先手动定义 3 组,每组有 5 种、10 种不同的颜色。然后您的代码从每组中随机选择一种颜色。含义:您可以选择混合时始终协同工作的颜色组,而不是使用完全随机的颜色。

关于java - 为什么我的笑脸 GUI 看起来是一种颜色,而不是所有元素都有自己单独的随机颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55719425/

相关文章:

java - 嵌套类有静态变量吗?

java - JApplet 无法连接到互联网问题 - 如何使其受信任?

java - JFrame 不采用实际屏幕尺寸

java - Jlist 未在字符串中显示我的项目

c++ - Bresenham 线图针对特定坐标失败?

java - 如何使用KeyListener绘制不同的图像?

java - 无法处理部署的 POST_MODULE 阶段

java - 哪些 Java 模块获得了 Spring Security

Java和C#通过消息队列进行通信

matlab - 在 Matlab 中交换 x 和 y 轴