variables for this program

hi i been trying to do a basseball inside this rectangle but i cant and also can you tell me how to make this program short by using variables thank you .

this is my code

import java.applet.*;
import java.awt.*;

public class Pruebas extends Applet

{
      public void init()
      
      {
            setBackground(Color.cyan);
      }
      
      public void paint (Graphics g)
      
      {
            g.setColor(Color.red);
            g.fillRect(50,25,75,120);
            g.setColor(Color.white);
            g.drawOval(73,93,50,50);
            g.drawArc(70,96,50,50,50,100);
      }
}


mijangosAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
CEHJCommented:
>>also can you tell me how to make this program short by using variables thank you .

You probably ought to parameterize the coordinates in paint
0
 
petmagdyConnect With a Mentor Commented:
here we go this is the applet:

package examples.tests.applets;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Applet1 extends Applet
{
      int x ;
      int y ;
      int width ;
      int height ;


  public Applet1()
  {
//  get
  }

  public void init()
  {
    try
    {
      jbInit();
      x = Integer.valueOf( getParameter("x")).intValue();
      y = Integer.valueOf( getParameter("y")).intValue();
      width = Integer.valueOf( getParameter("width")).intValue();
      height = Integer.valueOf( getParameter("height")).intValue();
     
     
      setBackground(Color.cyan);
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }

  }

  /**
   *
   * @param args
   */
  public static void main(String[] args)
  {
    Applet1 applet = new Applet1();
    Frame frame = new Frame();
    frame.addWindowListener(new WindowAdapter()
      {
        public void windowClosing(WindowEvent e)
        {
          System.exit(0);
        }
      });
    frame.add(applet, BorderLayout.CENTER);
    frame.setTitle("Applet Frame");
    applet.init();
    applet.start();
    frame.setSize(300, 300);
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = frame.getSize();
    frame.setLocation((d.width - frameSize.width) / 2, (d.height - frameSize.height) / 2);
    frame.setVisible(true);
  }

  public void start()
  {
  }

  public void stop()
  {
  }

  public void destroy()
  {
  }

  private void jbInit() throws Exception
  {
    this.setLayout(null);
  }
 
    public void paint (Graphics g)
     
     {
          g.setColor(Color.red);
          g.fillRect(x, y, width,height);
          g.setColor(Color.white);
          g.drawOval(x, y, width,height);
     }
 
}


/////////////////////////////////////////////////////////////

and this the html file will call the applet:

<HTML>
<HEAD>
<TITLE>
Title
</TITLE>
</HEAD>
<BODY>
<APPLET CODE="examples.tests.applets.Applet1" HEIGHT="500" WIDTH="500" ALIGN="bottom">
<PARAM NAME="width" VALUE="75">
<PARAM NAME="height" VALUE="120">
<PARAM NAME="y" VALUE="25">
<PARAM NAME="x" VALUE="50">This browser does not appear to support Applets.</APPLET>
</BODY>
</HTML>
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
petmagdyCommented:
Also in the same way u can add paramets for the colors if u want
0
 
CEHJCommented:
Are you not intending to animate the graphics?
0
 
mijangosAuthor Commented:
no i just want a basic thing , not to fancy , as am a begginer
0
 
mijangosAuthor Commented:
something really easy not to complicated it as am learning this language
0
 
petmagdyCommented:
mijangos,

 did u saw my comment? did u try it?
0
 
petmagdyCommented:
please not that u can drop the long main method if html file to run is enouph
0
 
mijangosAuthor Commented:
yes i did but for some reason i cant see nothing when i run the programm just a blank thing no rectangle or ovals in the applet
0
 
petmagdyCommented:
did u run the html, don't run applet directly so the parameters are effective

or as a begining put the applet like this:

import java.applet.*;
import java.awt.*;

public class Pruebas extends Applet

{
     public void init()
     
     {
          setBackground(Color.cyan);
     }
     
     public void paint (Graphics g)
     
     {
          g.setColor(Color.red);
          g.fillRect(50,25,75,120);
          g.setColor(Color.white);
          g.drawOval(50,25,75,120);
     }
}

0
 
mijangosAuthor Commented:
i created a whole new project for the one you posted , with the same class name and is not working
0
 
CEHJCommented:
Make it easier for yourself by *not* using a package. Delete the package statement and operate from the current directory
0
 
petmagdyCommented:
Please refer to my last comment simple code I was just pointing out that u call drawOval with the same values as u call fillRect
0
 
mijangosAuthor Commented:
sorry for respond late to your question , i went to school had exams today , but to the question yes i run your applet as you said only the html file and i get just a blank window

0
 
Giant2Commented:
Do you have any error in the java consolle?
0
 
petmagdyCommented:
mijangos I was just pointing out that u call drawOval with the same values as u call fillRect, please just simply try this code first:

import java.applet.*;
import java.awt.*;

public class Pruebas extends Applet

{
     public void init()
     
     {
          setBackground(Color.cyan);
     }
     
     public void paint (Graphics g)
     
     {
          g.setColor(Color.red);
          g.fillRect(50,25,75,120);
          g.setColor(Color.white);
          g.drawOval(50,25,75,120);
     }
}
0
 
mijangosAuthor Commented:
am sorry i accepted the wrong answer i was going to a petmagdy if theres a way to fix and give you the points petmagdy thanks for all the help guys
0
 
petmagdyCommented:
Please mijangos go the the above menu press "support" submit a link to this question and ask them to correct the accepted answer so i can get my points :(
0
 
CEHJCommented:
>>am sorry i accepted the wrong answer

Can you say why posting a properly working applet deserves no points at all mijangos?
0
 
mijangosAuthor Commented:
yes it worth points but i can see just  the applet but i dont see the code which is more important cehj
0
 
petmagdyCommented:
no problems guys

mijangos please go to Support ask a question free of points give them link to this page and ask their correction

I have no problem to split but I deserve to get points because I wrote and tested the sample specially for this question
0
 
mijangosAuthor Commented:
yes thats what am going to do am going to split the points for both of you guys
0
 
CEHJCommented:
>>but i dont see the code which is more important cehj

Naturally i'll give you the code ;-) Here it is:

import java.applet.*;
import java.awt.*;

public class Pruebas extends Applet {

  public void init() {
    setBackground(Color.cyan);
  }


  public void paint(Graphics g) {
    g.setColor(Color.white);
    g.fillRect(50, 50, 75, 75);
    g.setColor(Color.red);
    g.fillOval(50, 50, 75, 75);
  }
}
0
 
CEHJCommented:
8-)
0
All Courses

From novice to tech pro — start learning today.