Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Tautvydas Intas
cs349_w18_examples
Commits
e46a9c3f
Commit
e46a9c3f
authored
Jan 29, 2018
by
Keiko Katsuragawa
Browse files
add 2-7-Graphics
parent
0271a291
Changes
4
Hide whitespace changes
Inline
Side-by-side
java/2-7-Graphics/SimpleDraw.java
0 → 100644
View file @
e46a9c3f
import
javax.swing.*
;
import
java.awt.*
;
import
java.awt.event.*
;
public
class
SimpleDraw
{
public
static
void
main
(
String
[]
args
)
{
JFrame
f
=
new
JFrame
(
"SimpleDraw"
);
f
.
setDefaultCloseOperation
(
JFrame
.
EXIT_ON_CLOSE
);
f
.
setSize
(
300
,
300
);
f
.
setContentPane
(
new
Canvas
());
f
.
setVisible
(
true
);
}
}
// JComponent is a base class for custom components
class
Canvas
extends
JComponent
{
Point
M
=
new
Point
();
// mouse point
Point
C
=
new
Point
(
150
,
150
);
// click point
Canvas
(){
super
();
this
.
addMouseListener
(
new
MouseAdapter
(){
public
void
mouseClicked
(
MouseEvent
e
){
C
.
x
=
e
.
getX
();
C
.
y
=
e
.
getY
();
repaint
();
}
});
this
.
addMouseMotionListener
(
new
MouseAdapter
(){
public
void
mouseMoved
(
MouseEvent
e
){
M
.
x
=
e
.
getX
();
M
.
y
=
e
.
getY
();
repaint
();
}
});
}
// custom graphics drawing
public
void
paintComponent
(
Graphics
g
)
{
super
.
paintComponent
(
g
);
// cast to get 2D drawing methods
Graphics2D
g2
=
(
Graphics2D
)
g
;
g2
.
setColor
(
Color
.
BLACK
);
g2
.
setStroke
(
new
BasicStroke
(
2
));
g2
.
drawLine
(
C
.
x
,
C
.
y
,
M
.
x
,
M
.
y
);
g2
.
fillOval
(
M
.
x
-
5
,
M
.
y
-
5
,
10
,
10
);
g2
.
setColor
(
Color
.
RED
);
g2
.
fillOval
(
C
.
x
-
5
,
C
.
y
-
5
,
10
,
10
);
g2
.
drawString
(
String
.
format
(
"%d,%d"
,
C
.
x
,
C
.
y
),
C
.
x
+
10
,
C
.
y
+
10
);
g2
.
setColor
(
Color
.
BLACK
);
g2
.
drawString
(
String
.
format
(
"%d,%d"
,
M
.
x
,
M
.
y
),
M
.
x
+
10
,
M
.
y
+
10
);
}
}
java/2-7-Graphics/Transform1.java
0 → 100644
View file @
e46a9c3f
/*
* CS 349 Java Code Examples
*
* Transform1 Shows how to "manually" transform a shape model. NOTE: in practice,
you don't want do this. Use Graphics2D matrix tranformations instead.
*
*/
import
javax.swing.JFrame
;
import
javax.swing.JComponent
;
import
javax.swing.JButton
;
import
java.awt.*
;
import
java.awt.geom.*
;
import
java.util.ArrayList
;
import
javax.vecmath.*
;
import
java.lang.Math.*
;
import
java.util.Random
;
import
java.awt.event.*
;
// create the window and run the demo
public
class
Transform1
{
public
static
void
main
(
String
[]
args
)
{
// create the window
Canvas
canvas
=
new
Canvas
();
JFrame
f
=
new
JFrame
(
"Transform1"
);
// 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
}
}
class
Canvas
extends
JComponent
{
// the house shape model (position is centred at top left corner)
private
Polygon
shape
=
new
Polygon
(
new
int
[]
{
-
50
,
50
,
50
,
0
,
-
50
},
new
int
[]
{
75
,
75
,
-
25
,
-
75
,
-
25
},
5
);
Point2d
M
=
new
Point2d
();
Canvas
()
{
// events
this
.
addMouseMotionListener
(
new
MouseAdapter
(){
public
void
mouseMoved
(
MouseEvent
e
){
M
.
x
=
e
.
getX
();
M
.
y
=
e
.
getY
();
repaint
();
}});
}
// custom graphics drawing
public
void
paintComponent
(
Graphics
g
)
{
super
.
paintComponent
(
g
);
// JPanel paint
Graphics2D
g2
=
(
Graphics2D
)
g
;
// the shape will get transformed into "world" coordinates
// transform by hand
// get copy of shape
Polygon
transShape
=
new
Polygon
(
shape
.
xpoints
,
shape
.
ypoints
,
shape
.
npoints
);
rotate
(
transShape
,
45
);
scale
(
transShape
,
2
,
1
);
translate
(
transShape
,
M
.
x
,
M
.
y
);
g2
.
setStroke
(
new
BasicStroke
(
3
));
g2
.
drawPolygon
(
transShape
.
xpoints
,
transShape
.
ypoints
,
transShape
.
npoints
);
}
// non-matrix translate
void
translate
(
Polygon
s
,
double
tx
,
double
ty
)
{
for
(
int
i
=
0
;
i
<
s
.
npoints
;
i
++)
{
s
.
xpoints
[
i
]
+=
tx
;
s
.
ypoints
[
i
]
+=
ty
;
}
}
// non-matrix scale
void
scale
(
Polygon
s
,
double
sx
,
double
sy
)
{
for
(
int
i
=
0
;
i
<
s
.
npoints
;
i
++)
{
s
.
xpoints
[
i
]
*=
sx
;
s
.
ypoints
[
i
]
*=
sy
;
}
}
// no-matrix rotate
void
rotate
(
Polygon
s
,
double
theta
)
{
double
rad
=
Math
.
toRadians
(
theta
);
for
(
int
i
=
0
;
i
<
s
.
npoints
;
i
++)
{
int
x
=
s
.
xpoints
[
i
];
int
y
=
s
.
ypoints
[
i
];
s
.
xpoints
[
i
]
=
(
int
)((
double
)
x
*
Math
.
cos
(
rad
)
-
y
*
Math
.
sin
(
rad
));
s
.
ypoints
[
i
]
=
(
int
)((
double
)
x
*
Math
.
sin
(
rad
)
+
y
*
Math
.
cos
(
rad
));
}
}
}
java/2-7-Graphics/makefile
0 → 100644
View file @
e46a9c3f
# super simple makefile
# call it using 'make NAME=name_of_code_file_without_extension'
# (assumes a .java extension)
NAME
=
SimpleDraw
# you may need to pass OS=win to run on windows
OS
=
# HACK: vecmath is included regardless if needed
all
:
@
echo
"Compiling..."
javac
-cp
vecmath.jar
$(NAME)
.java
run
:
all
# windows needs a semicolon
ifeq
($(OS),win)
@echo
"Running on windows ..."
java
-cp
"vecmath.jar;."
$(NAME)
# everyone else likes a colon
else
@echo
"Running ..."
java -cp "vecmath.jar
:
." $(NAME)
endif
clean
:
rm
-rf
*
.class
java/2-7-Graphics/vecmath.jar
0 → 100644
View file @
e46a9c3f
File added
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment