【问题标题】:I want to pick a color from a large set of colors我想从一大堆颜色中挑选一个颜色
【发布时间】:2014-03-13 00:26:31
【问题描述】:

在这里我创建了一个程序。我的主要方法是创建许多我想要的形状(框、三角形...)

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

这是主程序(只有方框):

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);
        }
    }

}

这是盒子的另一个类(连接到主程序绘制我的盒子)

import java.awt.*;

public class Box {
    protected Color color;
    protected int x, y; // pixels from upper left to upper left corner

    // make a box
    public Box(int x1, int y1, Color c1) {
        x = x1;
        y = y1;
        color = c1;
    }

    public void drawMe(Graphics g) {
        g.setColor(color);
        g.fillRect(x, y, 20, 20);
    }

    public void setColor(Color c) {
        color = c;
    }

    public Color getColor() {
        return color;
    }
}

这是另一个类

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 );
}

【问题讨论】:

  • "我想要一个按钮,如果我点击它,我可以从大量颜色中选择一种颜色?"问号是否表明您不确定自己想要什么? :)
  • 请提供可编译的代码!
  • 好的,不知道如何说清楚。但我想从一大堆颜色中挑选一种颜色
  • 上述代码中的哪些部分实际上与您的问题相关?为了做你想做的事,你做了什么尝试?
  • 所以您正在寻找制作颜色选择器的方法?谷歌是一个很好的起点。如果我正确理解您的问题,您正在寻找所谓的ColorPicker

标签: java swing


【解决方案1】:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-24
    • 2020-03-20
    • 1970-01-01
    • 2014-06-03
    • 1970-01-01
    • 2021-04-26
    • 2021-05-20
    相关资源
    最近更新 更多