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

• January 26th, 2014, 04:49 PM
Solinta
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).

Code java:

```  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) // //   } }```
• January 26th, 2014, 04:57 PM
Norm
Re: First Programme after Hello World - Convex Lens - Why is my code going crazy?
[code=java]
[/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.
• January 26th, 2014, 05:32 PM
Solinta
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
Code java:

``` 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...
• January 26th, 2014, 05:32 PM
Norm
Re: First Programme after Hello World - Convex Lens - Why is my code going crazy?
Quote:

it is still crazy
Sorry, I have no idea what it means for a program to be crazy. Can you explain?
• January 26th, 2014, 05:41 PM
Solinta
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:
Code java:

```if (input()==1){ }```
It asked perpetually for values and nothing worked.
• January 26th, 2014, 06:11 PM
Norm
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?
• January 26th, 2014, 06:41 PM
Solinta
Re: First Programme after Hello World - Convex Lens - Why is my code going crazy?
Code java:

```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
Code java:

```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!
• January 26th, 2014, 06:43 PM
Norm
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.
• January 26th, 2014, 07:12 PM
Solinta
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?
Code java:

```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);```
• January 26th, 2014, 07:56 PM
Norm
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.
• January 27th, 2014, 05:14 AM
GregBrannon
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.
• January 27th, 2014, 11:52 PM
Solinta
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:
Code java:

```//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?
Code java:

```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).
• January 27th, 2014, 11:56 PM
Solinta
Re: First Programme after Hello World - Convex Lens - Why is my code going crazy?
This is the issue:
Attachment 2656
It should look like:
Attachment 2657

Also the issue is occurring whenever I add another page.drawstring("-"); line of code.
• January 27th, 2014, 11:57 PM
Solinta
Re: First Programme after Hello World - Convex Lens - Why is my code going crazy?
Here is my current code:
Code java:

```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) // //   } }```
• January 28th, 2014, 06:25 AM
Norm
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.
• January 29th, 2014, 05:31 AM
Praful Chougale
Re: First Programme after Hello World - Convex Lens - Why is my code going crazy?
do not add input in paint method.
• January 29th, 2014, 01:58 PM
Solinta
Re: First Programme after Hello World - Convex Lens - Why is my code going crazy?
Yup - got that :)
Fixed my problem too - thanks!!!!