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

Thread: First Programme after Hello World - Convex Lens - Why is my code going crazy?

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    9
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Question First Programme after Hello World - Convex Lens - Why is my code going crazy?

    It was all going smooth until I added the equations so that it could calculate the lines for ray3 on its own... Then it started looping the input
    and returning only a single value for both y1 and y2. Any ideas? Also - I suspect my style is super crappy, but I couldn't figure out how to
    carry variables between methods (adding something like (int amount) into the argument wasn't working for me).

     
    import javax.swing.*;
    import java.awt.*;
     
    import java.util.Scanner;
     
     
    public class Heart extends JFrame{
     
     
     
      public static void main(String[]args){
     
        Heart frame = new Heart();  
        frame.setSize(800,640);
        frame.setVisible(true);
      }
     
     
     
     
      public void paint (Graphics page)
      {
        System.out.println("Enter positive distance from lens (in cm):"); 
        Scanner keyboard = new Scanner(System.in);
        int amount = keyboard.nextInt();
        final int MID = 150;
        final int TOP = 50;
        setTitle("Convex lens");
        int x;
        int x1;
        int x2;
        int y1;
        int y2;
        x=0;
        x1=0;
        x2=600;
        y1=0;
        y2=0;
        if (amount==1){
          x=250;
          //y1=-50;
          //y2=550;
        }
        if (amount==2){
          x=200;
          //y1=75;
          //y2=375;
          }
        if (amount==3){
          x=150;}
        if (amount==4){
          x=100;}
        if (amount==5){
          x=50;}
        if (amount==6){
          x=0;}
        y1 = ((-50/(400-x))*(x1 - x)) - 200;
        y2 = ((-50/(400-x))*(x2 - x)) - 200;
        System.out.println(y1);
        System.out.println(y2);
        //lens
        page.setColor (Color.blue);
        page.fillArc (300, 150, 50, 200,90,180);  // sun
        //page.fillArc (300, 150, 50, 200,270,180);
        //Focal Point
        page.setColor (Color.red);
        page.drawOval(400,245,10,10);
        //Object  
        page.drawLine(x, 250, x, 200);
        //Normal
        page.drawLine(10, 250, 600, 250);
        //Scale
        page.setColor(Color.green);
        page.drawLine(250, 250, 250, 240);
        page.setColor(Color.green);
        page.drawLine(200, 250, 200, 240);
        page.setColor(Color.green);
        page.drawLine(150, 250, 150, 240);
        page.setColor(Color.green);
        page.drawLine(100, 250, 100, 240);
        page.setColor(Color.green);
        page.drawLine(50, 250, 50, 240);
        page.setColor(Color.green);
        page.drawLine(0, 250, 0, 240);
        //ray1
        page.setColor(Color.yellow);
        page.drawLine(x, 200, 300, 200);
        //ray2
        page.setColor(Color.yellow);
        page.drawLine(0, 65, 600, 335);
        //ray3
        page.drawLine(x1,y1,x2,y2);
     
        //ray1calc  - intersect at (300,200)
        //
        //
     
      }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,618
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: First Programme after Hello World - Convex Lens - Why is my code going crazy?

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    The code to get user input should NOT be in the paint() method. The paint() method can be called many times when the gui needs to be redrawn.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Solinta (February 6th, 2014)

  4. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    9
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: First Programme after Hello World - Convex Lens - Why is my code going crazy?

    I'm confused as how to do this - I shifted the input to this but it is still crazy
     public static int input(){
        System.out.println("Enter positive distance from lens (in cm):");
        Scanner keyboard = new Scanner(System.in);
        int amount = keyboard.nextInt();
        return amount;
      }


    --- Update ---

    But it shouldn't be being redrawn... Nothing is conditional on anything that can change once I run it...

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,618
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: First Programme after Hello World - Convex Lens - Why is my code going crazy?

    it is still crazy
    Sorry, I have no idea what it means for a program to be crazy. Can you explain?
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    Solinta (February 6th, 2014)

  7. #5
    Junior Member
    Join Date
    Jan 2014
    Posts
    9
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: First Programme after Hello World - Convex Lens - Why is my code going crazy?

    Sorry! If I enter 1 as my input it works fine, only asks me to enter another input (and if I do, that doesn't work). However, for any value over 1, Ray3 goes into the completely wrong place.

    --- Update ---

    Also - that's with my input still in paint. When I took it out and passed it to my if statements:
    if (input()==1){
    }
    It asked perpetually for values and nothing worked.

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,618
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: First Programme after Hello World - Convex Lens - Why is my code going crazy?

    Sorry, I don't understand what changes have been made to the code. Can you post the current version of the code that shows the problem?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Solinta (February 6th, 2014)

  10. #7
    Junior Member
    Join Date
    Jan 2014
    Posts
    9
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: First Programme after Hello World - Convex Lens - Why is my code going crazy?

    import javax.swing.*;
    import java.awt.*;
     
    import java.util.Scanner;
     
     
    public class Heart extends JFrame{
     
     
     
      public static void main(String[]args){
     
        Heart frame = new Heart();  
        frame.setSize(800,640);
        frame.setVisible(true);
      }
     
     
     
     
      public void paint (Graphics page)
      {
        System.out.println("Enter positive distance from lens (in cm):"); 
        Scanner keyboard = new Scanner(System.in);
        int amount = keyboard.nextInt();
        final int MID = 150;
        final int TOP = 50;
        setTitle("Convex lens");
        int x;
        int x1;
        int x2;
        int y1;
        int y2;
        x=0;
        x1=0;
        x2=600;
        y1=0;
        y2=0;
        if (amount==1){
          x=250;
          //y1=-50;
          //y2=550;
        }
        if (amount==2){
          x=200;
          //y1=75;
          //y2=375;
          }
        if (amount==3){
          x=150;}
        if (amount==4){
          x=100;}
        if (amount==5){
          x=50;}
        if (amount==6){
          x=0;}
        System.out.println(x);
        y1 = -1*(((-50/(300-x))*(-x)) - 200);
        y2 = -1*(((-50/(300-x))*(600 - x)) - 200);
        System.out.println(y1);
        System.out.println(y2);
        //lens
        page.setColor (Color.blue);
        page.fillArc (300, 150, 50, 200,90,180);  // sun
        //page.fillArc (300, 150, 50, 200,270,180);
        //Focal Point
        page.setColor (Color.red);
        page.drawOval(400,245,10,10);
        //Object  
        page.drawLine(x, 250, x, 200);
        //Normal
        page.drawLine(10, 250, 600, 250);
        //Scale
        page.setColor(Color.green);
        page.drawLine(250, 250, 250, 240);
        page.setColor(Color.green);
        page.drawLine(200, 250, 200, 240);
        page.setColor(Color.green);
        page.drawLine(150, 250, 150, 240);
        page.setColor(Color.green);
        page.drawLine(100, 250, 100, 240);
        page.setColor(Color.green);
        page.drawLine(50, 250, 50, 240);
        page.setColor(Color.green);
        page.drawLine(0, 250, 0, 240);
        //ray1
        page.setColor(Color.yellow);
        page.drawLine(x, 200, 300, 200);
        //ray2
        page.setColor(Color.yellow);
        page.drawLine(0, 65, 600, 335);
        //ray3
        page.drawLine(x1,y1,x2,y2);
     
        //ray1calc  - intersect at (300,200)
        //
        //
     
      }
    }


    --- Update ---

    Main issue appears to be that this piece of code
    y1 = -1*(((-50/(300-x))*(-x)) - 200);
        y2 = -1*(((-50/(300-x))*(600 - x)) - 200);
    is returning the wrong values - but I cannot see why. If I enter 2, it returns y1=200, y2=200. Same if I enter any value but 1. Is it because I am using integers and not doubles?! Ah!

  11. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,618
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: First Programme after Hello World - Convex Lens - Why is my code going crazy?

    The posted code still has code in the paint() method that is reading input from the user. That code should be moved out of the paint() method.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Solinta (February 6th, 2014)

  13. #9
    Junior Member
    Join Date
    Jan 2014
    Posts
    9
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: First Programme after Hello World - Convex Lens - Why is my code going crazy?

    Is there any chance you could show me how to do this? My every attempt is ending in failure...

    --- Update ---

    Also I recently added this to my code - it works fine, but for some reason it seems to be acting like 'prt sc' (print screen) and copies all images from the window behind onto my canvas. Any idea why this is?
    Graphics2D g2 = (Graphics2D)page;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
            Font font = new Font("Serif", Font.PLAIN, 10);
            g2.setFont(font);
     
            g2.drawString("Focal Point", 400, 270);

  14. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,618
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: First Programme after Hello World - Convex Lens - Why is my code going crazy?

    Create a constructor and move the user input code there. Make amount a class instance variable so both the constructor and paint() method can access it.
    If you don't understand my answer, don't ignore it, ask a question.

  15. The Following User Says Thank You to Norm For This Useful Post:

    Solinta (February 6th, 2014)

  16. #11
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,315
    My Mood
    Mellow
    Thanks
    194
    Thanked 670 Times in 657 Posts

    Default Re: First Programme after Hello World - Convex Lens - Why is my code going crazy?

    And if this is truly your first program after HelloWorld.java, it's too soon. You need to invest some time in yourself to learn programming basics before attempting a graphical user interface. If not, you're going to get discouranged and give up (for now). Get a good book or study reference that begins at the beginning and work through it until it presents graphics, typically in the latter half of the book.

  17. The Following User Says Thank You to GregBrannon For This Useful Post:

    Solinta (February 6th, 2014)

  18. #12
    Junior Member
    Join Date
    Jan 2014
    Posts
    9
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: First Programme after Hello World - Convex Lens - Why is my code going crazy?

    I have been crashing a number of lectures at the university. I would read the textbook I bought, only a friend is using it. I have done what you recommended, which has made my programme MUCH tidier - however I'm still having my issue of it printing everything behind it onto the canvas. This only happens when I include a single line of code:
    //page.drawString("Focal Point", 400, 270);
        page.drawString("Focal Point", 200, 270);
        page.drawString("Radius of Curvature", 100, 280);
        //page.drawString("Radius of Curvature", 500, 280);

    what I have above works fine - but this?
    page.drawString("Focal Point", 400, 270);
        page.drawString("Focal Point", 200, 270);
        page.drawString("Radius of Curvature", 100, 280);
        page.drawString("Radius of Curvature", 500, 280);
    does the strange print thing.
    Now I actually asked the lecturer about it today - and he took a good look at my code before admitting that in all his years of programming he had never seen the like, that my code was fine, and that he had absolutely no clue why it was happening...

    Yes it is my first programme after Hello World - however I have done quite a lot of python and believe that I know enough to have this functional (it nearly is).

  19. #13
    Junior Member
    Join Date
    Jan 2014
    Posts
    9
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: First Programme after Hello World - Convex Lens - Why is my code going crazy?

    This is the issue:
    Untitled.jpg
    It should look like:
    Untitleda.jpg

    Also the issue is occurring whenever I add another page.drawstring("-"); line of code.

  20. #14
    Junior Member
    Join Date
    Jan 2014
    Posts
    9
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: First Programme after Hello World - Convex Lens - Why is my code going crazy?

    Here is my current code:
    import javax.swing.*;
    import java.awt.*;
    import java.util.Scanner;
     
     
    public class Heart extends JFrame{
     
      static int amount;
     
      public static void main(String[]args){
     
        Heart frame = new Heart();  
        frame.setSize(800,640);
        frame.setVisible(true);
      }
     
      Heart(){
     
        System.out.println("Enter positive distance from lens (in cm) - Enter one of these values: 1, 2, 3, 4, 5 or 6:"); 
        Scanner keyboard = new Scanner(System.in);
        amount = keyboard.nextInt();
      }
     
     
      public void paint (Graphics page)
      {
        final int MID = 150;
        final int TOP = 50;
        setTitle("Convex lens");
     
        double x;
        double x1;
        double x2;
        double y1;
        double y2;
        double yr1;
        double yr2;
        double ox;
        double o;
        int oxo;
        int oy;
     
        int y11;
        int y22;
        int x11;
        int x22;
        x=0;
        ox=0;
        x1=0;
        x2=800;
        y1=0;
        y2=0;
        y11=0;
        y22=0;
        if (Heart.amount==1){
          x=250;
          System.out.println("Image is virtual, magnified and upright");}
        if (Heart.amount==2){
          x=200;
          System.out.println("Image is virtual, magnified and upright");}
        if (Heart.amount==3){
          x=150;
          System.out.println("Image is real, magnified and inverted");}
        if (Heart.amount==4){
          x=100;
          System.out.println("Image is real, magnified and inverted");}
        if (Heart.amount==5){
          x=50;
          System.out.println("Image is real, diminished and inverted");}
        if (Heart.amount==6){
          x=0;
          System.out.println("Image is real, diminished and inverted");}
        //System.out.println(x);
        //ray1calc
        y1 = -1*(((-50/(300-x))*(-x)) - 200);
        y2 = -1*(((-50/(300-x))*(800 - x)) - 200);
        //ray2calc
        //yr1 = -1*(((-45.0/(100.0))*(-300.0)) - 250.0);
        //yr2 = -1*(((-45.0/(100.0))*(800.0-300.0)) - 250.0);
     
     
     
        y11 = (int) y1;
        y22 = (int) y2;
        int xo = (int) x;
        x11 = (int) x1;
        x22 = (int) x2;
        //yr11 = (int) yr1;
        //yr22 = (int) yr2;
     
     
        //intersect calc
        ox = (((-0.45*x) + 85)/((-50/(300-x))) + x);
        oxo = (int) ox;
        o = -1*(((-50/(300-x))*(ox - x)) - 200);
        oy = (int) o;
        page.drawOval(oxo,oy,10,10);
     
        //System.out.println(y1);
        //System.out.println(y2);
        //lens
        page.setColor (Color.blue);
        page.fillArc (300, 150, 50, 200,90,180);  // sun
        //page.fillArc (300, 150, 50, 200,270,180);
        //Focal Point
        page.setColor (Color.red);
        page.drawOval(400,245,10,10);
        page.drawOval(200,245,10,10);
        //Radius of Curvature
        page.fillOval(500,245,10,10);
        page.fillOval(100,245,10,10);
     
        //Graphics2D g2 = (Graphics2D)page;
           //g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                //RenderingHints.VALUE_ANTIALIAS_ON);
            //Font font = new Font("Serif", Font.PLAIN, 10);
            //g2.setFont(font);
     
            //g2.drawString("Focal Point", 400, 270); 
     
     
        //WORDS
     
        //page.drawString("Focal Point", 400, 270);
        page.drawString("Focal Point", 200, 270);
        page.drawString("Radius of Curvature", 100, 280);
        //page.drawString("Radius of Curvature", 500, 280);
     
     
        //Object  
        page.drawLine(xo, 250, xo, 200);
        //Normal
        page.drawLine(10, 250, 600, 250);
        //Scale
        page.setColor(Color.green);
        page.drawLine(250, 250, 250, 240);
        page.setColor(Color.green);
        page.drawLine(200, 250, 200, 240);
        page.setColor(Color.green);
        page.drawLine(150, 250, 150, 240);
        page.setColor(Color.green);
        page.drawLine(100, 250, 100, 240);
        page.setColor(Color.green);
        page.drawLine(50, 250, 50, 240);
        page.setColor(Color.green);
        page.drawLine(0, 250, 0, 240);
        //ray1
        page.setColor(Color.orange);
        page.drawLine(xo, 200, 300, 200);
        //ray2
        page.setColor(Color.yellow);
        page.drawLine(0, 65, 600, 335);
        //Write equation for this
     
        //ray3
        page.drawLine(x11,y11,x22,y22);
     
        //ray1calc  - intersect at (300,200)
        //
        //
     
      }
    }

  21. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,618
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: First Programme after Hello World - Convex Lens - Why is my code going crazy?

    The background needs to be cleared before drawing the image. Calling the super.paint() method will do that.
    If you don't understand my answer, don't ignore it, ask a question.

  22. The Following User Says Thank You to Norm For This Useful Post:

    Solinta (February 6th, 2014)

  23. #16
    Junior Member
    Join Date
    Jan 2014
    Posts
    23
    My Mood
    Mellow
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: First Programme after Hello World - Convex Lens - Why is my code going crazy?

    do not add input in paint method.

  24. The Following User Says Thank You to Praful Chougale For This Useful Post:

    Solinta (February 6th, 2014)

  25. #17
    Junior Member
    Join Date
    Jan 2014
    Posts
    9
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: First Programme after Hello World - Convex Lens - Why is my code going crazy?

    Yup - got that
    Fixed my problem too - thanks!!!!

Similar Threads

  1. Going crazy
    By rmedford in forum Object Oriented Programming
    Replies: 2
    Last Post: September 15th, 2012, 02:39 PM
  2. its crazy
    By chonch in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 17th, 2011, 02:26 PM
  3. I'm sure it is really simple but it is driving me crazy
    By Ademske in forum AWT / Java Swing
    Replies: 2
    Last Post: January 17th, 2011, 06:41 AM
  4. Crazy array
    By javapenguin in forum What's Wrong With My Code?
    Replies: 18
    Last Post: June 26th, 2010, 03:02 AM
  5. [SOLVED] NullPointerException in Poker's program
    By dean_martin in forum Exceptions
    Replies: 10
    Last Post: April 21st, 2009, 06:40 AM

Tags for this Thread