gpt4 book ai didi

java - 仅 RGB JColorChooser (Java 7)

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:50:54 24 4
gpt4 key购买 nike

我正在尝试创建一个 JFrame包括 JPanel仅包含选择 RGB 颜色所需的内容。我一直在摆弄JColorChooser , AbstractColorChooserPanel和 ColorModel,阅读 Oracle 教程,但我不明白如何开发我想要的东西。我什至下载了 OpenJDK 源代码来获取这些类的源代码,但仍然一无所获。我想要得到的是:

What I want

Alpha 内容应该消失并且颜色代码字段应该设置为不可见但继续工作以便我可以在单击"is"按钮时检索代码(我猜是在 actionPerformed 方法中)。重写 paintComponent 方法也是一个很好的补充。

提前致谢。

编辑:这就是我得到我目前拥有的东西的方式(上面的照片,没有'Paint'-edits):

for (final AbstractColorChooserPanel accp : panels) {
if (accp.getDisplayName().equals("RGB")) {
JOptionPane.showOptionDialog(Main.frame, accp,
"Color selection tool", JOptionPane.OK_OPTION,
JOptionPane.QUESTION_MESSAGE, null, null, 0);
}
}

EDIT2:到目前为止,我现在已经能够删除 alpha 内容,但我还不能“找到”显示颜色代码的标签和字段,所以它们不断显示,此外,由于我无法访问该字段,因此我无法访问颜色代码:

achieved

这是这段代码:

package edu.utils;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Array;
import java.lang.reflect.Field;

import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JSpinner;
import javax.swing.WindowConstants;
import javax.swing.colorchooser.AbstractColorChooserPanel;

import edu.io.local.Log;

public final class RGBColorChooserPanel extends JDialog implements ActionListener {

private final JColorChooser jCC;
private final JPanel panel;
private String colorCode;

public RGBColorChooserPanel(final String title) {
super(edu.Main.frame);
this.setTitle(title);
this.jCC = new JColorChooser();
this.modifyJColorChooser();
this.panel = new JPanel() {
@Override
protected void paintComponent(final Graphics g) {
final Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);

final GradientPaint gp = new GradientPaint(0, 0, Color.BLUE, 0, this.getHeight(),
Color.BLACK);

g2d.setPaint(gp);
g2d.fillRect(0, 0, this.getWidth(), this.getHeight());

super.paintComponent(g);
}
};
this.panel.add(this.jCC);
this.panel.setOpaque(false);
this.jCC.setOpaque(false);
this.jCC.setPreviewPanel(new JPanel());
this.jCC.setColor(120, 20, 57);
this.add(this.panel, BorderLayout.CENTER);
this.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
this.pack();

final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((screenSize.width - this.getWidth()) / 2,
(screenSize.height - this.getHeight()) / 2);
this.setResizable(false);
this.setVisible(true);
}

@Override
public void actionPerformed(final ActionEvent e) {
// TODO Auto-generated method stub

}

private void modifyJColorChooser() {
final AbstractColorChooserPanel[] panels = this.jCC.getChooserPanels();
for (final AbstractColorChooserPanel accp : panels) {
if (!accp.getDisplayName().equals("RGB")) {
this.jCC.removeChooserPanel(accp);
}
}

final AbstractColorChooserPanel[] colorPanels = this.jCC.getChooserPanels();
final AbstractColorChooserPanel cp = colorPanels[0];

Field f = null;
try {
f = cp.getClass().getDeclaredField("panel");
} catch (NoSuchFieldException | SecurityException e) {
Log.log(e);
}
f.setAccessible(true);

Object colorPanel = null;
try {
colorPanel = f.get(cp);
} catch (IllegalArgumentException | IllegalAccessException e) {
Log.log(e);
}

Field f2 = null;
try {
f2 = colorPanel.getClass().getDeclaredField("spinners");
} catch (NoSuchFieldException | SecurityException e4) {
Log.log(e4);
}
f2.setAccessible(true);
Object rows = null;
try {
rows = f2.get(colorPanel);
} catch (IllegalArgumentException | IllegalAccessException e3) {
Log.log(e3);
}

final Object transpSlispinner = Array.get(rows, 3);
Field f3 = null;
try {
f3 = transpSlispinner.getClass().getDeclaredField("slider");
} catch (NoSuchFieldException | SecurityException e) {
Log.log(e);
}
f3.setAccessible(true);
JSlider slider = null;
try {
slider = (JSlider) f3.get(transpSlispinner);
} catch (IllegalArgumentException | IllegalAccessException e2) {
Log.log(e2);
}
slider.setVisible(false);
Field f4 = null;
try {
f4 = transpSlispinner.getClass().getDeclaredField("spinner");
} catch (NoSuchFieldException | SecurityException e1) {
Log.log(e1);
}
f4.setAccessible(true);
JSpinner spinner = null;
try {
spinner = (JSpinner) f4.get(transpSlispinner);
} catch (IllegalArgumentException | IllegalAccessException e) {
Log.log(e);
}
spinner.setVisible(false);
Field f5 = null;
try {
f5 = transpSlispinner.getClass().getDeclaredField("label");
} catch (NoSuchFieldException | SecurityException e1) {
Log.log(e1);
}
f5.setAccessible(true);
JLabel label = null;
try {
label = (JLabel) f5.get(transpSlispinner);
} catch (IllegalArgumentException | IllegalAccessException e) {
Log.log(e);
}
label.setVisible(false);

Field f6 = null;
try {
f6 = transpSlispinner.getClass().getDeclaredField("value");
} catch (NoSuchFieldException | SecurityException e1) {
Log.log(e1);
}
f6.setAccessible(true);
float value = 0;
try {
value = (float) f6.get(transpSlispinner);
} catch (IllegalArgumentException | IllegalAccessException e) {
Log.log(e);
}
}
}

PS:我知道异常处理的怪异之处,但我需要管理每个语句的异常,所以请不要提示。

最佳答案

您最好的选择是 Creating a Custom Chooser Panel通过扩展 AbstractColorChooserPanel。教程example ,虽然很初级,但却是安装和使用的有用指南。草图的完整实现超出了 SO 的范围;但在许多现存的examples , this onethis one似乎值得仔细研究。这个相关Q&A也可以提供一些见解。

关于java - 仅 RGB JColorChooser (Java 7),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15227644/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com