Programming Resources
For Fun and Learning
Charles Cusack
Computer Science
Hope College
main

Python
C++

JAVA


PHP
SQL
Alice

InheritanceExercise


Description

Download zip file (Right click)

BasicString.java
DrawingPanel.java
DrawingStuff.java
Oval.java
Rectangle.java

DrawingStuff.java

/**
 * DrawingStuff
 * The main class for the drawing program
 * 
 * This is an application that allows the user to move objects around the
 * screen to create drawings.  It is a "rough draft", so objects cannot
 * be added via the user interface.  For testing purposes, several objects
 * are added in the addStuff() method.
 * 
 * Right now, it supports 3 object types: Ovals, Rectangles, and Strings.
 * It would be nice if I could add triangles, Polygons, Lines, JPEG images, 
 * etc.
 * 
 * Your job is to get started on this by adding triangles and lines.
 * You may refactor the code and add as many new classes as you need to.
 * Of course, design is of utmost importance, so make wise and informed
 * decisions about your class structure.
 * 
 * You should probably not need to modify this class.
 * 
 * @author Chuck Cusack
 * @version 1.0, September, 2006
 */
import javax.swing.*;
import java.awt.*;

public class DrawingStuff extends JFrame {
    
    // The desired dimensions of the main window
    private int width=450;
    private int height=400;
    
    // The graphical components
    private DrawingPanel mainPanel;
    
    /**
     * The main method, which allows us to run the application from
     * anywhere.  
     */
    public static void main(String[] args) {  
        new DrawingStuff();
     }
    
    /**
     * The default constructor.
     * It creates the drawing panel and various other components
     * that we need for our program.
     */
    public DrawingStuff() {
        setTitle("A Drawing Example");
        
        // Instantiate the main drawing panel
        mainPanel=new DrawingPanel();
        
        // Place all of the graphical components on the main window
        Container cont=getContentPane();
        cont.setLayout(new BorderLayout());
        cont.add(mainPanel,BorderLayout.CENTER);
        
        // Finish setting up the main window
        setBackground(Color.white);
        pack(); 
        setSize(new Dimension(width,height));
        setVisible(true);
        addStuff();
    }
    
    /**
     * The addStuff method is for testing the class.  It creates
     * various objects of various types in various ways to help us
     * test our code.
     */
    public void addStuff() {
        
		mainPanel.addObject(new Oval(10,10,100,150,Color.green,false));
		mainPanel.addObject(new Oval(100,200,75,75,Color.green,true));
		
		Rectangle rect=new Rectangle(200,150,200,150,Color.blue,true);
		mainPanel.addObject(rect);
		mainPanel.setSelectedObject(rect);
        mainPanel.addObject(new BasicString(250,20,Color.blue,"Hello"));
        
    }  
}