Commit 52447fa0 authored by Daniel Vogel's avatar Daniel Vogel

=added java demos, updated x demos, added button x demo

parent 70b08d60
// Simple class hierarchy
// Demonstrates abstract classes, containment
public class Animals1 {
// inner classes
// base class
abstract class Animal {
abstract String talk();
}
// derived classes
class Cat extends Animal {
String talk() { return "Meow!"; }
}
class Dog extends Animal {
String talk() { return "Woof!"; }
}
// container class methods
Animals1() {
speak(new Cat());
speak(new Dog());
}
void speak(Animal a) {
System.out.println( a.talk() );
}
// static main methods -- entry point
public static void main(String[] args) {
Animals1 a = new Animals1();
}
}
// Simple class hierarchy
// Contrasts interface and abstract class implementations
public class Animals2 {
// interface
interface Pet {
String talk();
}
// inner classes
class Cat implements Pet {
public String talk() { return "Meow!"; }
}
class Dog implements Pet {
public String talk() { return "Woof!"; }
}
// container class methods
Animals2() {
speak(new Cat());
speak(new Dog());
}
void speak(Pet a) {
System.out.println( a.talk() );
}
// static main methods -- entry point
public static void main(String[] args) {
Animals2 a = new Animals2();
}
}
class Bicycle {
String owner = null;
int speed = 0;
int gear = 1;
// constructor
Bicycle() { }
Bicycle(String name) { owner = name; }
// methods
void changeSpeed(int newValue) { speed = newValue; }
void changeGear(int newValue) { gear = newValue; }
int getSpeed() { return speed; }
int getGear() { return gear; }
// static entry point - main method
public static void main(String[] args) {
Bicycle adultBike = new Bicycle("Jeff");
adultBike.changeSpeed(20);
System.out.println("speed=" + adultBike.getSpeed());
Bicycle kidsBike = new Bicycle("Austin");
kidsBike.changeSpeed(15);
System.out.println("speed=" + kidsBike.getSpeed());
}
}
// Simple class hierarchy
// Demonstrates abstract classes and containment.
// Bike container class
class Bikes1 {
// base class
abstract class Bike {
int wheels = 0;
int speed = 0;
void setWheels(int val) { wheels = val; }
void setSpeed(int val) { speed = val; }
void show() {
System.out.println("wheels = " + wheels);
}
}
// derived two-wheel bike
class Bicycle extends Bike {
Bicycle() {
setWheels(2);
}
}
// derived two-wheel bike
class Tricycle extends Bike {
Tricycle() {
setWheels(3);
}
}
// container class
Bikes1() {
Bicycle b = new Bicycle();
b.show();
Tricycle t = new Tricycle();
t.show();
}
// entry point
public static void main(String[] args) {
Bikes1 b = new Bikes1();
}
}
\ No newline at end of file
// Simple class hierarchy + interface
// Demonstrates abstract classes, containment, interfaces.
// Bike container class
class Bikes2 {
// base class
abstract class Bike {
int wheels = 0;
int speed = 0;
void setWheels(int val) { wheels = val; }
void setSpeed(int val) { speed = val; }
void show() {
System.out.println("wheels = " + wheels);
System.out.println("speed = " + speed);
}
}
// interface for ANYTHING driveable
// could be applied to car, scooter etc.
interface Driveable {
void accelerate();
void brake();
}
// derived two-wheel bike
class Bicycle extends Bike implements Driveable {
Bicycle() { setWheels(2); }
// interface methods
// need to make public to match access level for interface (above)
public void accelerate() { setSpeed(speed += 5); }
public void brake() { setSpeed(speed -= 5); }
}
// derived two-wheel bike
class Tricycle extends Bike implements Driveable {
Tricycle() { setWheels(3); }
// interface methods
// need to make public to match access level for interface (above)
public void accelerate() { setSpeed(speed += 1); }
public void brake() { setSpeed(speed -= 1); }
}
// container class
Bikes2() {
Bicycle b = new Bicycle();
b.accelerate();
b.show();
Tricycle t = new Tricycle();
t.accelerate();
t.show();
}
// entry point
public static void main(String[] args) {
Bikes2 b = new Bikes2();
}
}
\ No newline at end of file
import javax.swing.*;
// Create a simple form
public class BasicForm1 {
public static void main(String[] args) {
// create a window
JFrame frame = new JFrame("Window Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// create a panel and add components
JPanel panel = new JPanel();
JButton button = new JButton("Ok");
panel.add(button);
// add panel to the window
frame.add(panel);
// set window behaviour and display it
frame.setResizable(false);
frame.setSize(200, 200);
// frame.pack();
frame.setVisible(true);
}
}
import javax.swing.*;
import javax.swing.event.MouseInputListener;
import java.awt.event.MouseEvent;
// Create a simple form
// Adds a listener
public class BasicForm2 {
public static void main(String[] args) {
// create a window
JFrame frame = new JFrame("Window Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// create a panel and add components
JPanel panel = new JPanel();
JButton button = new JButton("Ok");
button.addMouseListener(new MyMouseListener());
panel.add(button);
// add panel to the window
frame.add(panel);
// set window behaviour and display it
frame.setResizable(false);
frame.setSize(200, 200);
// frame.pack();
frame.setVisible(true);
}
// create a custom listener for my ok button
static class MyMouseListener implements MouseInputListener {
@Override
public void mouseClicked(MouseEvent e) {
System.exit(1);
}
@Override
public void mousePressed(MouseEvent e) { }
@Override
public void mouseReleased(MouseEvent e) { }
@Override
public void mouseEntered(MouseEvent e) { }
@Override
public void mouseExited(MouseEvent e) { }
@Override
public void mouseDragged(MouseEvent e) { }
@Override
public void mouseMoved(MouseEvent e) { }
}
}
\ No newline at end of file
import javax.swing.*;
import javax.swing.event.MouseInputListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
// Create a simple form
// Converts listener to adapter
public class BasicForm3 {
public static void main(String[] args) {
// create a window
JFrame frame = new JFrame("Window Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// create a panel and add components
JPanel panel = new JPanel();
JButton button = new JButton("Ok");
button.addMouseListener(new MyMouseAdapter());
panel.add(button);
// add panel to the window
frame.add(panel);
// set window behaviour and display it
frame.setResizable(false);
frame.setSize(200, 200);
// frame.pack();
frame.setVisible(true);
}
// create a custom adapter from MouseAdapter base class
static class MyMouseAdapter extends MouseAdapter {
public void mouseClicked(MouseEvent e) {
System.exit(1);
}
}
}
import javax.swing.*;
import javax.swing.event.MouseInputListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
// Create a simple form
// Converts adapter to anonymous inner class
public class BasicForm4 {
public static void main(String[] args) {
// create a window
JFrame frame = new JFrame("Window Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// create a panel and add components
JPanel panel = new JPanel();
JButton button = new JButton("Ok");
button.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
System.exit(1);
}});
panel.add(button);
// add panel to the window
frame.add(panel);
// set window behaviour and display it
frame.setResizable(false);
frame.setSize(200, 200);
// frame.pack();
frame.setVisible(true);
}
// create a custom adapter from MouseAdapter base class
static class MyMouseAdapter extends MouseAdapter {
public void mouseClicked(MouseEvent e) {
System.exit(1);
}
}
}
import javax.swing.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.BasicStroke;
import java.awt.RenderingHints;
// JComponent is a base class for custom components
public class SimpleDraw extends JComponent {
public static void main(String[] args) {
JFrame f = new JFrame("SimpleDraw"); // jframe is the app window
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(400, 400); // window size
SimpleDraw canvas = new SimpleDraw();
f.setContentPane(canvas); // add canvas to jframe
f.setVisible(true); // show the window
}
// Constructor for SimpleDraw
public SimpleDraw() {
}
// custom graphics drawing
// this is a method that we overrode from the JComponent class
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g; // 2D drawing
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setStroke(new BasicStroke(32)); // 32 pixels wide
g2.setColor(Color.BLUE); // make it blue
g2.drawLine(0, 0, getWidth(), getHeight()); // draw line
g2.setColor(Color.RED);
g2.drawLine(getWidth(), 0, 0, getHeight());
}
}
import javax.swing.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.BasicStroke;
import java.awt.RenderingHints;
import java.awt.event.*;
// JComponent is a base class for custom components
public class SimpleDraw2 extends JComponent {
public static void main(String[] args) {
SimpleDraw2 canvas = new SimpleDraw2();
JFrame f = new JFrame("SimpleDraw"); // jframe is the app window
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(400, 400); // window size
f.setContentPane(canvas); // add canvas to jframe
f.setVisible(true); // show the window
}
// Object properties
private int mouseX;
private int mouseY;
// Constructor for SimpleDraw
public SimpleDraw2() {
this.addMouseMotionListener( new MyMotionListener() );
}
// custom graphics drawing
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g; // cast to get 2D drawing methods
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, // antialiasing look nicer
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setStroke(new BasicStroke(32)); // 32 pixel thick stroke
g2.setColor(Color.BLUE); // make it blue
g2.drawLine(0, 0, getWidth(), getHeight()); // draw line
g2.setColor(Color.RED);
g2.drawLine(getWidth(), 0, 0, getHeight());
String label = "Mouse at (" + mouseX + ", " + mouseY + ")";
g2.setColor(Color.BLACK);
g2.drawString(label, 130, 40);
}
// Inner class for handling mouse motion events
private class MyMotionListener implements MouseMotionListener {
public void mouseMoved(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
repaint();
}
public void mouseDragged(MouseEvent e) {
// Do nothing.
// But we still need this method implemented because it's part of the interface
}
}
}
import javax.swing.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.BasicStroke;
import java.awt.RenderingHints;
import java.awt.event.*;
// JComponent is a base class for custom components
public class SimpleDraw3 extends JComponent {
public static void main(String[] args) {
SimpleDraw3 canvas = new SimpleDraw3();
JFrame f = new JFrame("SimpleDraw"); // jframe is the app window
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(400, 400); // window size
f.setContentPane(canvas); // add canvas to jframe
f.setVisible(true); // show the window
}
// Object properties
private int mouseX;
private int mouseY;
// Constructor for SimpleDraw
public SimpleDraw3() {
this.addMouseMotionListener( new MyMotionListener() );
}
// custom graphics drawing
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g; // cast to get 2D drawing methods
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, // antialiasing look nicer
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setStroke(new BasicStroke(32)); // 32 pixel thick stroke
g2.setColor(Color.BLUE); // make it blue
g2.drawLine(0, 0, getWidth(), getHeight()); // draw line
g2.setColor(Color.RED);
g2.drawLine(getWidth(), 0, 0, getHeight());
String label = "Mouse at (" + mouseX + ", " + mouseY + ")";
g2.setColor(Color.BLACK);
g2.drawString(label, 130, 40);
}
// Inner class for handling mouse motion events
private class MyMotionListener extends MouseMotionAdapter {
public void mouseMoved(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
repaint();
}
// Notice the difference?
// We implemented the MotionListener using an adapter instead of an interface
// This allows is to ignore methods we don't want!
/*
public void mouseDragged(MouseEvent e) {
// Do nothing.
// But we still need this method implemented because it's part of the interface
}
*/
}
}
import javax.swing.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.BasicStroke;
import java.awt.RenderingHints;
import java.awt.event.*;
import java.util.Vector;
// JComponent is a base class for custom components
public class SimpleDraw4 extends JComponent {
public static void main(String[] args) {
SimpleDraw4 canvas = new SimpleDraw4();
JFrame f = new JFrame("SimpleDraw"); // jframe is the app window
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(400, 400); // window size
f.setContentPane(canvas); // add canvas to jframe
f.setVisible(true); // show the window
}
// Object properties
private int mouseX;
private int mouseY;
// Constructor for SimpleDraw
public SimpleDraw4() {
// this is an example of an anonymous inner class
this.addMouseMotionListener( new MouseAdapter() {
public void mouseMoved(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
repaint();
}
});
}
// custom graphics drawing
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g; // cast to get 2D drawing methods
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, // antialiasing look nicer
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setStroke(new BasicStroke(32)); // 32 pixel thick stroke
g2.setColor(Color.BLUE); // make it blue
g2.drawLine(0, 0, getWidth(), getHeight()); // draw line
g2.setColor(Color.RED);
g2.drawLine(getWidth(), 0, 0, getHeight());
String label = "Mouse at (" + mouseX + ", " + mouseY + ")";
g2.setColor(Color.BLACK);
g2.drawString(label, 130, 40);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class WidgetDemo {
public static void main(String[] args) {
WidgetDemo demo = new WidgetDemo();
}
JLabel label;
WidgetDemo()
{
JFrame f = new JFrame("WidgetDemo.java");
f.setSize(500, 200);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// create a button and add a listener for events
JButton button = new JButton("My Button");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.setText("button");
}
});
// create a slider
JSlider slider = new JSlider(0, 100, 50);
slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
JSlider s = (JSlider)e.getSource();
label.setText("slider " + s.getValue());
}
});
// create a radio button group
ButtonGroup radiobuttons = new ButtonGroup();
JPanel radioPanel = new JPanel(new GridLayout(1, 0));
for (String s: new String[] {"A", "B", "C"})
{
JRadioButton rb = new JRadioButton(s);
rb.addActionListener(radioButtonListener);
radiobuttons.add(rb);
radioPanel.add(rb);
}
// create a menu
JMenu menu = new JMenu("Choices");
// create some menu choices
for (String s: new String[] {"apple", "orange", "banana", "pear" })
{
// add this menu item to the menu
JMenuItem mi = new JMenuItem(s);
// set the listener when events occur
mi.addActionListener(menuItemListener);
// add this menu item to the menu
menu.add(mi);
}
JMenuBar menubar = new JMenuBar();
menubar.add(menu);
// create a label