Jump to content

Need Simple Java Code Help

Go to solution Solved by elpiop,

try 

 myapp.drawString(name,0,10);

0,0 is drawing it in the top left corner outside of the window 

Im having trouble with my java code and am making an applet for class that is just supposed to make a simple picture. I have everything working and compiled but it needs to have my name printed in the top left corner and for some reason I cant get it to work. The last 3 lines of actual code is what I need help with as I just cant get it to print the name. 

 

import javax.swing.JApplet;
import java.awt.*;
public class PP2_18 extends JApplet
{
  public void paint(Graphics myapp)
  {
    myapp.setColor(Color.white);
    myapp.fillRect(0,0,5000,5000);
    
    Color grassGreen = new Color(0,255,85);//Made a new sky and grass color. Others were too bright.
    Color skyBlue = new Color(0,200,255);
    
    myapp.setColor(skyBlue);
    myapp.fillRect(0,0,5000,5000);
    
    myapp.setColor(grassGreen);
    myapp.fillRect(0,450,5000,5000);
    
    myapp.setColor(Color.lightGray);
    myapp.fillRect(285,275,200,200);//Main part of the house
    
    myapp.setColor(Color.black);
    myapp.fillRect(360,400,50,75);//The Door
    
    myapp.setColor(Color.white);
    myapp.fillOval(400,440,10,10);//The Door handle
    
    myapp.setColor(Color.white);//Window Frames
    myapp.fillRect(300,320,50,60);
    myapp.fillRect(415,320,50,60);
    
    myapp.setColor(Color.black);//Window frame crossbars
    myapp.fillRect(320,320,5,60);
    myapp.fillRect(440,320,5,60);
    myapp.fillRect(300,350,50,5);
    myapp.fillRect(415,350,50,5);
    
    myapp.setColor(Color.lightGray);//Chimney Smoke. Put smoke before chimney so it didnt overlap chimney.
    myapp.fillOval(430,120,50,75);
    myapp.fillOval(450,100,50,70);
    myapp.fillOval(470,80,50,70);
    
    myapp.setColor(Color.red);//Chimney
    myapp.fillRect(425,190,50,75);
    
    myapp.setColor(Color.black);//Roof
    myapp.fillRect(285,250,200,40);//Layer 1
    myapp.fillRect(310,230,150,40);//Layer 2
    myapp.fillRect(340,210,100,40);//Layer 3
    
    myapp.setColor(Color.lightGray);//Clouds
    myapp.fillOval(800,100,200,100);
    myapp.fillOval(900,150,200,100);
    myapp.fillOval(900,90,200,100);//Last line for cloud 1(Left)
    
    myapp.fillOval(2000,180,200,100);
    myapp.fillOval(2100,180,200,100);
    myapp.fillOval(2100,130,200,100);//Last line of cloud 2(Right)
    
    Color brown = new Color(165,42,42);//Made brown to use on the tree
    myapp.setColor(brown);
    myapp.fillRect(1300,500,100,400);
    
    Color leafGreen = new Color(0,155,0);//Made green for the tree leaves
    myapp.setColor(leafGreen);
    myapp.fillOval(1150,500,200,150);
    myapp.fillOval(1250,400,200,150);
    myapp.fillOval(1350,500,200,150);
    myapp.fillOval(1150,450,200,150);
    myapp.fillOval(1250,500,200,150);
    myapp.fillOval(1400,500,200,150);
    myapp.fillOval(1350,400,200,150);
    
    myapp.setColor(brown);
    myapp.fillOval(2310,810,60,75);//Outer part of turtle legs
    myapp.fillOval(2475,810,60,75);
    myapp.fillOval(2295,690,260,170);//Outer part of turtle body
    
    myapp.setColor(leafGreen);
    myapp.fillOval(2300,700,250,150);//Turtle Body
    myapp.fillOval(2230,700,100,100);//Turtle Head
    myapp.fillOval(2315,810,50,65);//Inner part of turtle leg
    myapp.fillOval(2480,810,50,65);//Inner part of turtle leg
    
    myapp.setColor(Color.black);
    myapp.fillOval(2250,720,10,10);//Turtle eye
    myapp.fillRect(2235,765,30,5);//Turtle mouth
    
    myapp.setColor(brown);
    myapp.fillOval(2350,720,50,30);//Turtle spots
    myapp.fillOval(2370,780,50,30);
    myapp.fillOval(2450,740,50,30);
    myapp.fillOval(2430,750,50,30);
    myapp.fillOval(2430,790,50,30);
    
    myapp.setColor(Color.black);
    String name = new String("Name");
    myapp.drawString(name,0,0);
  }
}

 

If you could help that would be great, thanks

Edited by iamdarkyoshi
Formatted code
Link to comment
https://linustechtips.com/topic/837342-need-simple-java-code-help/
Share on other sites

Link to post
Share on other sites

Just now, elpiop said:

try 


 myapp.drawString(name,0,10);

 

drawstring is depreciated 

and so is java swing

 

 

if you can please use something modern

 

OFF TOPIC: I suggest every poll from now on to have "**CK EA" option instead of "Other"

Link to post
Share on other sites

Sweet thank you it worked. Didn't work the other day when i tried it for some reason. Also yea i would use something more up to date but were required to use whats in the certain chapters of the book. I'm sure eventually ill start using something more relevant but in the meantime I'm stuck with some out of date methods as well as Dr. Java which has quite a few problems and is a little out dated.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×