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...
Printable View
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...
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.Quote:
Can anyone make any simple application to change the CIRCLE color through JColorChooser?
How to Use Color Choosers (The Java™ Tutorials > Creating a GUI with JFC/Swing > Using Swing Components)
Good luck!
In the end
That is wat I want to do:
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();
}
}
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.
i've got to finish it till 23:59 (today) so plz help me
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);
}
}
solution here ignored: check
then same question cross-posted in the sun forums and same answer given today: check