Help on changing colour please....

I am doing a paint package that involve scribbling, I have the basic class and I have setted up the windows interface....... I want to allow the user to choose the colour of the line..... I have setted up the button but I am not sure how should I code to set the colour when the button is pressed..... I will leave my codes here......

DrawingTools.java

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.awt.geom.*;

public class DrawingTools {

public static void main (String [] args) {

Toolkit tk = Toolkit.getDefaultToolkit(); // get system dependent information

Dimension dim = tk.getScreenSize(); // encapsulate width and height of system

JFrame f = new DrawingToolFrame();

f.setSize(800, 550);

f.setTitle("Drawing Tool v1.10");

f.setIconImage(tk.getImage("Paint.gif"));

f.setVisible(true);

f.setResizable(false);

}

}

class DrawingToolFrame extends JFrame implements ActionListener {

private JCheckBox checkNormal, checkBold, checkDotted, checkSpray;

private JButton boldButton, dottedButton, sprayButton;

private JButton blackButton, blueButton, yellowButton, redButton, greenButton, houseButton, tvButton, starButton;

private ScribblePanel scribblePanel;

private JButton scribbleButton, undoButton, newButton, quitButton;

private JMenuItem newItem, quitItem, undoItem, houseItem, tvItem, starItem,/* normalItem,*/ boldItem, dottedItem, sprayItem, blackItem, blueItem, redItem, yellowItem, greenItem;

public DrawingToolFrame() {

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

dispose();

System.exit(0);

}

} );

Container contentPane = getContentPane();

//

JMenuBar menuBar = new JMenuBar();

setJMenuBar(menuBar);

JMenu fileMenu = new JMenu("File");

newItem = new JMenuItem("New");

newItem.setMnemonic(KeyEvent.VK_N);//add Alt-N to New short-cut

newItem.addActionListener(this);

fileMenu.add(newItem);

quitItem = new JMenuItem("Quit");

quitItem.setMnemonic(KeyEvent.VK_Q);//add Alt-Q to Quit short-cut

quitItem.addActionListener(this);

fileMenu.add(quitItem);// some code for menu.......

menuBar.add(optionMenu);

//

JPanel p = new JPanel();

ImageIcon scribbleIcon = new ImageIcon("Scribble.gif");

scribbleButton = new JButton("Scribble", scribbleIcon);

scribbleButton.setMnemonic(KeyEvent.VK_S);//add Alt-S to Undo short-cut

scribbleButton.setToolTipText("Click this button and you can draw free hand lines.");

p.add(scribbleButton);

p.add(Box.createHorizontalStrut(20));

ImageIcon undoIcon = new ImageIcon("Undo.gif");

undoButton = new JButton("Undo", undoIcon);

undoButton.setMnemonic(KeyEvent.VK_U);//add Alt-U to Undo short-cut

undoButton.setToolTipText("Undo the last step");

p.add(undoButton);

p.add(Box.createHorizontalStrut(20));

ImageIcon newIcon = new ImageIcon("New.gif");

newButton = new JButton("New", newIcon);

newButton.setMnemonic(KeyEvent.VK_N);//add Alt-N to New short-cut

newButton.setToolTipText("Make a new Drawing");

p.add(newButton);

p.add(Box.createHorizontalStrut(20));

ImageIcon quitIcon = new ImageIcon("Door.gif"); //add icon to button

quitButton = new JButton("Quit", quitIcon);

quitButton.setMnemonic(KeyEvent.VK_Q);//add Alt-Q to exit short-cut

quitButton.setToolTipText("Click to Exit");

p.add(quitButton);

undoButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

scribblePanel.undo();}});

scribbleButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

scribblePanel.repaint();}});

newButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

scribblePanel.repaint();}});

quitButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

System.exit(0);}});

contentPane.add(p, "South");

//

p = new JPanel();

p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));

p.add(Box.createVerticalStrut(10));

ImageIcon boldIcon = new ImageIcon("Bold.gif"); //add icon to button

boldButton = new JButton("Bold", boldIcon);

p.add(boldButton);

p.add(Box.createVerticalStrut(10));

ImageIcon dottedIcon = new ImageIcon("Dotted.gif"); //add icon to button

dottedButton = new JButton("Dotted", dottedIcon);

p.add(dottedButton);

p.add(Box.createVerticalStrut(10));

ImageIcon sprayIcon = new ImageIcon("Spray.gif"); //add icon to button

sprayButton = new JButton("Spray", sprayIcon);

p.add(sprayButton);

contentPane.add(p, "East");

//-

p = new JPanel();

p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));

p.add(Box.createVerticalStrut(10));

ImageIcon dIcon = new ImageIcon("Black.gif"); //add icon to button

blackButton = new JButton("Black", dIcon);

p.add(blackButton);

p.add(Box.createVerticalStrut(10));

ImageIcon eIcon = new ImageIcon("Blue.gif"); //add icon to button

blueButton = new JButton("Blue", eIcon);

p.add(blueButton);

p.add(Box.createVerticalStrut(10));

ImageIcon fIcon = new ImageIcon("Red.gif"); //add icon to button

redButton = new JButton("Red", fIcon);

p.add(redButton);

p.add(Box.createVerticalStrut(10));

ImageIcon gIcon = new ImageIcon("Yellow.gif"); //add icon to button

yellowButton = new JButton("Yellow", gIcon);

p.add(yellowButton);

p.add(Box.createVerticalStrut(10));

ImageIcon hIcon = new ImageIcon("Green.gif"); //add icon to button

greenButton = new JButton("Green", hIcon);

p.add(greenButton);

p.add(Box.createVerticalStrut(30));

ImageIcon houseIcon = new ImageIcon("house.gif");

houseButton = new JButton("House", houseIcon);

houseButton.setToolTipText("Click to add a house in the picture!");

p.add(houseButton);

p.add(Box.createVerticalStrut(10));

ImageIcon tvIcon = new ImageIcon("tv.gif");

tvButton = new JButton("Television", tvIcon);

tvButton.setToolTipText("Click to add a Television in the picture!");

p.add(tvButton);

p.add(Box.createVerticalStrut(10));

ImageIcon starIcon = new ImageIcon("star.gif");

starButton = new JButton("Star", starIcon);

starButton.setToolTipText("Click to add stars in the picture!");

p.add(starButton);

contentPane.add(p, "West");

//

scribblePanel = new ScribblePanel();

contentPane.add(scribblePanel, "Center");

}

//

private JButton addJButton(String text, Container container) {

JButton button = new JButton(text);

container.add(button);

return button;

}

public void actionPerformed(ActionEvent e) {

Object source = e.getSource();

if (source instanceof JMenuItem) {

String arg = e.getActionCommand();

if (arg.equals("Undo"))

scribblePanel.undo();

else if (arg.equals("New"))

scribblePanel.repaint();

else if (arg.equals("Quit"))

System.exit(0);

}

}

}

ScribblePanel.java

import java.awt.*;

import java.awt.geom.*;

import java.awt.event.*;

import javax.swing.*;

import java.awt.image.BufferedImage;

public class ScribblePanel extends JPanel implements MouseListener, MouseMotionListener {

private int lastX, lastY;

private static final int PANEL_WIDTH = 800;

private static final int PANEL_HEIGHT = 550;

private Stroke stroke;

private Line2D line;

private BufferedImage currImage, oldImage, tmpImage;

public ScribblePanel() {

super();

setBackground(Color.white);

currImage = new BufferedImage(PANEL_WIDTH, PANEL_HEIGHT,

BufferedImage.TYPE_INT_ARGB);

oldImage = new BufferedImage(PANEL_WIDTH, PANEL_HEIGHT,

BufferedImage.TYPE_INT_ARGB);

addMouseListener(this);

addMouseMotionListener(this);

line = new Line2D.Double(0,0,0,0);

stroke = new BasicStroke(10, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND);

}

public void paintComponent (Graphics g) {

super.paintComponent(g);

Graphics2D g2 = (Graphics2D) g;

g2.drawImage(currImage, 0, 0, this);

//scribblePanel.drawAll(g);

}

public void undo() {

tmpImage = currImage;

currImage = oldImage;

oldImage = tmpImage;

repaint();

}

public void moveTo(int x, int y) {

lastX = x; lastY = y;

}

/*public void lineTo(int x, int y) {

Graphics2D g2 = currImage.createGraphics();

line.setLine(lastX, lastY, x, y);

g2.setPaint(Color.blue);

g2.setStroke(stroke);

g2.draw(line);

moveTo(x,y);

g2.dispose();

repaint();

}*/

public void lineTo(int x, int y) {

Graphics2D g2 = currImage.createGraphics();

line.setLine(lastX, lastY, x, y);

/*if blueButton pressed

g2.setPaint(Color.blue);

else if redButton pressed

g2.setPaint(Color.red);

else if yellowButton pressed

g2.setPaint(Color.yellow);

else if greenButton pressed

g2.setPaint(Color.green);

else g2.setPaint(Color.black);*/

/*if boldButton pressed

g2.setStroke(stroke);

else if dottedButton pressed

g2.setStroke(dotted);

else if sprayButton pressed

g2.setStroke(spray);*/// these are my imagination...... i knoe they

// are wrong but what should I do?please

// help

g2.setPaint(Color.blue);

g2.draw(line);moveTo(x,y);

g2.dispose();

repaint();

}

public void mousePressed(MouseEvent e) {

oldImage.setData(currImage.getData());

moveTo(e.getX(), e.getY());

}

public void mouseReleased(MouseEvent e) {}

public void mouseClicked(MouseEvent e) {}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mouseDragged(MouseEvent e) {

lineTo(e.getX(), e.getY());

}

public void mouseMoved(MouseEvent e) {}

}

BTW...... if I want to use the SetStroke (for thicker line, dotted line) method in the Graphics2D package what should i do?

[10575 byte] By [trexuka] at [2007-9-19]
# 1
Two obvious routes: 1. Have a variable currentColor or somesuch which you set when the user clicks a button. Then before drawing the line call g.setColor(currentColor). 2. If you have access to the Graphics object when the button is clicked, call setColor directly.
pjt33a at 2007-7-8 > top of java,Archived Forums,Java Programming [Archive]...