User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 391,905 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,597 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 3192 | Replies: 4
Reply
Join Date: Feb 2006
Posts: 10
Reputation: javafan is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
javafan javafan is offline Offline
Newbie Poster

Color change problem

  #1  
Feb 14th, 2006
Please see the code

import java.awt.*;        // Defines basic classes for GUI programming.
     import java.awt.event.*;  // Defines classes for working with events.
     import java.applet.*;     // Defines the applet class.
     
     public class ColoredHelloWorldApplet
                       extends Applet implements ActionListener {
                          
           // Defines a subclass of Applet.  The "implements ActionListener"
           // part says that objects of type ColoredHelloApplet are
           // capable of listening for ActionEvents.  This is necessary
           // if the applet is to respond to events from the button.
                             
        Font textFont;    // The font in which the message is displayed.
                          // A font object represent a certain size and
                          // style of text drawn on the screen.
        TextField T1;
        TextField T2;
        TextField T3;
        int redColor, greenColor, blueColor;
        String as, ag, ab;
        Button bttn;
        public void init() {
     
               // This routine is called by the system to initialize 
               // the applet.  It sets up the font and initial colors
               // the applet.  It adds a button to the applet for 
               // changing the message color.
               
            setBackground(Color.lightGray);
                  // The applet is filled with the background color before
                  // the paint method is called.  The button and the message
                  // in this applet will appear on a light gray background.
     
            redColor=0;
            greenColor=0;
            blueColor=0;
            
            
            textFont = new Font("Serif",Font.BOLD,24);
                  // Create a font object representing a big, bold font.
     
     /*
     		TextField T1=new TextField("",12);
     		TextField T2=new TextField("",12);
     		TextField T3=new TextField("",12);
     		*/
     		
     		T1=new TextField("",12);
     	 	T2=new TextField("",12);
     	 	T3=new TextField("",12);
     		
     		
     		add(T1);
     		add(T2);
     		add(T3);
            bttn = new Button("Change Color");
                  // Create a new button.  "ChangeColor" is the text
                  // displayed on the button.

            bttn.addActionListener(this);  
                  // Set up bttn to send an "action event" to this applet
                  // when the user clicks the button.  The parameter, this,
                  // is a name for the applet object that we are creating.
                    
            add(bttn);  // Add the button to the applet, so that it 
                        // will appear on the screen.
     
        }  // end init()
        
        
        public void paint(Graphics g) {
              
              // This routine is called by the system whenever the content
              // of the applet needs to be drawn or redrawn.  It displays 
              // the message "Hello World" in the proper color and font.
     
           Color mixColor=new Color(redColor, greenColor,blueColor);
           //Color mixColor=new Color(255,255,50);

           //g.setColor(mixColor);
           g.setColor(mixColor);
           //g.setColor(new Color(redColor, greenColor, blueColor));
           g.setFont(textFont);       // Set the font.
           
           g.drawString("This Color changes", 20,70);    // Draw the message.
           //T1.setText("Is this function reached");
     
        }  // end paint()
        
     
        public void actionPerformed(ActionEvent evt) {
        
              // This routine is called by the system when the user clicks
              // on the button.  The response is to change the colorNum
              // which determines the color of the message, and to call
              // repaint() to see that the applet is redrawn with the
              // new color.
            // T1.setText("23");
            
            if (evt.getSource() == bttn)
            {
              
              as=T1.getText();
              ag=T2.getText();
              ab=T3.getText();
       	  	  as=as.trim();
          	  ag=ag.trim();
              ab=ab.trim();
          
           redColor= Integer.parseInt(as);
           greenColor= Integer.parseInt(ag);
           blueColor= Integer.parseInt(ab);
           
           repaint();  // Tell system that this applet needs to be redrawn
          }
        }  // end init()
        
     } // end class ColoredHelloWorldApplet


I tested everything
redColor, greenColor,blueColor are getting the values
The only problem with teh code is in these lines

Color mixColor=new Color(redColor, greenColor,blueColor);
//Color mixColor=new Color(255,255,50);

//g.setColor(mixColor);
g.setColor(mixColor);

Can anyone help please?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2006
Posts: 10
Reputation: javafan is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
javafan javafan is offline Offline
Newbie Poster

Re: Color change problem

  #2  
Feb 14th, 2006
When I call g.setColor(Color.red) it works but the above code
Why?
Reply With Quote  
Join Date: Feb 2006
Posts: 10
Reputation: javafan is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
javafan javafan is offline Offline
Newbie Poster

Re: Color change problem

  #3  
Feb 14th, 2006
I got it to work
Can someone please help me how do I change the background rather than changing just text color?
Reply With Quote  
Join Date: Jun 2004
Location: H4x0rville
Posts: 2,105
Reputation: server_crash is on a distinguished road 
Rep Power: 9
Solved Threads: 18
server_crash's Avatar
server_crash server_crash is offline Offline
Postaholic

Re: Color change problem

  #4  
Feb 14th, 2006
setBackground(Color c);
Reply With Quote  
Join Date: Feb 2006
Posts: 10
Reputation: javafan is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
javafan javafan is offline Offline
Newbie Poster

Re: Color change problem

  #5  
Feb 14th, 2006
Originally Posted by server_crash
setBackground(Color c);

Thanks, it worked.
Only the last problem
Whenever someone enters value except betwen 0 and 255 throw an error message

I spent few hours but not able to understand why this doesnt works!

if((redColor>255 || redColor<0)||(greenColor>255 || greenColor<0)||(blueColor>255 || blueColor<0))
{

String title = "Greetings"; // Shown in title bar of dialog box.
String message = "Please enter values only between 0 and 225.";
JOptionPane.showMessageDialog(null, message, title,
JOptionPane.INFORMATION_MESSAGE);

// g2d.setColor(Color.red);
// g2d.drawString("Please enter values only between 0 and 225", 20,70);
}
else
{
g2d.setColor(new Color(redColor, greenColor,blueColor));
}

     import java.awt.*;        // Defines basic classes for GUI programming.
     import java.awt.event.*;  // Defines classes for working with events.
     import java.applet.*;     // Defines the applet class.
     import java.util.StringTokenizer;
     import javax.swing.*;
          
     public class ColoredHelloWorldApplet
                       extends Applet implements ActionListener {
                          
           // Defines a subclass of Applet.  The "implements ActionListener"
           // part says that objects of type ColoredHelloApplet are
           // capable of listening for ActionEvents.  This is necessary
           // if the applet is to respond to events from the button.
                             
        Font textFont;    // The font in which the message is displayed.
                          // A font object represent a certain size and
                          // style of text drawn on the screen.
                          
        TextField T1;
        TextField T2;
        TextField T3;
        int redColor, greenColor, blueColor;
        int increment;
        /*String as, ag, ab; */
        //Button bttn;
        public void init() {
     
               // This routine is called by the system to initialize 
               // the applet.  It sets up the font and initial colors
               // the applet.  It adds a button to the applet for 
               // changing the message color.
               
            setBackground(Color.lightGray);
                  // The applet is filled with the background color before
                  // the paint method is called.  The button and the message
                  // in this applet will appear on a light gray background.
     
            redColor=10;
            greenColor=20;
            blueColor=30;
            increment=1;
                      
            textFont = new Font("Serif",Font.BOLD,24);
                  // Create a font object representing a big, bold font.
          		
     		T1=new TextField("",12);
     	 	T2=new TextField("",12);
     	 	T3=new TextField("",12);
			//String message = "Line1\nLine2";
    		//JOptionPane.showMessageDialog(this, message);
     		
     		
     		add(T1);
     		add(T2);
     		add(T3);
            Button bttn = new Button("Change Color");
                  // Create a new button.  "ChangeColor" is the text
                  // displayed on the button.

            bttn.addActionListener(this);  
                  // Set up bttn to send an "action event" to this applet
                  // when the user clicks the button.  The parameter, this,
                  // is a name for the applet object that we are creating.
                    
            add(bttn);  // Add the button to the applet, so that it 
                        // will appear on the screen.
     
        }  // end init()
        
        
        public void paint(Graphics g) {
              
              // This routine is called by the system whenever the content
              // of the applet needs to be drawn or redrawn.  It displays 
              // the message "Hello World" in the proper color and font.
              
           Graphics2D g2d = (Graphics2D)g;
                            
           Color mixColor=new Color(redColor, greenColor,blueColor);
     
           setBackground(new Color(redColor, greenColor, blueColor));
     
           if((redColor>255 || redColor<0)||(greenColor>255 || greenColor<0)||(blueColor>255 || blueColor<0))
           {

			 String title = "Greetings";  // Shown in title bar of dialog box.
	         String message = "Please enter values only between 0 and 225.";
        	 JOptionPane.showMessageDialog(null, message, title,
                                        JOptionPane.INFORMATION_MESSAGE);

    //		g2d.setColor(Color.red);
    //		g2d.drawString("Please enter values only between 0 and 225", 20,70);
           }
           else
           {
           	g2d.setColor(new Color(redColor, greenColor,blueColor));
           }
           
           increment=increment+1;
     
        }  // end paint()
        
     
        public void actionPerformed(ActionEvent evt) {
        
              // This routine is called by the system when the user clicks
              // on the button.  The response is to change the colorNum
              // which determines the color of the message, and to call
              // repaint() to see that the applet is redrawn with the
              // new color.
            // T1.setText("23");
                         
                         
                         String as1=new String();
                         String ag1=new String();
                         String ab1=new String();
                         
                         
                         
                         String as=new String();
                         String ag=new String();
                         String ab=new String();
                         
              as1=T1.getText();
              ag1=T2.getText();
              ab1=T3.getText();
              
       	  	  as=as1.trim();
          	  ag=ag1.trim();
              ab=ab1.trim();
          
           redColor= Integer.parseInt(as);
           greenColor= Integer.parseInt(ag);
           blueColor= Integer.parseInt(ab);
           
           repaint();  // Tell system that this applet needs to be redrawn
          
        }  // end init()
        
     } // end class ColoredHelloWorldApplet

Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

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

 

DaniWeb Java Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 7:38 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC