我试图在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://i.stack.imgur.com/8DVFC.jpg
我将如何实现这一目标?
这是我将实现笑脸的代码:
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
。
profile = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
...