我有这个类给我颜色对象的RGB值。
import java.awt.*;
import java.util.Collection;
import java.util.HashMap;
public class ColorChooserRGB {
public static void main(String[] args) {
// Create new HashMap
HashMap<String, Object> colorObjectsHashMap = new HashMap<>();
// Fill with colors
colorObjectsHashMap.put("black", Color.BLACK);
colorObjectsHashMap.put("lightGray", Color.LIGHT_GRAY);
colorObjectsHashMap.put("blue", Color.BLUE);
colorObjectsHashMap.put("magenta", Color.MAGENTA);
colorObjectsHashMap.put("cyan", Color.CYAN);
colorObjectsHashMap.put("orange", Color.ORANGE);
colorObjectsHashMap.put("darkGray", Color.DARK_GRAY);
colorObjectsHashMap.put("pink", Color.PINK);
colorObjectsHashMap.put("gray", Color.GRAY);
colorObjectsHashMap.put("red", Color.RED);
colorObjectsHashMap.put("green", Color.GREEN);
colorObjectsHashMap.put("white", Color.WHITE);
colorObjectsHashMap.put("yellow", Color.YELLOW);
// Get all values from the HashMap and display them.
System.out.println("Values of colorObjectsHashMap: ");
Collection<Object> objValues = colorObjectsHashMap.values();
for (Object value : objValues) {
System.out.println("\t" + value);
}
}
public Color getColor(String selected) {
return Color.decode(selected);
}
public Collection<Object> getKeySet() {
return null;
}
}
输出:
Values of colorObjectsHashMap:
java.awt.Color[r=192,g=192,b=192]
java.awt.Color[r=255,g=0,b=255]
java.awt.Color[r=255,g=175,b=175]
java.awt.Color[r=0,g=255,b=0]
java.awt.Color[r=0,g=0,b=0]
java.awt.Color[r=255,g=255,b=0]
java.awt.Color[r=0,g=255,b=255]
java.awt.Color[r=255,g=200,b=0]
java.awt.Color[r=255,g=0,b=0]
java.awt.Color[r=128,g=128,b=128]
java.awt.Color[r=0,g=0,b=255]
java.awt.Color[r=255,g=255,b=255]
java.awt.Color[r=64,g=64,b=64]
FLOW:我需要在ColorTest类中使用我的新类RGB数据来选择颜色并以该颜色绘制形状。如何在&#34时调用我的类,将我的Object数据传递给此ColorTest类; chooser = new ColorChooser();&#34; ???我把我的主要改为&#34;公共对象[] colorChooser(列表列表)&#34;并添加&#34;返回objValues.toArray();&#34;,但那不是它。那我该怎么回事呢?我已经尝试过我的IDE中的一些建议,但它总是会遇到更多问题。谢谢你的帮助!
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
public class ColorTest extends JFrame {
private JList<String> colorList;
private ColorChooser chooser;
private JPanel drawPanel;
public ColorTest() {
super("List Test");
setLayout(new GridLayout(2, 1));
chooser = new ColorChooser();
// create a list with items in colorNames array
colorList = new JList<String>(chooser.getKeySet().toArray(new String[0]));
colorList.setVisibleRowCount(5);
colorList.setSelectedIndex(0);
// do not allow multiple selections
colorList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JPanel choosePanel = new JPanel();
// add a JScrollPane containing JList to content pane
choosePanel.add(new JScrollPane(colorList));
// set up event handler
colorList.addListSelectionListener(
new ListSelectionListener() { // anonymous inner class
// handle list selection events
public void valueChanged(ListSelectionEvent event) {
repaint();
}
}
); // end call to addListSelectionListener
drawPanel = new JPanel(); // create a drawing panel
drawPanel.setBackground(Color.WHITE);
// add the panels to the window
add(drawPanel);
add(choosePanel);
setSize(300, 300);
setVisible(true);
}
public void paint(Graphics g) {
super.paint(g);
// obtain the selected value
Object selected = colorList.getSelectedValue();
// set the color
g.setColor(chooser.getColor((String)selected));
// draw the rectangle
Rectangle bounds = drawPanel.getVisibleRect();
g.fillRect(bounds.x + 10, bounds.y + 30, bounds.width - 15, bounds.height - 15);
}
public static void main(String[] args) {
ColorTest application = new ColorTest();
application.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
} // end class ColorTest
结果: 线程&#34; main&#34;中的例外情况显示java.lang.NullPointerException
更新ColorChooser: ColorChooser第38行=返回Color.decode(objValues); ColorTest行62 = g.setColor(chooser.getColor((String)selected)); 结果:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.lang.Integer.decode(Integer.java:1161)
at java.awt.Color.decode(Color.java:729)
at edu.austincc.colors.ColorChooser.getColor(ColorChooser.java:38)
at edu.austincc.colors.ColorTest.paint(ColorTest.java:62)
at javax.swing.RepaintManager$4.run(RepaintManager.java:842)
at javax.swing.RepaintManager$4.run(RepaintManager.java:814)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:814)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:789)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:738)
at javax.swing.RepaintManager.access$1200(RepaintManager.java:64)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1732)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
答案 0 :(得分:0)
从评论中发布的堆栈跟踪,下面的行似乎是抛出异常:
colorList = new JList<String>(chooser.getKeySet().toArray(new String[0]));
chooser.getKeySet()
正在返回一个null
对象,因此,在空对象上调用toArray
会引发异常。以下是getKeySet()
的实施:
public Collection<Object> getKeySet() {
return null;
}
在理想的场景中,它应返回的内容取决于类/模块的功能。但是,如果我们想要阻止异常,那么我们可以使用以下实现(它只返回一个空列表btw):
public Collection<Object> getKeySet() {
return Collections.<Object>emptyList();
}