ho_dare ago
ho_dare ago

Reputation: 137

Choose color to draw lines

Given a JFrame I am drawing lines on it by handling MouseListener. Now I had added a button to choose a color from Color chooser . I want that after selecting a color all lines that i draw become of that choose color.

Here is my code I had tried adding code to ButtonListener but can't get any success.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class myJPanelstd extends JPanel implements MouseMotionListener
{
JButton bmessage;
JPanel p1;
JButton changeColor;
public myJPanelstd(){
setBackground(Color.pink);
setLayout(new BorderLayout());
p1 = new JPanel();
p1.setLayout(new GridLayout(3,3));
bmessage = new JButton();
    changeColor= new JButton("CHANGE COLOR");
p1.add(bmessage);
    p1.add(changeColor);
add(p1,"South");
    changeColor.addActionListener(new ButtonListener());
    addMouseMotionListener(this);
}
public void mouseMoved(MouseEvent evt)
{
    Point pt = evt.getPoint();
    String sx = "x = " + pt.getX();
    String sy = "y = " + pt.getY();
    bmessage.setText("you are doing fine");
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
  //Color c = JColorChooser.showDialog(null, "Choose a Color", sampleText.getForeground());
  //if (c != null)
    //sampleText.setForeground(c);
}
}
public void mouseDragged(MouseEvent evt)
{
    bmessage.setText("Nice Drawing!");
    Point pt = evt.getPoint();
    Graphics gg = getGraphics();
    gg.setColor(Color.RED);         
    gg.fillRect(pt.x,pt.y,5,5);        
    getGraphics().setColor(Color.RED);         
    getGraphics().fillRect(pt.x,pt.y,5,5);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.blue); 
Image myImage = Toolkit.getDefaultToolkit().getImage("images/fred.jpg");
g.drawImage(myImage, 0, 0, this);
g.setColor(Color.yellow);
g.drawString("Draw on Me", 250,150); 
}
}

Upvotes: 0

Views: 1788

Answers (2)

Kuba
Kuba

Reputation: 839

You need to store all painted elements and repaint them in paintComponent(), otherwise they will disappear when you minimalize/resize/cover window and uncover/ etc.

For example every line you are creating store in ArrayList and repaint them in a loop at the beginning of paintComponent(), then when you change color, all lines will be painted in new color.

Don't forget to repaint() after color change

Simple pseudo code - just to see the idea - it won't compile

class A extends JPanel
{
  ArrayList<Shape> shapes;

  public A()
  {
      shapes = new ArrayList<Shape>();
  }

  public void afterMouseRelease();
  {
     //paint the shape 
     shapes.add(yourNewShape);  // store it for later
  }

  public void paintComponen(Graphics g)
  {
     super.paintComponent();
     setColor()
     for(Shape s : shapes)
     {
         // paint it again
     }

     // rest of painting
  }
}

I used Shape class from java.awt.Shape, and Graphics2D in my app some time ago.

Upvotes: 2

Chandan Sharma
Chandan Sharma

Reputation: 598

Use JColorChooser.showDailog(-,-); to choose color under your action listener

Refer sample color picker using Java swing

// ColorPicker.java
// A quick test of the JColorChooser dialog.
//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ColorPicker extends JFrame {

  public ColorPicker() {
    super("JColorChooser Test Frame");
    setSize(200, 100);
    final Container contentPane = getContentPane();
    final JButton go = new JButton("Show JColorChooser");
    go.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        Color c;
        c = JColorChooser.showDialog(
                  ((Component)e.getSource()).getParent(),
                  "Demo", Color.blue);
        contentPane.setBackground(c); 
      }
    });
    contentPane.add(go, BorderLayout.SOUTH);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
  }

  public static void main(String args[]) {
    ColorPicker cp = new ColorPicker();
    cp.setVisible(true);
  }
}

Upvotes: 0

Related Questions