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

    Question How to color a rectangle throught JColorChooser?

    Swing contains the class JColorChooser that allows interactive color selections through a dialog box. Modify the program to allow the selection of drawing colors using the JColorChooser class.


    I have done with JSlider to change the color but how to do it with JColorChooser?
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import java.awt.geom.*;
    
    public class TestColors extends JApplet {
      public static void main(String s[]) {
        JFrame frame = new JFrame();
        frame.setTitle("Colors");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JApplet applet = new TestColors();
        applet.init();
        frame.getContentPane().add(applet);
        frame.pack();
        frame.setVisible(true);
      }
      
      ColorPanel panel;
      public void init() {
        panel = new ColorPanel();
        Container cp = getContentPane();
        cp.setLayout(new BorderLayout());
        cp.add(panel, BorderLayout.CENTER);
        JPanel p = new JPanel();
        cp.add(p,BorderLayout.EAST);
        p.setLayout(new GridLayout(1,3,30,10));
            panel.repaint();
          }
        ;    
      }
    
    
    class ColorPanel extends JPanel{
      int red = 100;
      int green = 100;
      int blue = 100;
      
      public ColorPanel() {
        setPreferredSize(new Dimension(500, 500));
        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(new Color(red,green,blue));
        g2.fill(a);
        g2.setColor(Color.black);
        g2.draw(a);
        
        
      }
    }
    pls help....

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
      public void someHandler(SomeEvent e) {
        // In your event handler code for the JColorChooser:
        // get the selected color from the colorChooser
        Color selected color = colorChooser.getSelectedColor();
        colorPanel.setColor(selectedColor);
      }
    
    class ColorPanel extends JPanel{
      Color color = new Color(100,100,100);
    
      public void setColor(Color color) {
        this.color = color;
        repaint();
      }
    
      public void paintComponent(Graphics g) {
        ...
        g2.setColor(color);
        g2.fill(a);
        ...

Similar Threads

  1. Replies: 1
    Last Post: 10-02-2008, 04:06 AM
  2. Rectangle Intersection
    By Gwindow in forum Java 2D
    Replies: 1
    Last Post: 04-24-2008, 04:53 PM
  3. help with rectangle class
    By darkgt in forum New To Java
    Replies: 7
    Last Post: 11-14-2007, 07:19 PM
  4. Problems sending file throught TCP sockets
    By Nite in forum Advanced Java
    Replies: 2
    Last Post: 08-04-2007, 10:01 PM
  5. Rectangle with rounded edges??
    By orchid in forum Java 2D
    Replies: 1
    Last Post: 05-10-2007, 03:31 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
  •