Solved

variables for this program

Posted on 2004-10-21
185 Views
Last Modified: 2010-03-31
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);
      }
}


0
Question by:mijangos
    25 Comments
     
    LVL 86

    Expert Comment

    by:CEHJ
    >>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
     
    LVL 13

    Assisted Solution

    by:petmagdy
    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
     
    LVL 13

    Expert Comment

    by:petmagdy
    Also in the same way u can add paramets for the colors if u want
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    Are you not intending to animate the graphics?
    0
     

    Author Comment

    by:mijangos
    no i just want a basic thing , not to fancy , as am a begginer
    0
     

    Author Comment

    by:mijangos
    something really easy not to complicated it as am learning this language
    0
     
    LVL 13

    Expert Comment

    by:petmagdy
    mijangos,

     did u saw my comment? did u try it?
    0
     
    LVL 13

    Expert Comment

    by:petmagdy
    please not that u can drop the long main method if html file to run is enouph
    0
     

    Author Comment

    by:mijangos
    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
     
    LVL 13

    Expert Comment

    by:petmagdy
    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
     

    Author Comment

    by:mijangos
    i created a whole new project for the one you posted , with the same class name and is not working
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    Make it easier for yourself by *not* using a package. Delete the package statement and operate from the current directory
    0
     
    LVL 13

    Expert Comment

    by:petmagdy
    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
     

    Author Comment

    by:mijangos
    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
     
    LVL 12

    Expert Comment

    by:Giant2
    Do you have any error in the java consolle?
    0
     
    LVL 13

    Expert Comment

    by:petmagdy
    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
     
    LVL 86

    Accepted Solution

    by:
    0
     

    Author Comment

    by:mijangos
    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
     
    LVL 13

    Expert Comment

    by:petmagdy
    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
     
    LVL 86

    Expert Comment

    by:CEHJ
    >>am sorry i accepted the wrong answer

    Can you say why posting a properly working applet deserves no points at all mijangos?
    0
     

    Author Comment

    by:mijangos
    yes it worth points but i can see just  the applet but i dont see the code which is more important cehj
    0
     
    LVL 13

    Expert Comment

    by:petmagdy
    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
     

    Author Comment

    by:mijangos
    yes thats what am going to do am going to split the points for both of you guys
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    >>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
     
    LVL 86

    Expert Comment

    by:CEHJ
    8-)
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Learn The Basics of Ethical Hacking & Pen Testing

    Computer and network security is one of the fastest growing and most essential industries in technology, meaning companies will pay big bucks for ethical hackers. This is the perfect course to leap into this lucrative career, learning how to use ethical hacking to reveal ...

    Suggested Solutions

    1. Package the applet into a JAR file. The applet must be in a JAR file before a certificate can be attached to it. Use the jar JDK utility. If the applet was previously referenced with the help of a codebase attribute in  tag, replace the codebase …
    Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now