Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Open sidebar
Ke Shan
cs349-code
Commits
52447fa0
Commit
52447fa0
authored
Sep 25, 2017
by
Daniel Vogel
Browse files
=added java demos, updated x demos, added button x demo
parent
70b08d60
Changes
23
Hide whitespace changes
Inline
Side-by-side
Showing
20 changed files
with
984 additions
and
3 deletions
+984
-3
java/2.1-java-basics/Animals1.java
java/2.1-java-basics/Animals1.java
+36
-0
java/2.1-java-basics/Animals2.java
java/2.1-java-basics/Animals2.java
+35
-0
java/2.1-java-basics/Bicycle.java
java/2.1-java-basics/Bicycle.java
+30
-0
java/2.1-java-basics/Bikes1.java
java/2.1-java-basics/Bikes1.java
+46
-0
java/2.1-java-basics/Bikes2.java
java/2.1-java-basics/Bikes2.java
+63
-0
java/2.2-java-gui/BasicForm1.java
java/2.2-java-gui/BasicForm1.java
+26
-0
java/2.2-java-gui/BasicForm2.java
java/2.2-java-gui/BasicForm2.java
+56
-0
java/2.2-java-gui/BasicForm3.java
java/2.2-java-gui/BasicForm3.java
+40
-0
java/2.2-java-gui/BasicForm4.java
java/2.2-java-gui/BasicForm4.java
+43
-0
java/2.2-java-gui/SimpleDraw.java
java/2.2-java-gui/SimpleDraw.java
+40
-0
java/2.2-java-gui/SimpleDraw2.java
java/2.2-java-gui/SimpleDraw2.java
+59
-0
java/2.2-java-gui/SimpleDraw3.java
java/2.2-java-gui/SimpleDraw3.java
+65
-0
java/2.2-java-gui/SimpleDraw4.java
java/2.2-java-gui/SimpleDraw4.java
+54
-0
java/2.3-widgets/WidgetDemo.java
java/2.3-widgets/WidgetDemo.java
+113
-0
java/2.3-widgets/makefile
java/2.3-widgets/makefile
+15
-0
x/animation.cpp
x/animation.cpp
+1
-1
x/button.cpp
x/button.cpp
+257
-0
x/clipping.cpp
x/clipping.cpp
+2
-1
x/displaylist.cpp
x/displaylist.cpp
+2
-0
x/drawing.cpp
x/drawing.cpp
+1
-1
No files found.
java/2.1-java-basics/Animals1.java
0 → 100644
View file @
52447fa0
// 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
();
}
}
java/2.1-java-basics/Animals2.java
0 → 100644
View file @
52447fa0
// 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
();
}
}
java/2.1-java-basics/Bicycle.java
0 → 100644
View file @
52447fa0
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
());
}
}
java/2.1-java-basics/Bikes1.java
0 → 100644
View file @
52447fa0
// 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
java/2.1-java-basics/Bikes2.java
0 → 100644
View file @
52447fa0
// 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
java/2.2-java-gui/BasicForm1.java
0 → 100644
View file @
52447fa0
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
);
}
}
java/2.2-java-gui/BasicForm2.java
0 → 100644
View file @
52447fa0
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
java/2.2-java-gui/BasicForm3.java
0 → 100644
View file @
52447fa0
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
);
}
}
}
java/2.2-java-gui/BasicForm4.java
0 → 100644
View file @
52447fa0
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
);
}
}
}
java/2.2-java-gui/SimpleDraw.java
0 → 100644
View file @
52447fa0
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
());
}
}
java/2.2-java-gui/SimpleDraw2.java
0 → 100644
View file @
52447fa0
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
}
}
}
java/2.2-java-gui/SimpleDraw3.java
0 → 100644
View file @
52447fa0
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
}
*/
}
}
java/2.2-java-gui/SimpleDraw4.java
0 → 100644
View file @
52447fa0
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
);
}