Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

variables for this program

Posted on 2004-10-21
26
Medium Priority
?
226 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
Comment
Question by:mijangos
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 8
  • 7
  • +1
26 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12374197
>>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
petmagdy earned 1400 total points
ID: 12374758
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
ID: 12374771
Also in the same way u can add paramets for the colors if u want
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

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

Author Comment

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

Author Comment

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

Expert Comment

by:petmagdy
ID: 12374969
mijangos,

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

Expert Comment

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

Author Comment

by:mijangos
ID: 12375075
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
ID: 12375135
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
ID: 12375618
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
ID: 12375659
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
ID: 12375678
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
ID: 12377608
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
ID: 12378249
Do you have any error in the java consolle?
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12379054
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:
CEHJ earned 600 total points
ID: 12379783
0
 

Author Comment

by:mijangos
ID: 12384918
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
ID: 12384963
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
ID: 12385002
>>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
ID: 12385146
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
ID: 12385187
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
ID: 12385213
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
ID: 12385294
>>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
ID: 12385481
8-)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.
Suggested Courses

636 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