I am trying to use the color chooser to select a color to draw with. I can get it to display color selection and draw in black but I'm stuck after that.
package sketch;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
/**
*
* @author Brittany
*/
public class Sketch extends JPanel{
/**
* Keeps track of the last point to draw the next line from.
*/
private Point lastPoint;
/**
* Constructs a panel, registering listeners for the mouse.
*/
public Sketch() {
Color drawColor = Color.BLACK;
// When the mouse button goes down, set the current point
// to the location at which the mouse was pressed.
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
lastPoint = new Point(e.getX(), e.getY());
}
});
// When the mouse is dragged, draw a line from the old point
// to the new point and update the value of lastPoint to hold
// the new current point.
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
Graphics g = getGraphics();
g.setColor(drawColor);
g.drawLine(lastPoint.x, lastPoint.y, e.getX(), e.getY());
lastPoint = new Point(e.getX(), e.getY());
g.dispose();
}
});
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
JFrame frame = new JFrame("Simple Sketching Program");
JButton colorBtn = new JButton("Color");
colorBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
Color newColor = JColorChooser.showDialog(frame,"Choose Color",Color.BLACK);
if(newColor != null){
}
}
});
frame.getContentPane().add(new Sketch(), BorderLayout.CENTER);
frame.getContentPane().add(colorBtn, BorderLayout.NORTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setVisible(true);
}
}
Graphics g = getGraphics();is NOT how custom painting works.getGraphicscan returnnulland is nothing more than a snapshot of the previous paint pass, which will be replaced on the next paint pass.Start by taking a look at Performing Custom Painting and Painting in AWT and Swing for more details about how painting works and how you should with it.
You then have two choices. You can either use a
BufferedImageas your primary drawing surface, and then paint that to the component, or you can keep track of what's been painted (and in what color) and reproduce it each time a paint cycle occurs. Both have pros and cons and which you would use will depend on you needs.To answer your question. You need some way to pass the new color to an instance of
Sketch.I'd start by creating a setter in
Sketchwhich can be used to other classes to change the color...Then you would use this method to pass the new color to it...