shapes in java

Discuss all aspects of programming here.

Moderator: The Mod Squad

shapes in java

Postby cwl157 » Thu Jan 06, 2005 5:01 pm

i kinda know how to draw shapes in java but i have a couple questions.

Here is the code i used:
Code: Select all
/* Import the code to draw pictures & shapes */
import java.awt.*;

/* Import the code to display as an applet */
import java.applet.Applet;

public class House extends Applet {

    public void paint (Graphics page) {
        setBackground(Color.lightGray);  //sets the applet background to lightGray
        //setForeground(Color.white);

        page.setColor(Color.red);  //set the foreground color the color object we created

         // param 1 is x distance (the "X" coordinate)
         // param 2 is y distance down (the "Y" coordinate)
         // width is the width of the rectangle
         // height is the height of the rectangle
         
        page.fill3DRect(50, 300, 150,150,true);  //draw an opaque rectangle

        page.setColor(Color.yellow); // set the color yellow

        page.fillOval(50, 150, 75, 75);    //this draws an opaque oval
    }
}


I have 2 questions. My first is how do i run this as a program and not an applet and how do i draw triangles and lines in java? Thanks
cwl157
Black Belt
Black Belt
 
Posts: 851
Joined: Mon Apr 12, 2004 2:32 pm
Location: Chicago

Postby olly » Thu Jan 06, 2005 11:40 pm

olly
Green Belt
Green Belt
 
Posts: 222
Joined: Thu Jun 27, 2002 8:11 pm

Re: shapes in java

Postby olly » Fri Jan 07, 2005 2:36 am

cwl157 wrote:My first is how do i run this as a program and not an applet


You can use a frame. Here is an example of a Frame:

Code: Select all
import java.awt.*;
import java.awt.event.*;

public class FrHouse extends Frame
{
   //If you embed the main method in your frame class
   //You dont need a seperate application class
   public static void main(String[] args)
   {
      FrHouse myFrame = new FrHouse(); //Create a frame object
      myFrame.setVisible(true); //Make it visible
   }

   //Constructor
    public FrHouse()
    {
        setTitle("My Frame"); //Frame title
        setSize(500, 500); //Frame size
        setLocation(100, 100); //Frame location
        setBackground(Color.lightGray); //Frame background colour
        addWindowListener(new WlsFrame());
    }

    //Terminating an application when user closes the frame
    class WlsFrame extends WindowAdapter
    {
      public void windowClosing(WindowEvent we)
      {
         System.exit(0); //Terminates the application
      }
   }
}
olly
Green Belt
Green Belt
 
Posts: 222
Joined: Thu Jun 27, 2002 8:11 pm

Re: shapes in java

Postby olly » Fri Jan 07, 2005 2:43 am

cwl157 wrote:how do i draw triangles and lines in java? Thanks


I think you can make use of the classes:

Graphics &
Graphics2D

In the java.awt package.

Check the docs:

Java 2 Platform API Specification -> java.awt -> Graphics & Graphics2D
olly
Green Belt
Green Belt
 
Posts: 222
Joined: Thu Jun 27, 2002 8:11 pm


Return to Programming

Who is online

Users browsing this forum: No registered users and 2 guests