Thursday, November 18, 2010

Assignment 19 - Deck Of Cards



// Deck of Cards
//Card shuffling and dealing with collections method shuffle.

import java.util.List;
import java.util.Arrays;
import java.util.Collections;

//class to represent a Card in a deck of cards
class Card
{
public static enum Face { Ace, Deuce, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King };
public static enum Suit { Clubs, Diamonds, Hearts, Spades };

private final Face face; //face of card
private final Suit suit; //Suit of card

//two-argument constructor

public Card( Face cardFace, Suit cardSuit )
{
face = cardFace; //initialize face of card
suit = cardSuit; //initialize face of card
} //end two-argument card constructor

//return face of the card

public Face getFace()
{
return face;
} // end method getFace

//return suit of card

public Suit getSuit()
{
return suit;
} //end method getSuit

//return string representation of card

public String toString()
{
return String.format("%s of %s", face, suit );
} //end method toString
}//end class card

//class DeckofCards declaration

public class DeckOfCards
{
private List<> list; //declare List that will store cards

//set up deck of cards and shuffle
public DeckOfCards()
{
Card[] deck = new Card [ 52 ];
int count = 0; //number of cards

//populate deck with Card Objects

for ( Card.Suit suit : Card.Suit.values() )
{
for ( Card.Face face : Card.Face.values() )
{
deck[ count ] = new Card( face, suit );
++count;
} //end for
}//end for

list = Arrays.asList( deck ); // get list
Collections.shuffle( list ); //shuffle deck
} //end of DeckOfCards constructor

// output deck

public void printCards()
{
//display 52 cards in two colums

for ( int i = 0; i System.out.printf( "%-19s%s", list.get( i ),
( ( i+1 ) %4 == 0 ) ? "\n" : "");
} //end method print cards

public static void main( String[] args )
{
DeckOfCards cards = new DeckOfCards();
cards.printCards();
}//end main
}//end class DeckOfCards

Assignment 17: Slider Demo




/*
//Graphics problem
*/
import java.awt.Graphics;
import java.awt.Dimension;
import javax.swing.JPanel;

public class OvalPanel extends JPanel
{
private int diameter = 10;//default diameter of 10

//draw an oval of the specified diameter
public void paintComponent( Graphics g )
{
super.paintComponent( g );

g.fillOval( 10, 10, diameter, diameter ); //draw circle
}

//validate and set diameter
public void setDiameter( int newDiameter )
{
//if diameter invalid, default to 10
diameter = ( newDiameter >= 0 ? newDiameter : 10 );
repaint(); //repaint panel
}

//use by layout manager to determine preffered size
public Dimension getPreferredSize()
{
return new Dimension( 200, 200 );
}

//used by layout manager to determine minimum size
public Dimension getMinimumSize()
{
return getPreferredSize();
}
}


-----------------------------------------------------------------



/*
//SliderFrame
*/
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;

public class SliderFrame extends JFrame
{
private JSlider diameterJSlider; // slider to select diameter
private OvalPanel myPanel; // panel to draw circle

//no argument constructor
public SliderFrame()
{
super( "Slider Demo" );

myPanel = new OvalPanel(); //create panel to draw circle
myPanel.setBackground( Color.YELLOW ); //set bacground to yellow

//set up JSlider to control diameter value
diameterJSlider = new JSlider( SwingConstants.HORIZONTAL, 0 , 200, 10 );
diameterJSlider.setMajorTickSpacing(10); //create tick every 10
diameterJSlider.setPaintTicks( true ); //paint ticks on slider

//register JSlider event listener
diameterJSlider.addChangeListener(

new ChangeListener() //anonymous inner circle
{
//handle change in slider value
public void stateChanged( ChangeEvent e )
{
myPanel.setDiameter( diameterJSlider.getValue() );
}
}
);

add( diameterJSlider, BorderLayout.SOUTH ); //add slider to frame
add( myPanel, BorderLayout.CENTER ); //add panel to frame
}
}

---------------------------------------------------------------------

/*
//main method
*/

import javax.swing.JFrame;

public class SliderDemo
{
public static void main( String args[] )
{
SliderFrame sliderFrame = new SliderFrame();
sliderFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
sliderFrame.setSize( 220, 270 ); //set frame size
sliderFrame.setVisible( true ); //display frame
}
}

Thursday, November 11, 2010

Assignment 18 - Menu Test



// Fig. 22.5: MenuFrame.java
// Demostrating menus.

import java.awt.Color;
import java.awt.Font;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JFrame;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.ButtonGroup;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;

public class MenuFrame extends JFrame
{
private final Color colorValues[]=
{Color.BLACK, Color.BLUE, Color.RED, Color.GREEN};
private JRadioButtonMenuItem colorItems[]; //color menu items
private JRadioButtonMenuItem fonts[]; //font menu items
private JCheckBoxMenuItem styleItems[]; //font style menu items
private JLabel displayJLabel; //displays sample text
private ButtonGroup fontButtonGroup; //manages font menu items
private ButtonGroup colorButtonGroup; //manages color menu items
private int style; // used to create style for font

// no argument constructor set up GUI
public MenuFrame()
{
super("Using Menus");

JMenu fileMenu = new JMenu("File"); //create file menu
fileMenu.setMnemonic('F'); //set mnemonic to F

//create About... menu item
JMenuItem aboutItem = new JMenuItem("About...");
aboutItem.setMnemonic('A'); //set mnemonic to A
fileMenu.add( aboutItem ); //add about item to file menu
aboutItem.addActionListener(

new ActionListener() //anonuymous inner class
{
//displays message dialog when the user selects About...
public void actionPerformed(ActionEvent event)
{
JOptionPane.showMessageDialog(MenuFrame.this,
"This is an example\nof using menus",
"About",JOptionPane.PLAIN_MESSAGE);
}//end method actionPerformed
}//end anonymous inner class
);//end call to addActionListener

JMenuItem exitItem = new JMenuItem("Exit"); //create exit item
exitItem.setMnemonic('X'); //set mnemonic to x
fileMenu.add(exitItem); //add exit item to file menu
exitItem.addActionListener(

new ActionListener() //anonymous inner class
{
//terminate application when user clicks exitItem
public void actionPerformed(ActionEvent event)
{
System.exit(0); //exit application
}//end method actionPerformed
}//end anonymous inner class
);//end call to addActionListener

JMenuBar bar = new JMenuBar(); //create menu bar
setJMenuBar(bar); //add menu bar to application
bar.add(fileMenu); //add file menu to menu bar

JMenu formatMenu = new JMenu("Format"); //create format menu
formatMenu.setMnemonic('r'); //set mnemonic to r

//array listing string colors

String colors[]={"Black", "Blue", "Red", "Green"};

JMenu colorMenu = new JMenu("Color"); //create color menu
colorMenu.setMnemonic('C'); //set mnenonic to C

//create radio button menu items for colors
colorItems = new JRadioButtonMenuItem[ colors.length ];
colorButtonGroup = new ButtonGroup(); //manages colors
ItemHandler itemHandler = new ItemHandler(); //handler for colors

//create color radio button menu items
for (int count = 0; count{
colorItems[count] =
new JRadioButtonMenuItem( colors[ count ]); //create item
colorMenu.add(colorItems[count]); //add item to color menu
colorButtonGroup.add(colorItems[ count ]); //add to group
colorItems [ count ].addActionListener(itemHandler);
}//end for

colorItems[ 0 ].setSelected(true); //select first Color item

formatMenu.add(colorMenu); //add color menu to format menu
formatMenu.addSeparator(); //add separator in menu

//array listing font names
String fontNames[]={"Serif","Monospaced","SansSerif"};
JMenu fontMenu = new JMenu("Font"); //create font menu
fontMenu.setMnemonic('n'); //set mnemonic to n

//create radio button menu items for font names
fonts = new JRadioButtonMenuItem[ fontNames.length];
fontButtonGroup = new ButtonGroup(); //manages font names

//create Font radio button menu items
for (int count = 0; count{
fonts[ count ] = new JRadioButtonMenuItem(fontNames[count]);
fontMenu.add(fonts[count]); //add font to font menu
fontButtonGroup.add(fonts[count]); //add to button group
fonts[count].addActionListener(itemHandler); //add handler
}//end for

fonts[ 0 ].setSelected(true); //select first Font menu item
fontMenu.addSeparator(); //add separator bar to font menu

String styleNames[]={"Bold","Italic"}; //names of styles
styleItems = new JCheckBoxMenuItem[ styleNames.length];
StyleHandler styleHandler = new StyleHandler(); //style handler

//create style checkbox menu items
for (int count =0; count{
styleItems[count]=
new JCheckBoxMenuItem(styleNames[count]); //for style
fontMenu.add(styleItems[count]); //add to font menu
styleItems[count].addItemListener(styleHandler); //handler
}//end for

formatMenu.add(fontMenu); //add font menu to format menu
bar.add(formatMenu); //add format menu to menu bar

// set up label to display text

displayJLabel = new JLabel("Sample Text", SwingConstants.CENTER);
displayJLabel.setForeground(colorValues[0]);
displayJLabel.setFont( new Font("Serif", Font.PLAIN,72));

getContentPane().setBackground(Color.CYAN); //set background
add(displayJLabel,BorderLayout.CENTER); //add displayJLabel
}//end MenuFrame Constructor

//inner class to handle action events from menu items

private class ItemHandler implements ActionListener
{
//process color and font selections
public void actionPerformed(ActionEvent event)
{
//process color selection
for (int count =0; count {
if (colorItems[count].isSelected())
{
displayJLabel.setForeground(colorValues[ count]);
break;
}//end if
}//end for

//process font selection

for (int count =0; count{
if (event.getSource()==fonts[count])
{
displayJLabel.setFont(
new Font(fonts[count].getText(), style,72));
}//end if
}//end for

repaint(); //redraw application
}//end method actionPerformed
}//end class ItemHandler

// inner class to handle item events from chech box menu items
private class StyleHandler implements ItemListener
{
//process font style selections
public void itemStateChanged(ItemEvent e)
{
style = 0; //initialize style

//check for bold selection
if (styleItems[1].isSelected())
style +=Font.BOLD; //add bold to style

//check for italic selection
if (styleItems[1].isSelected())
style +=Font.ITALIC; //add italic to style

displayJLabel.setFont(
new Font(displayJLabel.getFont().getName(),style,72));
repaint(); //redraw application
}//end method itemStateChanged
}//end class StyleHandler
}//end class MenuFrame

------------------------------------------------------------------------------

//Fig 22.65 MenuTest.java
//Testing MenuFrame.

import javax.swing.JFrame;

public class MenuTest
{
public static void main(String args[])

{
MenuFrame menuFrame = new MenuFrame(); //create MenuFrame
menuFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
menuFrame.setSize(500, 200); //set frame size
menuFrame.setVisible(true); //display frame
}//end main
}//end class MenuTest