描画された線の色を変更するために、カラー パレット (私が調べた JColorChooser というクラスによって提供される) を促すボタンを追加しようとしました。ボタン (「色の変更」と名付けました) をクリックし、パレットから色を選択して再描画しようとすると、線の色が黒のままになり、コンソールに次のメッセージが表示されます。
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Etch_A_Sketch.actionPerformed(Etch_A_Sketch.java:45)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
など...リストは続きます
コードについてこれまでに持っているものは次のとおりです。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Etch_A_Sketch implements MouseListener, MouseMotionListener, ActionListener
{
JFrame window;
Container content;
int mouseX,mouseY,oldX,oldY;
JLabel coords;
JButton color;
Color lineColor = Color.black;
public Etch_A_Sketch()
{
JFrame window = new JFrame("Classic Etch a Sketch");
content = window.getContentPane();
content.setLayout( new FlowLayout() );
color = new JButton("Change color");
color.addActionListener(this);
coords = new JLabel();
coords.setFont(new Font("TimesRoman", Font.ITALIC + Font.BOLD, 32));
content.add(color);
content.add( coords);
content.addMouseListener(this);
content.addMouseMotionListener(this);
window.setSize(640,480);
window.setVisible(true);
}
public void actionPerformed( ActionEvent e)
{
if(e.getSource() == color)
{
lineColor = JColorChooser.showDialog(window, "Pick a color", lineColor);
if(lineColor != null)
{
window.getContentPane().setBackground(lineColor);
}
}
}
public void mouseClicked( MouseEvent me)
{
mouseX = me.getX();
mouseY = me.getY();
reportCoords("Mouse clicked at: " + mouseX + "," + mouseY);
}
public void mousePressed( MouseEvent me)
{
mouseX = me.getX();
mouseY = me.getY();
reportCoords("Mouse Pressed at: " + mouseX + "," + mouseY);
}
public void mouseReleased( MouseEvent me)
{
mouseX = me.getX();
mouseY = me.getY();
reportCoords("Mouse released at: " + mouseX + "," + mouseY);
}
public void mouseExited( MouseEvent me)
{
mouseX = me.getX();
mouseY = me.getY();
reportCoords("Mouse exited at: " + mouseX + "," + mouseY);
}
public void mouseEntered( MouseEvent me)
{
mouseX = me.getX();
mouseY = me.getY();
reportCoords("Mouse Entered at: " + mouseX + "," + mouseY);
}
public void mouseDragged( MouseEvent me)
{
mouseX = me.getX();
mouseY = me.getY();
if (oldX ==0 )
{
oldX=mouseX;
oldY=mouseY;
return;
}
Graphics g = content.getGraphics();
g.drawLine( oldX,oldY, mouseX, mouseY );
oldX = mouseX;
oldY = mouseY;
reportCoords("Mouse Dragged at: " + mouseX + "," + mouseY);
}
public void mouseMoved( MouseEvent me)
{
mouseX = me.getX();
mouseY = me.getY();
reportCoords("Mouse Moved at: " + mouseX + "," + mouseY);
}
// ..............................................................
public static void main( String[] args)
{
new Etch_A_Sketch();
}
private void reportCoords( String msg )
{
coords.setText( msg );
}
}
私が達成したいのは、線の色をユーザーの色の選択に変更できるようにすることだけです。