Assignment 2
A Graphics Virtual Machine

The purpose of this programming assignment is to give you programming experience with:

It also is intended to give you conecptual exerience with a very simple computer that executes a stored program that operates on data in the computer's memory.

A virtual machine is a computer program that is executed by a physical computer (directly or ultimately). The high-level architecture of our Graphics Virtual Machine (GVM) comprises a central processing unit that executes machine instructions that it fetches from a program memory, where the program to be executed is stored. The machine instructions are capable of reading & writing data memory, which is implemented an array of integer slots. The small instruction set is oriented, such as it is, towards producing graphical output (e.g., rectangles, ovals, lines) of various colors.

Specification

This application is organized roughly according to the Model-View-Controller (MVC) design pattern. The model is the GVM. The view is the graphics output of the GVM: Since this is a graphics virtual machine, the purpose of the programs that the machine executes is to programmatically "draw" graphics onto its image object. The controller is the rather simple control panel of the GVM. These application components are integrated in a Java application class, called App, which instantiates the 3 components. (Use of the name "App" is an informal naming convention.)

The App class

The App class implementation is complete.

Please study its details; in future assignments, rather than give you the App code, you will need to adapt the code below.

App.java
 1 import java.awt.BorderLayout;
 2 import java.awt.Dimension;
 3 import javax.swing.JFrame;
 4 
 5 /**
 6  *
 7  * @author Pete Cappello
 8  */
 9 public class App extends JFrame
10 {
11     private final View view = new View();;
12     private final ControlPanel controlPanel;
13     private final GVM gvm;
14                 
15     App() 
16     {        
17         gvm = new GVM();
18         controlPanel = new ControlPanel( view, gvm );        
19         setTitle( "Graphics Virtual Machine" );
20         setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
21                       
22         add( view, BorderLayout.CENTER );
23         add( controlPanel, BorderLayout.SOUTH );
24         
25         Dimension dimension = new Dimension( GVM.IMAGE_SIZE, GVM.IMAGE_SIZE + controlPanel.getHeight() );
26         setSize( dimension  );
27         setPreferredSize( dimension );
28         setVisible( true );
29     }
30     
31     /**
32      * Run the Graphics Virtual Machine application.
33      * @param args unused 
34      */
35     public static void main( String[] args ) { App app = new App(); }
36 }
37 

The View class

The View class is a JPanel with 2 small changes:

The code below does all this, except for the Image setter.

 1 import java.awt.Graphics;
 2 import java.awt.Image;
 3 import javax.swing.JPanel;
 4 
 5 /**
 6  *
 7  * @author Peter Cappello
 8  */
 9 public class View extends JPanel
10 {
12     private Image image;
13     
14     @Override
15     public void paintComponent( Graphics graphics )
16     {
17         super.paintComponent( graphics );
18         graphics.drawImage( image, 0, 0, GVM.IMAGE_SIZE, GVM.IMAGE_SIZE, this );
19     }
20     
21     // Declare a setter method for the Image attribute, to be invoked by the Controller.
22 } 

The Controller

The controller has 2 controls: a Run button & a Step button:

Because the controller has a lot of Graphical User Interface (GUI) code that uses Java class libraries that we have not covered, I include that code in the skeleton given below. I will explain the organization of this code in class.

Please study its details; in future assignments, rather than give you the GUI code, you will need to adapt the code below.
Note the contoller TEMPLATE CODE for each action comes in 2 flavors. One version uses the Java 8 lambda expression feature (see a tutorial on Java lambda expressions), & another version (commented out in the code given below) does not use this feature. If you are not running Java 8, use the pre-Java 8 version of the code.

You need to implement the methods runButtonActionPerformed & stepButtonActionPerformed. The former needs to invode the GVM run method, then invoke the view's repaint method; the latter needs to invode the GVM step method, then invoke the view's repaint method.

ControlPanel.java
 1 import java.awt.GridLayout;
 2 import java.awt.event.ActionEvent;
 3 //import java.awt.event.ActionListener;
 4 import javax.swing.JButton;
 5 import javax.swing.JPanel;
 6 
 7 /**
 8  *
 9  * @author Pete Cappello
10  */
11 public class ControlPanel extends JPanel 
12 {
13     private final View view;
14     private final GVM gvm;
15     
16     private final JButton runButton  = new JButton( "Run" );
17     private final JButton stepButton = new JButton( "Step" );
18     
19     ControlPanel( View view, GVM gvm ) 
20     {
21         this.view = view;
22         this.gvm = gvm;
23      
24         setLayout( new GridLayout( 1, 2 ) );
25         add( runButton );
26         add( stepButton );
27 
28         initialize();
29         view.setImage( gvm.getImage() );
30         gvm.load(); // load program to be executed
31     }
32 
33     private void initialize() 
34     {
35         //------------------------------------------
36         // contoller TEMPLATE CODE for each action
37         //------------------------------------------
38         // If you are running Java 8, use lambda expressions
39         runButton.addActionListener( ( ActionEvent actionEvent ) -> 
40         {
41             runButtonActionPerformed( actionEvent );
42         });
43         
44         stepButton.addActionListener( ( ActionEvent actionEvent ) -> 
45         {
46             stepButtonActionPerformed( actionEvent );
47         });
48         
49         // If you are not running Java 8, uncomment the code below   
50 //        runButton.addActionListener( new ActionListener() 
51 //        {
52 //            @Override
53 //            public void actionPerformed( ActionEvent actionEvent ) 
54 //            {
55 //                runButtonActionPerformed( actionEvent );
56 //            }
57 //        });
58                
59 //        stepButton.addActionListener( new ActionListener() 
60 //        {
61 //            @Override
62 //            public void actionPerformed( ActionEvent actionEvent ) 
63 //            {
64 //                stepButtonActionPerformed( actionEvent );
65 //            }
66 //        });
67     }
68 
69     // _____________________________
70     //  controller for each action
71     // _____________________________
72     private void runButtonActionPerformed( ActionEvent actionEvent ) 
73     {
74         // your implementation goes here.
76     }
77 
78     private void stepButtonActionPerformed( ActionEvent actionEvent ) 
79     { 
80         // your implementation goes here.
82     }
83 }
84 

The Model: The Graphics Virtual Machine

The GVM is the most complex component of the assignment.

Attributes

Methods

A skeleton for this class is given below. You may want to add private helper methods to keep your program organization clean.

GVM.java
// imports go here

/**
 * Graphics Virtual Machine
 *
 * @author Peter Cappello
 */
public class GVM 
{
    public static final int IMAGE_SIZE = 800;
    // other symbolic program constants go here
    
    // attribute type declarations go here
        
    GVM() 
    {         
        image = new BufferedImage( IMAGE_SIZE, IMAGE_SIZE, BufferedImage.TYPE_INT_RGB );
        /*
         * Set the GVM's Graphics attribute to the image's Graphics.
         * Make the entire image white, using a fillRect.
         * set the GVM's current color to black.
         */
    }
    
    Image getImage() { /* Your implementationm goes here. */ }
    
    void load()
    {
        // The basic testing program is given below. Replace it, if you want to go for extra credit. See below.
        // Note: I defined symbolic program constants (not shown in listing) 
        // that enable this somewhat more readable version to compile.
        
        programMemory = new int[][]
        {
            { SET, 20 },  // ACC <- 10
            { STORE, X },   // STORE ACC -> X
            { STORE, Y },   // STORE ACC -> Y
            { STORE, WIDTH }, // STORE ACC -> WIDTH
            { STORE, HEIGHT }, // STORE ACC -> HEIGHT
            { DRAWRECT }, // DRAWRECT
            { SET, 255 },  // ACC <- 255
            { STORE, RED }, // ACC -> RED
            { SETCOLOR }, // SETCOLOR to red
            { LOAD, X }, // ACC <- X
            { ADD, X }, // ACC += X
            { STORE, X }, // ACC -> X
            { STORE, Y }, // ACC -> Y
            { STORE, WIDTH }, // STORE ACC -> WIDTH
            { STORE, HEIGHT }, // STORE ACC -> HEIGHT
            { DRAWOVAL }, // FILLRECT
            { LOAD, X }, // ACC <- X
            { ADD, X }, // ACC += X
            { STORE, X }, // ACC -> X
            { STORE, Y }, // ACC -> Y
            { STORE, WIDTH }, // STORE ACC -> WIDTH
            { STORE, HEIGHT }, // STORE ACC -> HEIGHT
            { FILLOVAL }, // FILLRECT
            { LOAD, X }, // ACC <- X
            { ADD, X }, // ACC += X
            { STORE, X }, // ACC -> X
            { STORE, Y }, // ACC -> Y
            { STORE, WIDTH }, // STORE ACC -> WIDTH
            { STORE, HEIGHT }, // STORE ACC -> HEIGHT
            { FILLRECT }, // FILLRECT
            { LOAD, X }, // ACC <- X
            { ADD, X }, // ACC += X
            { STORE, X }, // ACC -> X
            { STORE, Y }, // ACC -> Y
            { STORE, WIDTH }, // STORE ACC -> WIDTH
            { STORE, HEIGHT }, // STORE ACC -> HEIGHT
            { DRAWLINE }, // FILLRECT
            { STOP  }       // STOP
        };
    }
        
    void step()
    { 
        // your implementation goes here.
    }
    
    void run()
    {
        // your implementation goes here.
    }
    
    private void executeInstruction( int[] instruction )
    {
        // your implementation goes here
    }
}

Feel free to add helper methods that enhance the readability of your program.

Basic GVM Program Output

My implementation, when executed on the basic GVM program that is loaded by default, produces the following output. This is what we will test your implementation against.

Basic GVM Program Output

Extra credit opportunity

Below, the image generated by another GVM program is given. If you can write a GVM program to produce this image, you will receive an extra 40%.

Basic GVM Program Output

Discussion of possible enhancements

There are many enhancements that can be made. Here are but a few:

Rubric

ValueAspect
1Compiles
6Correctly produces the basic program output image
3Style
10Total

For style, we want:


 cappello@cs.ucsb.edu © Copyright 2014 Peter Cappello                                           2014.04.16