Results 1 to 12 of 12
  1. #1
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Program is not doing what I want it to

    Hey, I made an extremely basic program (for you guys), but it took me a while because I am learning. I try to make the words cross, as in an x, but instead, it just displays the code part of the first class.I want it to cross as in an X. Why isn't it doing so? Here is the code, and thank you!


    Java Code:
    import javax.swing.*;
    import java.awt.*;
    class Loop extends JFrame{
    int x = 30;
    int y = 50;
     Loop() {
         setTitle("Loop");
        setSize(1000,1000);
       setVisible(true);
      setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    public void paint(Graphics g){
    while(y<1000 && x<1000){
    g.drawString("This is a loop.", x, y);
    y = y +15;
    x= x + 15;
        }
    }
    public static void main(String[] args) {
    new Loop();
        }
    }
    
    
    class Pic extends JFrame{
    int z = 950;
    int v = 950;
    public void paint(Graphics g){
       while(v> 1 && z >1){
        g.drawString("This is a loop.", v, z);
        z = z - 15;
        v= v - 15;
           }
       }
    }
    Thank you very much in advanced.

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,042
    Rep Power
    20

    Default Re: Program is not doing what I want it to

    Format your code correctly so it is easy to read. Proper indenting.

    Next, do not override paint(). You should be overriding paintComponent().
    Indeed you should not be extending JFrame in the first place.
    Extend JPanel, then add that to an ordinary JFrame.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: Program is not doing what I want it to

    Quote Originally Posted by Tolls View Post
    Format your code correctly so it is easy to read. Proper indenting.

    Next, do not override paint(). You should be overriding paintComponent().
    Indeed you should not be extending JFrame in the first place.
    Extend JPanel, then add that to an ordinary JFrame.
    Hi Tolls. I'm confused as to how to do this. Would I use extend JPanel and then how do i go about making a panel?

  4. #4
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: Program is not doing what I want it to

    Hi Tolls. I did what you requested in the code.
    Here it is:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    class Loop extends JFrame  {
    int x = 30;
    int y = 50;
     Loop() {
    setTitle("Loop");
    setSize(1000,1000);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
     }
    public void paint(Graphics g) {
    while(y<1000 && x<1000) {
    g.drawString("This is a loop.", x, y);
    y = y +15;
    x= x + 15;
         }
    }
    public static void main(String[] args) {
        new Loop();
         }
    }
    
    
    class Pic extends JPanel{
      int z = 950;
      int v = 950;
      public void paintComponent(Graphics g){
      while(v > 1 && z > 1){
    g.drawString("This is a loop.", v, z);
    z = z - 15;
    v= v - 15;
          }
       }
    }

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,042
    Rep Power
    20

    Default Re: Program is not doing what I want it to

    You need to format your code properly.
    That is really hard to read and follow the flow without proper indentation.

    You aren't using the Pic class anywhere.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,565
    Blog Entries
    7
    Rep Power
    21

    Default Re: Program is not doing what I want it to

    Quote Originally Posted by MW130 View Post
    Hi Tolls. I did what you requested in the code.
    Here it is: [ snip ]
    According to your own post in the Lobby section:

    Quote Originally Posted by MW130 View Post
    I understand methods, constructors, swing, and all the basics... What do you guys think about this? Thanks for all opinions.
    I must ask: who or what created the code in this thread; your hamster?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: Program is not doing what I want it to

    Quote Originally Posted by JosAH View Post
    According to your own post in the Lobby section:



    I must ask: who or what created the code in this thread; your hamster?

    kind regards,

    Jos
    No. I don't have a hamster, and even if I did, I doubt it would be able to learn Java. On a serious note, can you please tell me what to do correctly, how would I set up a panel? Please don't link me to Oracle's panel tutorial, because they are not well-explained. Thanks.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Program is not doing what I want it to

    Quote Originally Posted by MW130 View Post
    No. I don't have a hamster, and even if I did, I doubt it would be able to learn Java. On a serious note, can you please tell me what to do correctly, how would I set up a panel? Please don't link me to Oracle's panel tutorial, because they are not well-explained. Thanks.
    Those tutorials are how most of us learned Swing and Java, and the more you use them, the easier they become to use. We've linked you to them for a reason as they explain why you should not draw on JFrames. In fact, I thought that we had this discussion long ago.

  9. #9
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: Program is not doing what I want it to

    Quote Originally Posted by Fubarable View Post
    Those tutorials are how most of us learned Swing and Java, and the more you use them, the easier they become to use. We've linked you to them for a reason as they explain why you should not draw on JFrames. In fact, I thought that we had this discussion long ago.
    Hi Furable. I know :P. I was just saying that, I don't quite understand the java tutorial on using panels :( ... I understand how to make the panels, but not where I should declare them, and how to set their size. I don't really get what they are in general... Is there another tutorial (other than the oracle one?) Thank you

  10. #10
    tamilarasi is offline Member
    Join Date
    Nov 2012
    Location
    India
    Posts
    70
    Rep Power
    0

    Default Re: Program is not doing what I want it to

    Hi,

    I don't know what you want do it exactly.
    But you only extends Jframe that is not enough you also call Jpanel for call the paint method.

    I did some changes in your code. I have given that code below.. So Have you look on that..

    ***********/ Code / ****************

    [ crappy, unformatted, spoonfeeding attempting code delete -mod]

    Thanks,
    Last edited by JosAH; 01-25-2013 at 11:20 AM.

  11. #11
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,042
    Rep Power
    20

    Default Re: Program is not doing what I want it to

    Swing consists of containers and things you put in a container.
    A JFrame is a container that holds a JPanel (actually slightly more than that, but we'll just stick to the basics) and controls the display of the application as a whole...it's the window the app appears in. A JPanel is a container that holds your other widgets (including other JPanels) and is used to structure the layout of those widgets. Various subclasses of JPanel provide additional functionality (eg JScrollPane), but are still essential panels at heart.

    That's it in a nutshell.

    So you create a JFrame (don't create your own subclass of it) then either add a JPanel to it, or use the default one. Then add widgets to the JPanel.

    In your case you probably just want to add an instance of your Pic JPanel subclass to it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,042
    Rep Power
    20

    Default Re: Program is not doing what I want it to

    Quote Originally Posted by tamilarasi View Post
    Hi,

    I don't know what you want do it exactly.
    But you only extends Jframe that is not enough you also call Jpanel for call the paint method.

    I did some changes in your code. I have given that code below.. So Have you look on that..
    Please do not provide solutions like that. We don't spoonfeed here.
    In addition that is poor practice. You do not override the paint() method. That's what paintComponent() is for.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 4
    Last Post: 11-21-2012, 01:19 PM
  2. Replies: 2
    Last Post: 04-02-2011, 12:57 PM
  3. Replies: 13
    Last Post: 03-22-2011, 06:49 AM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •