Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to use JColorChooser?

843789Apr 2 2009 — edited Apr 2 2009
I have try many times but I don't know how to use JColorChooser,

the problem:
Swing contains the class JColorChooser that allows interactive color selections through a dialog box. Draw and fill a Rectangle and let it allow the selection of drawing colors using the JColorChooser class.

I have done one but it does not work properly:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.*;
public class Alban extends JFrame implements ActionListener
{	Color ngjyra = Color.white;
	JButton b;
	public Alban()
	{	b = new JButton("Zgjidh ngjyren");
		b.addActionListener(this);
	    Container content = this.getContentPane();
	    content.setLayout(new FlowLayout()); 
	    content.add(b);
	    setTitle("Example1");
	    setSize(400, 450);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
	    setResizable(false);
 	}
	  public void actionPerformed(ActionEvent e) {
	   ngjyra = JColorChooser.showDialog(this,
	                                 "Choose Background Color",
	                                 getBackground());
	   repaint();
	  }
	public void paint(Graphics g)
	{   Graphics2D g2 = (Graphics2D)g;
	    int w = this.getWidth();
	    int h = this.getHeight();
	    int r = Math.min(w, h)-100;
	    Area a = new Area(new Rectangle(r, r));
	    a.subtract(new Area(new Ellipse2D.Double(r/4, r/4, r/2, r/2)));
	    g2.translate((w-r)/2, (h-r)/2);
	    g2.setColor(ngjyra);
	    g2.fill(a);
	    g2.setColor(Color.black);
	    g2.draw(a);
	}
	public static void main(String[] args)
	{	Alban f = new Alban();
	}
}
I need to send it to my teacher until 23:59 today...

many thnks aRTx.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 30 2009
Added on Apr 2 2009
18 comments
405 views