Results 1 to 7 of 7
  1. #1
    aRTx is offline Member
    Join Date
    Mar 2009
    Posts
    27
    Rep Power
    0

    Question How to use JColorChooser?

    Hi,

    I'm trying to learn JColorChooser (this is a powerful tool of Java for Colors).

    I have seen some samples, but I don't understand how to use it.

    Can anyone make any simple application to change the CIRCLE color through JColorChooser?

    please...

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Can anyone make any simple application to change the CIRCLE color through JColorChooser?
    Yep you can, but first I recommend that you go through the Sun color chooser tutorial which will show you step by step how to do this. After you go through this, why not give it a go on your own, then come on back if you hit any speed-bumps along the way.

    How to Use Color Choosers (The Java™ Tutorials > Creating a GUI with JFC/Swing > Using Swing Components)

    Good luck!

  3. #3
    aRTx is offline Member
    Join Date
    Mar 2009
    Posts
    27
    Rep Power
    0

    Default Thank you!

    In the end
    That is wat I want to do:

    Java Code:
    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();
    	}
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I would do all the drawing in a JPanel (and override its paintComponent method, not paint). The first method of this method should be super.paintComponent(g).

    Then add this JPanel to your JFrame's contentPane.

  5. #5
    albi_87m is offline Member
    Join Date
    Apr 2009
    Posts
    8
    Rep Power
    0

    Default This is original problem

    i've got to finish it till 23:59 (today) so plz help me


    Java Code:
    /**  
    3.5 Swing contains the class JColorChooser that allows 
        interactive color selections through a dialog box. 
        Modify the program in Exercise 3.2 to allow the 
        selection of drawing colors using the JColorChooser 
        class.  */
    
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.geom.*;
    
    public class Ex2 extends JApplet {
      public static void main(String s[]) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JApplet applet = new Ex2();
        applet.init();
        frame.getContentPane().add(applet);
        frame.pack();
        frame.setVisible(true);
      }
      
      public void init() {
        JPanel panel = new Ex2Panel();
        getContentPane().add(panel);
      }
    }
    
    class Ex2Panel extends JPanel{          
      public Ex2Panel() {
        setPreferredSize(new Dimension(400, 400));
        setBackground(Color.white);
      }
    
      public void paintComponent(Graphics g) {
        super.paintComponent(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(Color.green);
        g2.fill(a);
        g2.setColor(Color.black);
        g2.draw(a);
      }
    }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by albi_87m View Post
    i've got to finish it till 23:59 (today) so plz help me...
    multiposted elsewhere in these fora.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    solution here ignored: check
    then same question cross-posted in the sun forums and same answer given today: check
    Last edited by Fubarable; 04-03-2009 at 04:10 AM.

Similar Threads

  1. Replies: 1
    Last Post: 03-31-2009, 04:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •