Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Program
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
batman




PostPosted: Sat Nov 04, 2006 8:55 pm   Post subject: Program

How can I modify the Oval class so that x and y refer to the top left of the screen (0,0), and setHeight and setPosition still work correclty?

code:

import java.awt.*;

public class Oval
{
    // Fields (instance variables)
    int x, y; // centre of object
    int height; // width is half height
    int width;
    Color c;    // color to draw oval

    // Constructors

    /**
     * Creates an oval with the specified x,y and
     * height
     *
     * @param initialX initial x
     * @param initialY initial y
     * @param initialHeight initial height
     **/
    public Oval (int initialX, int initialY, int initialHeight)
    {
        x = initialX;
        y = initialY;
        height = initialHeight;
        width = height / 2;
        c = Color.BLUE;
    }
   
    /**
     * Creates an oval with the specified x,y,
     * height, and color
     *
     * @param initialX initial x
     * @param initialY initial y
     * @param initialHeight initial height
     * @param initialColor initial color
     **/
    public Oval (int initialX, int initialY, int initialHeight, Color initialColor)
    {
        x = initialX;
        y = initialY;
        height = initialHeight;
        width = height / 2;
        c = initialColor;
    }
   
    // Methods

    /**
     * Set oval position
     *
     * @param newX New x position
     * @param newY New y position
     **/
    public void setPosition (int newX, int newY)
    {
        x = newX;
        y = newY;
    }

    /**
     * Set oval height
     *
     * @param newHeight New height
     **/
    public void setHeight (int newHeight)
    {
        height = newHeight;
        width = height / 2;
    }

    /**
     * Draw oval on graphics object
     *
     * @param g Graphics object for drawing
     **/
    public void draw (Graphics g)
    {
        int drawX = x - width / 2;
        int drawY = y - height / 2;

        g.setColor(c);
        g.fillOval (drawX, drawY, width, height);
    }
}

code:

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

public class OvalDemo extends Applet implements Runnable
{
    // Instance variables
    Oval o;         // ovals
    Thread t;       // thread

    public void init ()
    {
        o = new Oval (200, 200, 100);
       
    } // init method


    /**
     * Starts a new thread
     **/
    public void start ()
    {
        t = new Thread (this);
        t.start ();
    }

 
    public void run ()
    {
        // loop for height of first oval, from
        // h=200 to h=1
        for (int h = 200 ; h > 0 ; h--)
        {
            // Set the heights of the two ovals
          
            o.setHeight (100 + (200 - h));
            // Repaint the screen
            repaint ();
            // Pause
            try
            {
                Thread.sleep (50);
            }
            catch (InterruptedException e)
            {
            }
        }
    }

 
    public void paint (Graphics g)
    {
        o.draw (g);
    } // paint method
} // OvalDemo class
Sponsor
Sponsor
Sponsor
sponsor
ericfourfour




PostPosted: Sat Nov 04, 2006 10:57 pm   Post subject: (No subject)

I'm no expert at java, but if you want to modify a class you should just extend another class and do the modifications you want in that class. In this case extend the oval class and do only the modifications you want in that class.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: