Tuesday, August 31, 2010

Assignment #3 Part II CS360

Assignment #3 Part II CS360
"Shapes"
08-31-10




//Shapes.java
//Demostrates drawing different shapes.
import java.awt.Graphics;
import javax.swing.JPanel;

public class Shapes extends JPanel
{
private int choice; //user's choice of which shape to draw

//constructor sets the user's choice
public Shapes(int userChoice)
{
choice = userChoice;
} //end Shapes constructor

// draws a cascade of shapes starting from the top-left corner
public void paintComponent(Graphics g)
{
super.paintComponent(g);
for ( int i = 0; i < 10; i++)
{
//pick the shape based on the user's choice
switch (choice)
{
case 1://draw rectangles
g.drawRect(100 - i * 5, 100 - i * 5 , 10 + i * 10, 10 + i * 10 );
break;
case 2://draw ovals
g.drawOval(100 - i * 5, 100 - i * 5 , 10 + i * 10, 10 + i * 10 );
break;
} // end switch
} // end for
} // end method paintCompnent
} //end class Shapes





/*

Test for Shapes

*/

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class ShapesTest
{
public static void main ( String args[])
{
//obtain user's choice

String input = JOptionPane.showInputDialog(
"Enter 1 to draw rectangles\n"+
"Enter 2 to draw ovals");

int choice=Integer.parseInt( input );//converts input to int

//create the panel with the user's input
Shapes panel = new Shapes(choice);

JFrame application = new JFrame();// creates new JFrame

application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.add(panel);
application.setSize(300,300);
application.setVisible(true);
}
}

Thursday, August 26, 2010

Assignment #3 CS360

Assignment #3 CS360
"Shapes"
08-26-10



//Shapes.java
//Demostrates drawing different shapes.
import java.awt.Graphics;
import javax.swing.JPanel;

public class Shapes extends JPanel
{
private int choice; //user's choice of which shape to draw

//constructor sets the user's choice
public Shapes(int userChoice)
{
choice = userChoice;
} //end Shapes constructor

// draws a cascade of shapes starting from the top-left corner
public void paintComponent(Graphics g)
{
super.paintComponent(g);
for ( int i = 0; i < 10; i++)
{
//pick the shape based on the user's choice
switch (choice)
{
case 1://draw rectangles
g.drawRect(10 + i * 10, 10 + i * 10, 50 + i * 10, 50 + i * 10 );
break;
case 2://draw ovals
g.drawOval(10 + i * 10, 10 + i * 10, 50 + i * 10, 50 + i * 10 );
break;
} // end switch
} // end for
} // end method paintCompnent
} //end class Shapes

/*

Test for Shapes

*/

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class ShapesTest
{
public static void main ( String args[])
{
//obtain user's choice

String input = JOptionPane.showInputDialog(
"Enter 1 to draw rectangles\n"+
"Enter 2 to draw ovals");

int choice=Integer.parseInt( input );//converts input to int

//create the panel with the user's input
Shapes panel = new Shapes(choice);

JFrame application = new JFrame();// creates new JFrame

application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.add(panel);
application.setSize(300,300);
application.setVisible(true);
}
}

Assignment #2 CS360

Assignment #2 CS360
"Array On Frame"
08-26-10



/*
Assignment 2
*/

import javax.swing.*;

public class ArrayOnFrame
{

private static void createAndShowGUI()
{
int[]anArray;

anArray = new int[10];

anArray[0] = 100;
anArray[1] = 200;
anArray[2] = 300;
anArray[3] = 400;
anArray[4] = 500;
anArray[5] = 600;
anArray[6] = 700;
anArray[7] = 800;
anArray[8] = 900;
anArray[9] = 1000;

JFrame frame = new JFrame("ArrayOnFrame");
frame.setDefaultCloseOperation(JFrame.EXIT_ONra_CLOSE);

JLabel label = new JLabel("element at index0:" + anArray[0] );
frame.getContentPane().add(label);

frame.pack();
frame.setVisible(true);
}

public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}

Assignment #1 CS360

Assignment #1 CS360
"Hello World Swing"
08-24-10



/*
Assignment 1 HelloWorldSwing

*/

/*
*HelloWorldSwing.java required no other files.
*/
import javax.swing.*;

public class HelloWorldSwing {
/**
* Create the GUI and show it.
*/
private static void createAndShowGUI () {
//Create and set up the window.
JFrame frame = new JFrame ("HelloWorldSwing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Add the ubiquitous "Hello World" label.
JLabel label = new JLabel(" Beatriz Martinez :) ");
frame.getContentPane().add(label);

//Display the window.
frame.pack();
frame.setVisible(true);
}

public static void main (String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI ();
}
});
}
}