Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我想从一大堆颜色中挑选一种颜色_Java_Swing - Fatal编程技术网

Java 我想从一大堆颜色中挑选一种颜色

Java 我想从一大堆颜色中挑选一种颜色,java,swing,Java,Swing,在这里,我创建了一个程序。我的主要方法是创建许多形状、长方体、三角形。。。我想 有一个按钮,如果我点击它,我可以从一大组颜色中选择一种颜色 这是仅包含以下框的主程序: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Kaleidescope extends JFrame implements MouseListener, ActionListener, MouseMotionListe

在这里,我创建了一个程序。我的主要方法是创建许多形状、长方体、三角形。。。我想

有一个按钮,如果我点击它,我可以从一大组颜色中选择一种颜色

这是仅包含以下框的主程序:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Kaleidescope extends JFrame implements MouseListener, ActionListener, MouseMotionListener {

    Box b;
    Box[] boxes; // 2-d array of Box objects, form a color pallet
    int boxCount;
    JButton boxButton;

    int x1, y1; // mousePressed
    int w1, z1; // mouseEntered

    int mode = 1; // 1 = line, 2= boxes, 3 = oval, 4= text, 5 = SG, twoLines = 7. 

    public static void main(String[] args) {
        System.out.println("hi there.");
        new Kaleidescope();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        addMouseListener(this);
        addMouseMotionListener(this);
        boxes = new Box[20];
        boxCount = 0;

        setLayout(new FlowLayout());
        boxButton = new JButton("Boxes");
        add(boxButton);

        boxButton.addActionListener(this);
        setSize(new Dimension(500, 500));
        setVisible(true);

    }

    // returns a random color

    public Color randomColor() {
        int red = (int) (Math.random() * 255);
        int green = (int) (Math.random() * 255);
        int blue = (int) (Math.random() * 255);

        return new Color(red, green, blue);
    }

    public void mouseClicked(MouseEvent e) {

        System.out.println("click at x=" + e.getX() + " y=" + e.getY());

        // convert window coords to box array indexes.

        // These were adjusted slightly after the video was made

        // (no more flakiness, these are right on target).

        int boxi = (e.getX() - 10) / 20; // convert mouse x to box index
        int boxj = (e.getY() - 40) / 20;

        System.out.println("click at boxi=" + boxi + " boxj=" + boxj);

        // set extra box to the color that we clicked on

        if (mode == 2) {
            boxes[boxCount++] = new Box(e.getX(), e.getY(), randomColor());
        }
        repaint();
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == boxButton) {
            mode = 2;
            repaint();
        }
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void paint(Graphics g) {

        super.paint(g);
        for (int i = 0; i < boxCount; i++) {
            boxes[i].drawMe(g);
        }
    }

}
这是另一节课

package kaleidescope;

public class Point {

    int x;
    int y;

    public Point( int x1, int y1 )    {
        x = x1; y = y1;
    }

}
还有这个班

package kaleidescope;

import java.awt.*;

public abstract class Shape {
    protected Color color;
    abstract public void drawMe( Graphics g );
}

我想要一个按钮,如果我点击它,我可以从一大组颜色中选择一种颜色?问号是否表示您不确定自己想要什么请提供可编译的代码!好的,我不知道怎么说清楚。但是我想从一大组颜色中挑选一种颜色,上面代码中的一些实际上与您的问题有关?为了做你想做的事,你尝试了什么?所以你在寻找一种制作颜色选择器的方法?谷歌是一个很好的起点。如果我没弄错你的问题,你是在找一个叫做颜色选择器的东西
package kaleidescope;

import java.awt.*;

public abstract class Shape {
    protected Color color;
    abstract public void drawMe( Graphics g );
}