繁体   English   中英

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

[英]Why does my GUI of smiley faces appear to be one color instead of all elements having their own separate, random colors?

我试图在 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 种不同的颜色。 然后您的代码从每组中随机选择一种颜色。 意思是:您可以选择在混合时始终协同工作的颜色组,而不是使用完全随机的颜色。

欢迎来到 SO。 您将相同的Color对象分配给所有变量,因此它们自然具有相同的颜色。 您需要为它们中的每一个生成新的随机颜色,而不是为所有color分配color

profile = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM