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 12 of 12

Thread: (Total begginner) simple GUI program nt displayin

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Location
    Nigeria
    Posts
    12
    My Mood
    Cheerful
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Question (Total begginner) simple GUI program nt displayin

    dis code compiles w/o errors and runs buh doesnt produce d desired effect...its supposed to paint lines like d ones in the picture

    Capture.JPG
    pls kindly check it out

    d DrawPanelTest class just runs d program n displays d DrawPanel, d DrawPanel class is d main problem

     
    import java.awt.Graphics;
    import javax.swing.JPanel;
     
    //modified DrawPanel Class for esercise 4.1
    public class DrawPanel extends JPanel
    {
    	public void paintComponent(Graphics g)
    	{
    	 super.paintComponent( g );//pls explain y dis shud ALWAYS be here
     
    	 int width = getWidth(); 
    	 int height = getHeight();
     
    	 int widthCounter = 0;
    	 int heightCounter = 10;
     
     
    	 g.drawLine(0,0,width,height); // found out it painted dis line
     
    	 //mayb am wrong buh i fink d probs 4rm here
    	 while (widthCounter <= 10)
    		{
    		 width = (width*widthCounter)/10;
    		 height = (height*heightCounter)/10;
     
    		 g.drawLine(0,0,width,height);
     
    		 ++widthCounter;
    		 --heightCounter;
    		}
    	}
    }

     
    import javax.swing.JFrame;
     
    public class DrawPanelTest
    {
    	public static void main( String args[] )
    	{
    	 DrawPanel panel = new DrawPanel();
    	 JFrame application = new JFrame();
     
    	 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
     
    	 application.add(panel);
    	 application.setSize( 300,300); 
    	 application.setVisible( true );
    	}
    }
    I AM MONEY B-)


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: (Total begginner) simple GUI program nt displayin

    I thought paintComponent was a protected method.

    ----Edit-----
    It appears to draw a line across the screen of the window. What should it be doing?

    I'm wondering if it's going wrong about here:

     while (widthCounter <= 10)
    		{
    		 width = (width*widthCounter)/10;
    		 height = (height*heightCounter)/10;
     
    		 g.drawLine(0,0,width,height);
     
    		 ++widthCounter;
    		 --heightCounter;
    		}

    width = 0 * widthCounter / 10 = 0;
    height = (getHeight() * 10) /10 = getHeight();
    g.drawLine(0,0, 0, getHeight());

    width = width /10;
    height = (height * 9) / 10;
    g.drawLine(0,0,width/10, (height*9)/10);

    As for why the super should be there, JPanel, while it actually doesn't define a method called paintComponent(Graphics g), it inherits one from its parent, the grandparent of your class, JComponent. JComponent already has a method, though it's a protected method, so you should probably change the type from public to protected, which does the painting. If you leave out super, it will basically be you overriding the JComponent one but not getting any of its features. Calling super enables the paint to get all the conditions that the JComponent paint method has.

    Kind of like this:

     
    public class Square 
    {
     
    private double width;
    private double height;
     
    public Square()
    {
    setWidth(0.0);
    setHeight(0.0);
    }
     
    public Square(double width, double height)
    {
    setWidth(width);
    setHeight(height);
    }
     
    public void setWidth(double width)
    {
    this.width = width;
    }
     
    public double getWidth()
    {
    return width;
    }
     
    public void setHeight(double height)
    {
    this.height = height;
    }
     
    public double getHeight()
    {
    return height;
    }
     
    }

     
    public class Cube extends Square
    {
     
    private double depth;
     
    public Cube()
    {
    super(); // calls the Square default constructor
    setDepth(0.0);
    }
     
    public Cube(double width, double height, double depth)
    {
    super(width, height);
    setDepth(depth);
    }
     
    public void setDepth(double depth)
    {
    this.depth = depth;
    }
     
    public double getDepth()
    {
    return depth;
    }
    }
    Last edited by javapenguin; January 10th, 2012 at 06:07 PM.

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Location
    Nigeria
    Posts
    12
    My Mood
    Cheerful
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: (Total begginner) simple GUI program nt displayin

    yeah, netbeans said sumtin abt it bein overridden
    I AM MONEY B-)

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: (Total begginner) simple GUI program nt displayin

    Quote Originally Posted by javapenguin View Post
    I thought paintComponent was a protected method.
    It is, but you can always widen the visibility of methods being overridden.

    Quote Originally Posted by moneyman021 View Post
    yeah, netbeans said sumtin abt it bein overridden
    It is good practice to annotate methods that you are overriding. ("Overriding" just means that the method is declared in the parent class and you are giving it special behaviour in your class)

    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent( g );//pls explain y dis shud ALWAYS be here
        // etc

    Regarding the comment in that line: If the parent is opaque calling super.paintComponent() will ensure that the background is painted. See the Painting pages in Oracle's Tutorial.

    -----

    You can see what lines are being painted by adding a line of code:

    while (widthCounter <= 10)
    {
        width = (width*widthCounter)/10;
        height = (height*heightCounter)/10;
     
        System.out.println("Drawing line with width=" + width + " height=" + height);
        g.drawLine(0,0,width,height);
     
        ++widthCounter;
        --heightCounter;
    }

    If width and height don't have the values you expect you might think about declaring new variables for the width and height you are going to draw (the ones on the left hand side of the assignments.)

    -----

    Just my 2c, but using standard English in place of d txt spek would have a couple of advantages. In an international forum it would allow non English speakers to follow the posts more easily. And it would convey a sense of seriousness that might encourage people to make the effort to respond.

  5. The Following User Says Thank You to pbrockway2 For This Useful Post:

    moneyman021 (January 10th, 2012)

  6. #5
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: (Total begginner) simple GUI program nt displayin

    Here lies a problem, though I'm not sure it's THE problem.
    int width = getWidth(); 
    int height = getHeight();
     
    int widthCounter = 0;
    int heightCounter = 10;
    //... other code ...
    while (widthCounter <= 10)
    {
    	 width = (width*widthCounter)/10;
    	 height = (height*heightCounter)/10;
     
    	 g.drawLine(0,0,width,height);
     
    	 ++widthCounter;
    	 --heightCounter;
    }
    Think about what happens when Java does this:

    width = (width*widthCounter)/10;
    height = (height*heightCounter)/10;

    First, it substitutes the values (we'll say the width and height are 100, for ease):

    width = (100 * 0) / 10
    height = (100 * 10) / 10

    ...

    width = 0
    height = 100

    If you think about it, now since your width is zero, it will remain zero for the rest of the program because you use multiplication to change its value.
    When I follow height's value, I find...
    90... 72... 50.4 (50)... 30.24 (30)...

    My brain tells me that this pattern will not result in the lines you want drawn. I suggest revising your formula. By the looks of that example, I would suggest trying to avoid multiplication altogether.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  7. The Following User Says Thank You to snowguy13 For This Useful Post:

    moneyman021 (January 10th, 2012)

  8. #6
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: (Total begginner) simple GUI program nt displayin

    Oh! I see what you want! You want ten lines to be drawn total, as fractions of the width of the panel, I think. To do this, you can't just continue using...
    width = (100 * 0) / 10

    Width keeps getting changed, and from the first time you change it, it no longer represents the actual width of the panel. So, when you reassign width's value, you may have to call again the panel's getWidth() method. The same goes for height. I think this would fix your problem.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  9. The Following User Says Thank You to snowguy13 For This Useful Post:

    moneyman021 (January 10th, 2012)

  10. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: (Total begginner) simple GUI program nt displayin

    After some printlns inside the while loop I came up with this:

     

    Width is: 0
    Height is: 262
    Width is: 0
    Height is: 235
    Width is: 0
    Height is: 188
    Width is: 0
    Height is: 131
    Width is: 0
    Height is: 78
    Width is: 0
    Height is: 39
    Width is: 0
    Height is: 15
    Width is: 0
    Height is: 4
    Width is: 0
    Height is: 0
    Width is: 0
    Height is: 0
    Width is: 0
    Height is: 0



  11. #8
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: (Total begginner) simple GUI program nt displayin

    Quote Originally Posted by moneyman021 View Post
    dis [SP] code compiles w/o [Forum posts can be considered formal writing for most purposes, and as such text-speak like abbreviations should be avoided] errors and runs buh [SP] doesnt [SP] produce d [Please see note two] desired effect...its [This form of its is to show possession, you want the contraction it's, meaning it is] supposed to paint lines like d [SP] ones in the picture

    pls [SP] kindly check it out

    d [SP] DrawPanelTest class just runs d [SP] program n [SP] displays d [SP] DrawPanel, d [SP] DrawPanel class is d [SP] main problem
    Think that sums up what everyone was thinking when they read this.

  12. The Following User Says Thank You to Tjstretch For This Useful Post:

    moneyman021 (January 10th, 2012)

  13. #9
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: (Total begginner) simple GUI program nt displayin

    Quote Originally Posted by snowguy13 View Post
    So, when you reassign width's value, you may have to call again the panel's getWidth() method. The same goes for height. I think this would fix your problem.
    I agree. But you could avoid all the getXXX() calls by introducing two new variables for the width/height of the line and keeping width and height as they are for the width/height of the component.

  14. The Following User Says Thank You to pbrockway2 For This Useful Post:

    moneyman021 (January 10th, 2012)

  15. #10
    Junior Member
    Join Date
    Jan 2012
    Location
    Nigeria
    Posts
    12
    My Mood
    Cheerful
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: (Total begginner) simple GUI program nt displayin

    Thanks all of you. It was a logical error...
    My first time in a forum and I was'nt disappointed...Nice!!!
    I AM MONEY B-)

  16. #11
    Junior Member
    Join Date
    Jan 2012
    Location
    Nigeria
    Posts
    12
    My Mood
    Cheerful
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: (Total begginner) simple GUI program nt displayin

    Also, what does the /this/ keyword mean
    I AM MONEY B-)

  17. #12
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: (Total begginner) simple GUI program nt displayin

    Java's this keyword makes a reference to the object that is current being coded.

    It is most commonly used when you have a parameter variable that shadows a field, like this:
    //Let's say name is an object field
    String name;
     
    public void setName(String name) //Parameter is named same thing as field
    {
       //"this" tells the program to set the name variable in THIS object
       //to the value of the parameter name. It makes things more concise.
       this.name = name;
    }

    For more information on the this keywoard, see Oracle's tutorial.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

Similar Threads

  1. Program to calculate pay and total sum with daily increment. Urgent!
    By ePerKar3 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: December 5th, 2011, 01:20 PM
  2. program wont renew total
    By that_guy in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 17th, 2011, 01:00 PM
  3. How to get a System's total CPU usage percentage using a java program
    By userj2ee in forum Java Theory & Questions
    Replies: 2
    Last Post: January 7th, 2011, 01:28 AM
  4. Guessing Game begginner question
    By okupa in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 20th, 2010, 07:31 AM
  5. urgent simple help for a very simple program
    By albukhari87 in forum Java Applets
    Replies: 4
    Last Post: June 5th, 2010, 03:43 PM

Tags for this Thread