700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Java背景颜色怎么设成随机的_java - 如何将按钮的背景设置为具有设置约束的随机颜色

Java背景颜色怎么设成随机的_java - 如何将按钮的背景设置为具有设置约束的随机颜色

时间:2018-10-17 10:26:58

相关推荐

Java背景颜色怎么设成随机的_java - 如何将按钮的背景设置为具有设置约束的随机颜色

这是错的:

for (int i = 0; i < arrayButtons.length; i++) {

arrayButtons[i].setDrawColor(new Color(rand.nextInt()));

}

您需要创建一半随机颜色和颜色2按钮,然后随机播放阵列。我会使用一个ArrayList - 更容易随机播放和添加内容。所以假设一个名为buttonList的List:

for (int i = 0; i < buttonList.size() / 2; i++) {

Color color = new Color(rand.nextInt();

buttonList.get(2 * i).setDrawColor(color));

buttonList.get(2 * i + 1).setDrawColor(color));

}

Collections.shuffle(buttonList);

或者如果你必须使用数组,并将洗牌阵列:

for (int i = 0; i < arrayButtons.length / 2; i++) {

Color color = new Color(rand.nextInt();

arrayButtons[2 * i].setDrawColor(color));

arrayButtons[2 * i + 1].setDrawColor(color));

}

// shuffle your array here

编辑:我改变了主意。最简单的创建一个List,填充8对不同的颜色,随机播放,然后将颜色添加到按钮。而不是随机颜色,如果你想让它们全部变亮,使用Color的.getHSBColor(...)方法使用8种不同的颜色色调:

List colorList = new ArrayList<>();

for (int i = 0; i < 8; i++) {

float hue = i * 1f / 8;

Color c = Color.getHSBColor(hue, 1f, 1f);

colorList.add(c);

colorList.add(c);

}

Collections.shuffle(colorList);

// add colors to buttons in for loop

要么...

colorList = new ArrayList<>();

for (int i = 0; i < ROWS * COLS / 2; i++) {

float r = (float) Math.random();

float g = (float) Math.random();

float b = (float) Math.random();

Color color = new Color(r, g, b);

colorList.add(color);

colorList.add(color);

}

Collections.shuffle(colorList);

// add colors to buttons using for loop

我自己,我会做一些不同的事情:

我不会扩展JButton

我使用模型 - 视图 - 控制器程序结构的变体将我的模型从我的视图中分离出来

所有逻辑都将保存在模型中,包括随机排列在ArrayList中的8个数字对

例如:

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

import javax.swing.*;

@SuppressWarnings("serial")

public class ColorTest extends JPanel {

private static final int ROWS = 4;

private static final int COLS = 4;

private static final int BTN_W = 100;

private static final int BTN_H = 100;

private static final Dimension BTN_SIZE = new Dimension(100, 100);

// how long to show mismatched color pairs

private static final int COLOR_DISPLAY_DELAY = 2000; // msecs

private static final int NO_PICK = -1;

private List buttonList = new ArrayList<>();

private List colorList = new ArrayList<>();

private int firstPick = NO_PICK;

private int secondPick = NO_PICK;

private ButtonListener buttonListener = new ButtonListener();

private boolean listenerEnabled = true;

public ColorTest() {

JPanel gridPanel = new JPanel(new GridLayout(ROWS, COLS));

for (int index = 0; index < ROWS * COLS; index++) {

ColorButton2 button = new ColorButton2(index, BTN_W, BTN_H);

button.addActionListener(buttonListener);

button.setPreferredSize(BTN_SIZE);

buttonList.add(button);

gridPanel.add(button);

}

reset();

JButton resetButton = new JButton("Reset");

resetButton.addActionListener(e -> reset());

resetButton.setMnemonic(KeyEvent.VK_R);

JButton exitButton = new JButton("Exit");

exitButton.setMnemonic(KeyEvent.VK_X);

exitButton.addActionListener(e -> System.exit(0));

JPanel btnPanel = new JPanel(new GridLayout(1, 0));

btnPanel.add(resetButton);

btnPanel.add(exitButton);

setLayout(new BorderLayout());

add(gridPanel);

add(btnPanel, BorderLayout.PAGE_END);

}

public void reset() {

colorList = new ArrayList<>();

for (int i = 0; i < ROWS * COLS / 2; i++) {

// Color color = randomRgbColor();

float hue = (2f * i) / (ROWS * COLS); // non-random hue

Color color = randomHsbColor(hue); // randomize the saturation and

// brilliance

colorList.add(color);

colorList.add(color);

}

Collections.shuffle(colorList);

for (int i = 0; i < buttonList.size(); i++) {

ColorButton2 btn = buttonList.get(i);

btn.reset();

btn.setColor(colorList.get(i));

}

}

@SuppressWarnings("unused")

private Color randomRgbColor() {

float r = (float) Math.random();

float g = (float) Math.random();

float b = (float) Math.random();

Color color = new Color(r, g, b);

return color;

}

private Color randomHsbColor(float hue) {

// float hue = (float) Math.random();

float sat = ((int) (2 * Math.random()) + 1) / 2f;

float bril = ((int) (2 * Math.random()) + 1) / 2f;

return Color.getHSBColor(hue, sat, bril);

}

private class ButtonListener implements ActionListener {

@Override

public void actionPerformed(ActionEvent e) {

if (!listenerEnabled) {

// if button listener disabled -- get out of here

return;

}

ColorButton2 source = (ColorButton2) e.getSource();

if (source.getNumber() == firstPick) {

// if user chooses same button twice, a mistake, so exit

return;

}

if (source.isPairMatched()) {

// button already matched

return;

}

source.flip(true); // show button's real color

// if user has selected first button

if (firstPick == NO_PICK) {

firstPick = source.getNumber(); // set first button pick field

} else {

// else user has selected 2nd button

secondPick = source.getNumber();

// check if the two buttons hold the same color

Color c1 = buttonList.get(firstPick).getColor();

Color c2 = buttonList.get(secondPick).getColor();

if (c1.equals(c2)) {

// matching picks, then disable both buttons

buttonList.get(firstPick).setPairMatched(true);

buttonList.get(secondPick).setPairMatched(true);

// reset these variables

firstPick = NO_PICK;

secondPick = NO_PICK;

// check that the game isn't over

checkForWin();

} else {

// start timer to display buttons for period of time, then

// flip back

listenerEnabled = false; // disable all button's action

// listeners

new Timer(COLOR_DISPLAY_DELAY, e2 -> {

// show background color for both buttons

buttonList.get(firstPick).flip(false);

buttonList.get(secondPick).flip(false);

// re-enable the action listener

listenerEnabled = true;

firstPick = NO_PICK;

secondPick = NO_PICK;

((Timer) e2.getSource()).stop(); // non-repeating timer

}).start();

}

}

}

}

public void checkForWin() {

boolean win = true;

for (ColorButton2 button : buttonList) {

win &= button.isPairMatched();

}

if (win) {

String message = "You've matched all the colors";

String title = "Congratulations!";

int messageType = JOptionPane.INFORMATION_MESSAGE;

JOptionPane.showMessageDialog(ColorTest.this, message, title, messageType);

}

}

private static void createAndShowGui() {

ColorTest mainPanel = new ColorTest();

JFrame frame = new JFrame("ColorTest");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().add(mainPanel);

frame.pack();

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

public static void main(String[] args) {

SwingUtilities.invokeLater(() -> createAndShowGui());

}

}

@SuppressWarnings("serial")

class ColorButton2 extends JButton {

// background color

private static final Color BG_COLOR = Color.LIGHT_GRAY;

private int number;

// non-background color

private Color color = null;

// true if a pair has been found for this button

private boolean pairMatched = false;

public ColorButton2(int number, int width, int height) {

this.number = number;

setPreferredSize(new Dimension(width, height));

setBackground(BG_COLOR);

}

// reset back to initial conditions

public void reset() {

setPairMatched(false);

setBackground(BG_COLOR);

}

// if true -- show real color, else show background color

public void flip(boolean flip) {

if (flip) {

setBackground(color);

} else {

setBackground(BG_COLOR);

}

}

public Color getColor() {

return color;

}

public void setColor(Color color) {

this.color = color;

}

public void setNumber(int number) {

this.number = number;

}

public int getNumber() {

return number;

}

public boolean isPairMatched() {

return pairMatched;

}

// if match is found, disable the button and set the field

public void setPairMatched(boolean pairMatched) {

setEnabled(!pairMatched);

this.pairMatched = pairMatched;

}

}

Java背景颜色怎么设成随机的_java - 如何将按钮的背景设置为具有设置约束的随机颜色? - SO中文参考 - ...

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。