Results 1 to 13 of 13
  1. #1
    Join Date
    Feb 2015
    Posts
    6
    Rep Power
    0

    Default Program to changeto colors of a shape in the other window.

    Hi, I'm new to the forums :)

    I was working on this project, and I have everything working, except that it doesn't change the colors of the shapes in the other window.

    Here's the code to see what I'm doing wrong:



    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class JDemo {
    	//Global Variables
    	static Color color0 = Color.blue; //sets the value in the color0 variable to the method Color.blue .
    	static Color color1 = Color.red; //Sets the value in the color1 variable to the method Color.red .
    	
    	//YOU NEED TO CREATE THE EMPTY VARIABLES NOW, SO THAT WHEN YOU FILL THEM, YOU CAN CALL THEM IN THE MAIN FUNCTION.
    	static JPanel Panel0 = null;
    	static JPanel Panel1 = null;
    	
    	public static void main(String[] args) {
    		/**
    		 * This is the main function. It's only purpose in this program is to call the classes, methods,
    		 * etc. that were created in the public class JDemo and have the program execute it. 
    		 * 
    		 * They needed to have been created along with the global variables at the top of the program, otherwise 
    		 * they would not be able to be called here in the main function - The error "Panel0 cannot be resolved to a variable"
    		 * would appear.
    		 */
    		
    		
    		Panel0 = startPanel0();
    		Panel1 = startPanel1();
    		
    		
    		
    	}
    	
    	/**
    	 * Below are the classes for the panel that will be in the first window (window 0).
    	 * 
    	 * The first class is the class that extends JPanel, meaning that it is a sub-Class of JPanel. 
    	 * 
    	 * It contains the methods for creating inside the Panel0 class a rectangle, setting
    	 * the color to yellow and red (so they can be switched), and it's height and width.
    	 * 
    	 */
    	private static class Panel0 extends JPanel{
    		
    		public void paintComponent (Graphics g){ //you need to set the name of the method as 
    			//paintComponent and the arguments of the method as Graphics, the variable name g
    			//can be named anything.
    			
    			int height = 200;
    			int width = 120; //declare local variables.
    			
    			g.setColor(Color.red); //set color to the global variable color1.
    			g.fillRect(10, 10, height, width); 
    			
    		}
    	}
    	
    	/**
    	 * Below is the class that implements the ACTIONLISTENER.
    	 * 
    	 * This is the class that actually performs the actions that the button will perform,
    	 * changing the colors of the shapes in window1.
    	 */
    	private static class ButtonHandler0 implements ActionListener { //this class implements the actionlistener class,
    		//meaning that you can only specify methods, but not implement them.
    		public void actionPerformed (ActionEvent e){ //YOU NEED TO PUT IN THIS METHOD, OTHERWISE THE WHOLE CLASS WILL NOT WORK.
    			
    			/**
    			 * Below is the if statement that is performed by the actionPerformed method within the ButtonHandler0 class.
    			 * 
    			 * It checks to see if the value inside the variable color1 is the method Color.red, and if it is, it sets 
    			 * it to color.yellow. If it is not Color.red, it sets it back to Color.red. Because there are only two colors 
    			 * that it can be, it flips back and forth between the colors.
    			 * 
    			 */
    			if (color1 == Color.blue){
    				color1 = Color.green;
    			}
    			else {
    				color1 = Color.blue;
    			}
    		}
    	}
    	
    	/**
    	 * Below are the classes that will go into panel1, which is put into the second window (window 2).
    	 * 
    	 * It is essentially the same as the above set of classes, just with different names (all the 0's will be 1's).
    	 * 
    	 * It will handle all the same things as the above classes but for the second window.
    	 * 
    	 */
    	
    private static class Panel1 extends JPanel{
    		
    		public void paintComponent (Graphics g){ 
    			
    			int height = 200;
    			int width = 120; //declare local variables.
    			
    			g.setColor(color0); //Set the color to the global variable color0.
    			g.fillRect(10, 10, height, width); 
    			
    		}
    	}
    	
    	/**
    	 * Below is the class that implements the ACTIONLISTENER.
    	 * 
    	 * This is the class that actually performs the action that the button will perform, which is
    	 * changing the colors of the shapes in window0.
    	 */
    	private static class ButtonHandler1 implements ActionListener { //this class implements the actionlistener class,
    		//meaning that you can only specify methods, but not implement them.
    		public void actionPerformed (ActionEvent e){ //YOU NEED TO PUT IN THIS METHOD, OTHERWISE THE WHOLE CLASS WILL NOT WORK.
    			
    			/**
    			 * Below is the if statement that is performed by the actionPerformed method within the ButtonHandler0 class.
    			 * 
    			 * It checks to see if the value inside the variable color1 is the method Color.blue, and if it is, it sets 
    			 * it to color.green. If it is not Color.blue, it sets it back to Color.green. Because there are only two colors 
    			 * that it can be, it flips back and forth between the colors.
    			 * 
    			 */
    			if (color1 == Color.red){
    				color1 = Color.yellow;
    			}
    			else {
    				color0 = Color.red;
    			}
    		}
    	}
    	
    	/**
    	 * Below are the two methods that implement all of those previous classes into the program.
    	 * 
    	 * They implement the classes created and their methods, as well as set up the JFrame window.
    	 */
    	
    	static JPanel startPanel0(){
    		
    		Panel0 displayPanel = new Panel0(); //Create an instance of the Panel0 class 
    		JButton okButton = new JButton("Change window 2 shape"); //Instance of the JButton class
    		ButtonHandler0 listener = new ButtonHandler0(); //instance of the ButtonHandler0 class
    		okButton.addActionListener(listener); //give the properties of the listener object to the okButton object.
    		
    		JPanel content = new JPanel(); //instance of the JPanel class.
    		content.setLayout(new BorderLayout()); //This is the BorderLayout class, which divides the panel into multiple parts.
    		content.add(displayPanel, BorderLayout.CENTER);//This adds to the content object the object displayPanel, and puts it
    		//in the center of the BorderLayout.
    		content.add(okButton, BorderLayout.SOUTH); //Adds the okButton to the bottom of the borderlayout and into the content object.
    		
    		//This is the creation of the JFrame and the implementation of the JPanel.
    		JFrame window = new JFrame();
    		window.setContentPane(content); //Sets into the JFrame the JPanel instance content.
    		window.setSize(400,400);
    		window.setVisible(true);
    		window.setLocation(200,100);
    		
    		return (displayPanel); //This is what is needed for the method to function. You need to tell the method what information
    		//it needs to send out to the other classes etc. All of the stuff that has been done in the method goes back into 
    		//displayPanel, and then displayPanel is RETURNED to the method startPanel0.
    		
    	}
    	
    static JPanel startPanel1(){
    		
    		Panel1 displayPanel = new Panel1(); //Create an instance of the Panel0 class 
    		JButton okButton = new JButton("Change window 1 shape"); //Instance of the JButton class
    		ButtonHandler1 listener = new ButtonHandler1(); //instance of the ButtonHandler0 class
    		okButton.addActionListener(listener); //give the properties of the listener object to the okButton object.
    		
    		JPanel content = new JPanel(); //instance of the JPanel class.
    		content.setLayout(new BorderLayout()); //This is the BorderLayout class, which divides the panel into multiple parts.
    		content.add(displayPanel, BorderLayout.CENTER);//This adds to the content object the object displayPanel, and puts it
    		//in the center of the BorderLayout.
    		content.add(okButton, BorderLayout.SOUTH); //Adds the okButton to the bottom of the borderlayout and into the content object.
    		
    		//This is the creation of the JFrame and the implementation of the JPanel.
    		JFrame window = new JFrame();
    		window.setContentPane(content); //Sets into the JFrame the JPanel.
    		window.setSize(400,400);
    		window.setVisible(true);
    		window.setLocation(800,100);
    		
    		return (displayPanel); //This is what is needed for the method to function. You need to tell the method what information
    		//it needs to send out to the other classes etc. All of the stuff that has been done in the method goes back into 
    		//displayPanel, and then displayPanel is RETURNED to the method startPanel1.
    		
    	}
    	
    	
    
    }
    the button0 is supposed to switch the color in window1, and button1 is supposed to switch the color in window0.

    Thank you all for helping :)

    EDIT: Is there a special way to put the code into the post? It doesn't indent it the way I indented it in eclipse. I imagine there is and I will look now.
    Last edited by DarrylBurke; 02-14-2015 at 02:48 AM. Reason: Fixed comment delimiters

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    384
    Rep Power
    11

    Default Re: Program to changeto colors of a shape in the other window.

    you can wrap your code in a code tag

  3. #3
    Join Date
    Feb 2015
    Posts
    6
    Rep Power
    0

    Default Re: Program to changeto colors of a shape in the other window.

    Thanks for the code tags.

    EDIT: For some reason, the code on this page looks to be commented out. I cannot find why it's doing that on this page, but I can assure you that the code is not commented out on my computer's version.

    What's happening right now is the two windows pop up, but they do not change each others colors. Everything is inside the windows, but clicking the button doesn't do anything.
    Last edited by DesireToLearnJava; 02-13-2015 at 10:31 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Program to changeto colors of a shape in the other window.

    Can you explain what the code does and what it is supposed to do?
    For example:
    When the program starts frame 1 is green and frame 2 is red. When the button in frame 1 is pressed frame 2 should change color to yellow. Now nothing happens when the button in frame 1 is pressed.

    Suggestion: Get rid of all the static variables and use references to the classes to access their variables.
    Last edited by Norm; 02-13-2015 at 10:56 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Program to changeto colors of a shape in the other window.

    What you need to do is be able to have your button listeners invoke a repaint. Each button listener should do a repaint of the others frame. That means each instance of the Frames must contain references to the other panels. Note that in one listener you use both color1 and color0. In the other, only color1.

    Regards,
    Jim
    Last edited by jim829; 02-13-2015 at 11:19 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Program to changeto colors of a shape in the other window.

    The listeners should not know about the other color variables like it has now with the static variables. It should only have access to the color variable for the panel it is associated with.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Program to changeto colors of a shape in the other window.

    Did you mean switch the colors or change the colors? If you want to switch the color in one panel to that of the other then you need to be able to access each others panel objects. If you just mean change the color, then that is different. I may have misunderstood.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Program to changeto colors of a shape in the other window.

    Quote Originally Posted by DesireToLearnJava View Post
    For some reason, the code on this page looks to be commented out. I cannot find why it's doing that on this page
    Not your fault. The forum syntax highlighter doesn't deal correctly with block comments (/* ...*/). I've changed them to doc comments (/** ... */).

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    18

    Default Re: Program to changeto colors of a shape in the other window.

    Remove all uses of the static keyword - variables, classes, blocks - keeping only a single static main() method.

    Honestly "static" shouldn't be used unless you have a reason, and keeping the compiler from giving you useful messages is not a reason.
    Last edited by pbrockway2; 02-14-2015 at 04:23 AM.

  10. #10
    Join Date
    Feb 2015
    Posts
    6
    Rep Power
    0

    Default Re: Program to changeto colors of a shape in the other window.

    Quote Originally Posted by pbrockway2 View Post
    Remove all uses of the static keyword - variables, classes, blocks - keeping only a single static main() method.

    Honestly "static" shouldn't be used unless you have a reason, and keeping the compiler from giving you useful messages is not a reason.
    I tried to delete static from everything but the main method, and the following error comes up:

    panel0 = startpanel0();
    panel1 = startPanel1(); //This is the error, it exists the same for both. it says "Cannot make a static reference to a non static method startPanel1 from the type JDemo.

    So then when I make startPanel0 ( or startPanel1, respectively) I get and error when I declare an instance of the displayPanel class inside startPanel0 and startPanel1, which says "No inclosing instance of type JDemo is accesible. Must qualify the allocation with an enclosing instance of type JDemo (eg. X.new A() where x is an instance of JDemo". I don't know what this means.

    Quote Originally Posted by jim829 View Post
    Did you mean switch the colors or change the colors? If you want to switch the color in one panel to that of the other then you need to be able to access each others panel objects. If you just mean change the color, then that is different. I may have misunderstood.

    Regards,
    Jim
    The program is supposed to change the red rectangle into a yellow rectangle when the button in window1 is pressed, and when the button in window0 is pressed the blue circle is supposed to change into a green circle.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Program to changeto colors of a shape in the other window.

    Move those two new statements to the JDemo constructor and have one new statement in the main() method that calls the JDemo constructor.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    Join Date
    Feb 2015
    Posts
    6
    Rep Power
    0

    Default Re: Program to changeto colors of a shape in the other window.

    I'm sorry, but I'm unsure of how to do that, how would I do that?

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Program to changeto colors of a shape in the other window.

    Which part don't you understand?
    1)Move those two new statements to the JDemo constructor
    Java Code:
    JDemo() {
      panel0 = startpanel0(); 
      panel1 = startPanel1(); 
    }
    2)Have one statement in the main() method that calls the JDemo constructor: new JDemo().
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: 09-16-2013, 08:51 PM
  2. Trying to build new shape program
    By JavaChipMocha in forum New To Java
    Replies: 5
    Last Post: 10-21-2012, 06:11 PM
  3. Simple shape program will not draw
    By wdh321 in forum New To Java
    Replies: 1
    Last Post: 10-02-2012, 05:33 PM
  4. Creating Callout shape window
    By VROF in forum New To Java
    Replies: 3
    Last Post: 01-25-2012, 09:30 PM
  5. Exponential Shape Program
    By checkmylongboarding in forum New To Java
    Replies: 3
    Last Post: 09-23-2008, 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
  •