Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: HELP! Cant pass my input values to the paint component

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default HELP! Cant pass my input values to the paint component

    I am trying to write a program that accepts user input to draw shapes. Like It would ask what shape do you want , if it was square it would calculate perimeter and give to you along with a pop up window drawing of a square with a drawstring saying this is a square with a perimeter of..... What is below is just a short version to find out how to pass the parameters(a test version). When I can do it I will add a loop to keep asking for shapes and sizes to draw until they hit like 5-To Exit.
    I am new to JAVA and have read and watched an untold number of videos but can't find anything on why I can't pass over the parameters, I think if I get that the rest will be easy. Thanks



    import javax.swing.*;
     
    public class InputTest{
    	public static final double PI=3.14159;
     
    	public static void main(String[] args){
     
    		JFrame frame = new JFrame("Test");
    		frame.setVisible(true);
    		frame.setSize(400,200);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		Square object = new Square();
    		frame.add(object);
     
    		object.drawing();
     
     
    		// Gets the first number
     
    		String firstnum = JOptionPane.showInputDialog("Enter a number");
     
    		// Gets the second number
     
    		String secnum = JOptionPane.showInputDialog("Enter another number");
     
    		int num1 = Integer.parseInt(firstnum);
    		int num2 = Integer.parseInt(secnum);
     
     
    		Square SquareObject = new Square();
    		SquareObject.circumf(num1,num2);
     
     
    	}}
     
    import javax.swing.*;
     
    import java.awt.*;
     
    public class Square extends JPanel{
     
    	public void circumf(int newx1, int  newx2){
     
     
    		int circumf = newx1 * newx2;
    		JOptionPane.showMessageDialog(null,"You selected Rectangle" +
    									  "\n Length is = " + newx1 +"cm "+" Width is " +newx2 +"cm" +
    									  "\n Perimeter is = " + circumf +"cm","RECTANGLE",JOptionPane.PLAIN_MESSAGE);
     
    	}
    	public void drawing(){
    		repaint();
     
    	}
     
     
    	public void paintComponent(Graphics g){
     
    		super.paintComponent(g);
    		g.setColor(Color.BLUE);
    		g.fillRect(10, 15,newx1,newx2);
     
    	}
    }


  2. #2
    Junior Member
    Join Date
    Apr 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: HELP! Cant pass my input values to the paint component

    Seriously, no one has any ideas? Does anyone even know if its possible?

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: HELP! Cant pass my input values to the paint component

    Seriously, no one has any ideas? Does anyone even know if its possible?
    We are not a 24/7 service and there are others who have their own questions that contributors must address as well, so please be patient.

    Based upon the title, you don't pass values to paintComponent - use instance variables. See Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

  4. The Following User Says Thank You to copeg For This Useful Post:

    mindmaster (April 22nd, 2011)

  5. #4
    Junior Member
    Join Date
    Apr 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: HELP! Cant pass my input values to the paint component

    I read it and was able to add a "public static int" with a value, to the class the paint component is in and pass the variable to the paint component in same class, but I still can't get the input which is in a different class to pass into the class the contains the paint component. Is there another swing draw method that would

    Quote Originally Posted by copeg View Post
    We are not a 24/7 service and there are others who have their own questions that contributors must address as well, so please be patient.

    Based upon the title, you don't pass values to paintComponent - use instance variables. See Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

  6. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: HELP! Cant pass my input values to the paint component

    Provide setters to the values of the component you wish to paint (no need to make it static unless it needs to be). For example:
    public void MyComponent extends JPanel{
        private int myValue = 0;
     
        public void setMyValue(int value){
            myValue = value;
        }
     
        @Override
        public void paintComponent(Graphics g){
            //you can use myValue for whatever the need may be
        }
    }

    The other class would then call setMyValue with the appropriate value.

  7. #6
    Junior Member
    Join Date
    Apr 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: HELP! Cant pass my input values to the paint component

    yes that works ,but "myvalue" is only what you set it to. How do you get myvalue to be whatever the user inputs? Like if it was in a loop and said enter length of one side of square:, whatever was entered would become myvalue(say 10) and the paintcomp would draw it 10px by 10px, then loop would ask again til it ends.

    Quote Originally Posted by copeg View Post
    Provide setters to the values of the component you wish to paint (no need to make it static unless it needs to be). For example:
    public void MyComponent extends JPanel{
        private int myValue = 0;
     
        public void setMyValue(int value){
            myValue = value;
        }
     
        @Override
        public void paintComponent(Graphics g){
            //you can use myValue for whatever the need may be
        }
    }

    The other class would then call setMyValue with the appropriate value.

  8. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: HELP! Cant pass my input values to the paint component

    Write something out and see. Use a loop, prompt the user, then set the value...if/when you have a problem post the code, error messages, misbehavior, etc...

  9. #8
    Junior Member
    Join Date
    Apr 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: HELP! Cant pass my input values to the paint component

    Doing something wrong


     
    import java.awt.*;
    import javax.swing.*;
     
     
     
    public class myTest extends JPanel{
     
     
     
    		        public static void main(String[] args){
     
    		            JFrame frame = new JFrame("Test");
    		            frame.setVisible(true);
    		            frame.setSize(400,200);
    		            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		            myTest object = new myTest();
    	                    frame.add(object);
    		            object.drawing(); 
     
     
     
    		            // Gets the first number
     
    		            String firstnum = JOptionPane.showInputDialog("Enter a number");
     
     
     
    		            int myValue = Integer.parseInt(firstnum);
     
     
    		            Square SquareObject = new Square();
    		            SquareObject.perim(myValue);
     
     
    		        }}
     
     
    		         class Square extends JPanel{
     
    		        	 private int myValue=0;
    		       	  public void setmyValue(int select){
    		       		   myValue=select;
    		       	   }
     
    		    	 public void perim(int newx1){
     
    		    	 int perim = 4*newx1;
    		    	 JOptionPane.showMessageDialog(null,"You selected Square" +
    		    	 "\n Side Length is = " + newx1 +"cm "+
    		    	 "\n Perimeter is = " + perim +"cm","SQUARE",JOptionPane.PLAIN_MESSAGE);
     
    		    	 }
    		    	 public void drawing(){
    		    	 repaint();
     
    		    	 }
     
     
     
    		    	 public void paintComponent(Graphics g){
    		    	 super.paintComponent(g);
    		    	 g.setColor(Color.BLUE);
    		    	 g.fillRect(10, 15,myValue,myValue);
     
    		    	 }
    		    	 }
    Last edited by mindmaster; April 22nd, 2011 at 05:45 PM. Reason: added missed code

Similar Threads

  1. paintComponent() instead of paint()
    By Ciaran54 in forum AWT / Java Swing
    Replies: 1
    Last Post: February 22nd, 2011, 02:46 PM
  2. 'pass by value' and 'pass by reference'
    By pokuri in forum Object Oriented Programming
    Replies: 5
    Last Post: January 26th, 2011, 11:30 AM
  3. Paint only part of screen
    By bonus_clip in forum AWT / Java Swing
    Replies: 7
    Last Post: October 16th, 2010, 07:21 PM
  4. JTable Updating String Values from User Input
    By aussiemcgr in forum AWT / Java Swing
    Replies: 5
    Last Post: August 3rd, 2010, 01:48 PM
  5. Values of Input
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 8th, 2009, 03:46 AM