Go Back   CodeCall Programming Forum > Software Development > Tutorials > Java Tutorials
Register Blogs Search Today's Posts Mark Forums Read

Java Tutorials Tutorials and Code for Java

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-10-2009, 08:09 AM
Turk4n's Avatar
Code Warrior
 
Join Date: May 2008
Location: 4chan.org/g/
Age: 20
Posts: 3,822
Turk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud of
Send a message via MSN to Turk4n Send a message via Skype™ to Turk4n
Color Mixer and JSlider

Well, why not have fun with colors?

So let me introduce you guys to my color mixer and Slider tutorial !

Packages
Code:
import javax.swing.*;
import static javax.swing.JOptionPane.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
Note by using a static import I can easily just type in instances instead of referring to the class then use the instance method...

The class
Code:
public class JMyColor extends JFrame implements ActionListener,ChangeListener {
Inheriting everything from the JFrame class and implementing instructions from the ActionListener and ChangeListener interface
Variables and instances...
Code:
JFrame JF = new JFrame();
	private JLabel mix = new JLabel("Mix Colors",JLabel.CENTER);
	private JTextField X = new JTextField("0");
	private JTextField Y = new JTextField("0");
	private JTextField Z = new JTextField("0");
	private JPanel ColorPanel = new JPanel();
	private JSlider Alpha = new JSlider();
	private JSlider Beta = new JSlider();
	private JSlider Zeta = new JSlider();
	private JButton MixColor = new JButton("Mix");
	private JMenuBar JMB = new JMenuBar();
	private JMenu JM = new JMenu("Actions");
	private JMenuItem quit = new JMenuItem("Close");
	private JLabel CopyRight = new JLabel("Copyright - Turk4n",JLabel.CENTER);
Everything in here will be used. The text fields will present the numbers you can add to change the colors. Remember we are using RGB color scheme(DEC=INTEGERS) LOL Plus the sliders will change the colors
Constructor GUI
Code:
public JMyColor() {
		setLayout(null);
		add(JMB);
		JMB.add(JM);
		JM.add(quit);
		add(MixColor);
		add(CopyRight);
		add(ColorPanel);
		add(mix); add(X); add(Y); add(Z);
		add(Alpha); add(Beta); add(Zeta);
		
		CopyRight.setBounds(0,215,100,20);
		CopyRight.setFont(new Font("Sanserif",Font.ITALIC,10));
		JMB.setBounds(0,0,300,15);
		X.setBounds(10,30,60,20);
		Y.setBounds(10,60,60,20);
		Z.setBounds(10,85,60,20);
		Alpha.setBounds(200,35,90,20);
		Alpha.setValue(0);
		Alpha.setMinimum(0);
		Alpha.setMaximum(255);
		Beta.setBounds(200,60,90,20);
		Beta.setValue(0);
		Beta.setMinimum(0);
		Beta.setMaximum(255); 
		Zeta.setBounds(200,80,90,20);
		Zeta.setValue(0);
		Zeta.setMinimum(0);
		Zeta.setMaximum(255);
		mix.setBounds(100,25,80,40);
		MixColor.setBounds(110,60,60,20);
		ColorPanel.setBounds(100,130,80,80);
		MixColor.addActionListener(this);
		quit.addActionListener(this);
		Alpha.addChangeListener(this);
		Zeta.addChangeListener(this);
		Beta.addChangeListener(this);
		
		setSize(305,270);
		setVisible(true);
                setTitle("Color Mixer");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		show();
	}
As more or less, we are adding each item without following a Layout. This not recommended for users who likes to have something prebuild and easy to use. However if you want it to be positionable in your way you have to follow like I did... Our sliders will add a change as they are moved. That's why we are using Change Listener instead of the Action Listener that we are using for our close operation and mixer !
Sliders Listener
Code:
public void stateChanged(ChangeEvent e) {
		Color c = new Color(Alpha.getValue(),Beta.getValue(),Zeta.getValue());
		
		String NewAlpha = String.valueOf(Alpha.getValue());
		String NewBeta = String.valueOf(Beta.getValue());
		String NewZeta = String.valueOf(Zeta.getValue());
		
		X.setText(NewAlpha);
		Y.setText(NewBeta);
		Z.setText(NewZeta);
		ColorPanel.setBackground(c);
	}
As we have sliders that are move able. they also generate an integer value from 0-255
So basically we set the values the sliders are on to the ColorPanel and to the textfields :>
Action Listener
Code:
public void actionPerformed(ActionEvent e) {
		
			if(e.getSource()==MixColor) {
				
				String x = X.getText();
				String y = Y.getText();
				String z = Z.getText();
				
				int NewX = 0;
				int NewY = 0;
				int NewZ = 0;
				
				try
				{
					NewX = Integer.parseInt(x);
					NewY = Integer.parseInt(y);
					NewZ = Integer.parseInt(z);
					Alpha.setValue(NewX);
					Beta.setValue(NewY);
					Zeta.setValue(NewZ);
					
					Color c = new Color(NewX, NewY, NewZ);
						ColorPanel.setBackground(c);
				}catch(NumberFormatException NFE) {
					showMessageDialog(JF,"Does not compute !","String Error/One or two fields incorrect",JOptionPane.ERROR_MESSAGE);
				}
				catch(Exception OtherException){
					showMessageDialog(JF,"Does not compute !","Out of range 0-255",JOptionPane.ERROR_MESSAGE);
				}
			}
			else if(e.getSource()==quit) {
				System.exit(0);
			}
	}
Now the action listener will read in from our textfields. However the fields are strings and we can't use strings when integers are needed to display the color... We parse it and represent them as numbers from 0-255. Now. When the user is doing something wrong, like posting something out of boundary or just plain stupid and typing in color names...It will run some error messages for their own safety...
The main
Code:
public static void main(String[] arg) {
		new JMyColor();
	}
}
Here is the end and we are going to start the application and see the power of sliders And of course the colors !

Output
color-mixer-jslider-colormixer.png
Cheers !
__________________

Hatsune Miku ~❤❤❤
初音ミク。~❤❤❤
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 08-10-2009, 08:13 AM
Jordan's Avatar
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 24,556
Jordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to all
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan Send a message via Yahoo to Jordan
Re: Color Mixer and JSlider

Very cool and useful tutorial! +rep
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 08-10-2009, 08:16 AM
Turk4n's Avatar
Code Warrior
 
Join Date: May 2008
Location: 4chan.org/g/
Age: 20
Posts: 3,822
Turk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud of
Send a message via MSN to Turk4n Send a message via Skype™ to Turk4n
Re: Color Mixer and JSlider

Quote:
Originally Posted by Jordan View Post
Very cool and useful tutorial! +rep
Why thank you
__________________

Hatsune Miku ~❤❤❤
初音ミク。~❤❤❤
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 08-10-2009, 10:39 AM
WingedPanther's Avatar
Super Moderator
 
Join Date: Jul 2006
Age: 36
Posts: 11,435
WingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud of
Re: Color Mixer and JSlider

Next stop, CMYK mixers +rep
__________________
CodeCall Blog | CodeCall Wiki | Shareware
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 08-10-2009, 10:47 AM
Turk4n's Avatar
Code Warrior
 
Join Date: May 2008
Location: 4chan.org/g/
Age: 20
Posts: 3,822
Turk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud of
Send a message via MSN to Turk4n Send a message via Skype™ to Turk4n
Re: Color Mixer and JSlider

Quote:
Originally Posted by WingedPanther View Post
Next stop, CMYK mixers +rep
Haha, yeah I could present a small update with a CMYK mixer
__________________

Hatsune Miku ~❤❤❤
初音ミク。~❤❤❤
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 08-10-2009, 01:36 PM
chili5's Avatar
Code Slinger
 
Join Date: Mar 2008
Posts: 7,018
chili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond repute
Re: Color Mixer and JSlider

Wow, that is cool. +rep

Edit: regarding the use of static imports, is it good to do that? I think it gets a bit confusing as you don't know where the method is coming from anymore?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 08-10-2009, 01:39 PM
Turk4n's Avatar
Code Warrior
 
Join Date: May 2008
Location: 4chan.org/g/
Age: 20
Posts: 3,822
Turk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud of
Send a message via MSN to Turk4n Send a message via Skype™ to Turk4n
Re: Color Mixer and JSlider

Quote:
Originally Posted by chili5 View Post
Wow, that is cool. +rep

Edit: regarding the use of static imports, is it good to do that? I think it gets a bit confusing as you don't know where the method is coming from anymore?
Thanks
I like to type less when using instances
Of course it's a programmers taste of choices right?
I mean I don't like to type.
Code:
JOptionPane.showMessageDialog(null,"LoL");
However I find this really cool and simple
Code:
showMessageDialog(null,"LoL");
More or less I can say, it's all bout the feeling :>
__________________

Hatsune Miku ~❤❤❤
初音ミク。~❤❤❤
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 08-10-2009, 02:02 PM
chili5's Avatar
Code Slinger
 
Join Date: Mar 2008
Posts: 7,018
chili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond repute
Re: Color Mixer and JSlider

Yea, it all comes down to choice I guess.

The problem with using absolute position with setBounds is it doesn't let you resize the form and maintain a good look. With the layout it lets you keep a good layout all the time.

In your constructor you use the show method.

This method is deprecated as of JDK 1.5 and you should be using Window.setVisible(boolean bVisible) instead.

So instead of:

Code:
show();
You should be using:

Code:
setVisible(true);
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 08-10-2009, 02:10 PM
Turk4n's Avatar
Code Warrior
 
Join Date: May 2008
Location: 4chan.org/g/
Age: 20
Posts: 3,822
Turk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud of
Send a message via MSN to Turk4n Send a message via Skype™ to Turk4n
Re: Color Mixer and JSlider

Quote:
Originally Posted by chili5 View Post
Yea, it all comes down to choice I guess.

The problem with using absolute position with setBounds is it doesn't let you resize the form and maintain a good look. With the layout it lets you keep a good layout all the time.

In your constructor you use the show method.

This method is deprecated as of JDK 1.5 and you should be using Window.setVisible(boolean bVisible) instead.

So instead of:

Code:
show();
You should be using:

Code:
setVisible(true);
I have often done both...LOL
Code:
setVisible(true);
setTitle("Color Mixer");
setDefaultCloseOperation(EXIT_ON_CLOSE);
show();
__________________

Hatsune Miku ~❤❤❤
初音ミク。~❤❤❤
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 08-10-2009, 02:12 PM
chili5's Avatar
Code Slinger
 
Join Date: Mar 2008
Posts: 7,018
chili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond repute
Re: Color Mixer and JSlider

Oh shoot, I didn't notice that. At least it doesn't show your form twice. What I'm saying is you shouldn't even be using show at all.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes



All times are GMT -5. The time now is 08:20 AM.


vBulletin v3.8.0 ©2010, Jelsoft Enterprises Ltd.


no new posts

LinkBacks Enabled by vBSEO 3.1.0