为什么我的笑脸的GUI看起来是一种颜色而不是所有具有各自独立颜色的元素?

问题描述 投票:1回答:2

我试图在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);
    }
}
java swing graphics colors java-2d
2个回答
1
投票

这里:

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

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

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


0
投票

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

profile = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
...
© www.soinside.com 2019 - 2024. All rights reserved.