Java 在不创建变量并为其指定随机数0或1的情况下,是否可以在JFrame的两种背景色之间进行选择?
我是java新手。我有一个猜谜游戏的家庭作业。用户输入猜测,JFrame上的JLabel显示猜测是否过高或过低,或者是否正确。输入猜测时,JFrame的背景色应更改为红色或蓝色。我知道如何将其更改为一种颜色,但是否有任何方法可以在红色或蓝色之间选择颜色,而无需使用math.random将0或1赋值给变量,然后使用if-else语句?谢谢。您的问题有点模糊,所以我不确定这是否符合您的要求,但是 你可以做的是设置一个混合过程,这样你会有“最坏”和“最好”的情况,例如,红色代表下方,蓝色代表上方,绿色代表现场。然后根据用户与您猜测的距离,您可以生成两种颜色的混合(更差-更好-更差,基于方向),例如Java 在不创建变量并为其指定随机数0或1的情况下,是否可以在JFrame的两种背景色之间进行选择?,java,swing,jframe,Java,Swing,Jframe,我是java新手。我有一个猜谜游戏的家庭作业。用户输入猜测,JFrame上的JLabel显示猜测是否过高或过低,或者是否正确。输入猜测时,JFrame的背景色应更改为红色或蓝色。我知道如何将其更改为一种颜色,但是否有任何方法可以在红色或蓝色之间选择颜色,而无需使用math.random将0或1赋值给变量,然后使用if-else语句?谢谢。您的问题有点模糊,所以我不确定这是否符合您的要求,但是 你可以做的是设置一个混合过程,这样你会有“最坏”和“最好”的情况,例如,红色代表下方,蓝色代表上方,绿色
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class ColorBlend {
public static void main(String[] args) {
new ColorBlend();
}
public ColorBlend() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(new JLabel("Enter a value between 1-100 (press enter)"), gbc);
JTextField field = new JTextField(4);
field.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Colors to be used
Color[] colors = new Color[]{Color.RED, Color.GREEN, Color.BLUE};
// The ratio at which the colors applied across the band...
float[] ratios = new float[]{0f, 0.5f, 1f};
String text = field.getText();
try {
int value = Integer.parseInt(text);
if (value >= 1 && value <= 100) {
float percentage = value / 100f;
// Get the color that best meets out needs
Color color = blendColors(ratios, colors, percentage);
setBackground(color);
} else {
field.setText("Out of range");
field.selectAll();
}
} catch (NumberFormatException exp) {
field.setText("Bad Value");
field.selectAll();
}
}
});
add(field, gbc);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 200);
}
}
public static Color blendColors(float[] fractions, Color[] colors, float progress) {
Color color = null;
if (fractions != null) {
if (colors != null) {
if (fractions.length == colors.length) {
int[] indicies = getFractionIndicies(fractions, progress);
float[] range = new float[]{fractions[indicies[0]], fractions[indicies[1]]};
Color[] colorRange = new Color[]{colors[indicies[0]], colors[indicies[1]]};
float max = range[1] - range[0];
float value = progress - range[0];
float weight = value / max;
color = blend(colorRange[0], colorRange[1], 1f - weight);
} else {
throw new IllegalArgumentException("Fractions and colours must have equal number of elements");
}
} else {
throw new IllegalArgumentException("Colours can't be null");
}
} else {
throw new IllegalArgumentException("Fractions can't be null");
}
return color;
}
public static int[] getFractionIndicies(float[] fractions, float progress) {
int[] range = new int[2];
int startPoint = 0;
while (startPoint < fractions.length && fractions[startPoint] <= progress) {
startPoint++;
}
if (startPoint >= fractions.length) {
startPoint = fractions.length - 1;
}
range[0] = startPoint - 1;
range[1] = startPoint;
return range;
}
public static Color blend(Color color1, Color color2, double ratio) {
float r = (float) ratio;
float ir = (float) 1.0 - r;
float rgb1[] = new float[3];
float rgb2[] = new float[3];
color1.getColorComponents(rgb1);
color2.getColorComponents(rgb2);
float red = rgb1[0] * r + rgb2[0] * ir;
float green = rgb1[1] * r + rgb2[1] * ir;
float blue = rgb1[2] * r + rgb2[2] * ir;
if (red < 0) {
red = 0;
} else if (red > 255) {
red = 255;
}
if (green < 0) {
green = 0;
} else if (green > 255) {
green = 255;
}
if (blue < 0) {
blue = 0;
} else if (blue > 255) {
blue = 255;
}
Color color = null;
try {
color = new Color(red, green, blue);
} catch (IllegalArgumentException exp) {
NumberFormat nf = NumberFormat.getNumberInstance();
System.out.println(nf.format(red) + "; " + nf.format(green) + "; " + nf.format(blue));
exp.printStackTrace();
}
return color;
}
}
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.EventQueue;
导入java.awt.GridBagConstraints;
导入java.awt.GridBagLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.text.NumberFormat;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.JTextField;
导入javax.swing.UIManager;
导入javax.swing.UnsupportedLookAndFeelException;
公共级颜色混合{
公共静态void main(字符串[]args){
新ColorBlend();
}
公共颜色混合(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
例如printStackTrace();
}
JFrame=新JFrame(“测试”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(newtestpane());
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公共类TestPane扩展了JPanel{
公共测试窗格(){
setLayout(新的GridBagLayout());
GridBagConstraints gbc=新的GridBagConstraints();
gbc.gridwidth=GridBagConstraints.rements;
添加(新JLabel(“输入1-100之间的值(按Enter键)”),gbc;
JTextField=新的JTextField(4);
field.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
//要使用的颜色
颜色[]颜色=新颜色[]{Color.RED,Color.GREEN,Color.BLUE};
//在整个频带上应用颜色的比率。。。
浮动[]比率=新浮动[]{0f,0.5f,1f};
String text=field.getText();
试一试{
int value=Integer.parseInt(文本);
如果(值>=1&&255){
红色=255;
}
如果(绿色<0){
绿色=0;
}否则如果(绿色>255){
绿色=255;
}
如果(蓝色<0){
蓝色=0;
}否则,如果(蓝色>255){
蓝色=255;
}
颜色=空;
试一试{
颜色=新颜色(红色、绿色、蓝色);
}捕获(IllegalArgumentException exp){
NumberFormat nf=NumberFormat.getNumberInstance();
System.out.println(nf.format(红色)+“;”+nf.format(绿色)+“;”+nf.format(蓝色));
exp.printStackTrace();
}
返回颜色;
}
}
取决于您希望颜色的随机性。您可以读取当前颜色并将其更改为另一种颜色,或执行输入响应%2
,或其他操作。这取决于您想要的颜色。哦,好的。如果让我们说JFrame显示红色或蓝色,而不考虑之前显示的颜色,是否有其他方法可以做到不要求用户提供颜色?如果您想要它randon,您必须使用Random
。否则,您必须使用在某个点上可以读取或推断的信息。可能您有一个带有numberofthries
的计数器,您可以使用它。如果猜得太高,可能有一种颜色,如果猜得太低,可能有一种颜色偶数,另一个假设猜测是奇怪的。这取决于你想要什么。哦,好的,哥们亚。非常感谢!:)没问题。希望你找到适合你的东西。