FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaExercises/Java 1/swing/DrawGraphs.java at master · biblelamp/JavaExercises · GitHub
biblelamp
/
JavaExercises
Public
Notifications
You must be signed in to change notification settings
Fork
130
Star
153
Code
Issues
1
Pull requests
9
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaExercises
/
Java 1
/
swing
/
DrawGraphs.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
127 lines (111 loc) · 3.97 KB
Breadcrumbs
JavaExercises
/
Java 1
/
swing
/
DrawGraphs.java
Copy path
File metadata and controls
127 lines (111 loc) · 3.97 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* Java. Level 1. Drawing graphs of simple functions
*
* @author Sergey Iryupin
* @version 0.3 dated Aug 14, 2017
*/
import
javax
.
swing
.*;
import
java
.
awt
.*;
import
java
.
awt
.
event
.*;
class
DrawGraphs
extends
JFrame
implements
KeyListener
{
final
String
TITLE_OF_PROGRAM
=
"Drawing graphs of simple functions"
;
final
int
START_LOCATION
=
200
;
final
int
WINDOW_WIDTH
=
550
;
final
int
WINDOW_HEIGTH
=
350
;
final
int
LEFT
=
37
;
// key codes
final
int
UP
=
38
;
final
int
RIGHT
=
39
;
final
int
DOWN
=
40
;
final
int
SIN
=
83
;
// for choosing function
final
int
COS
=
67
;
final
int
TAN
=
84
;
Canvas
canvas
;
// JPanel for painting
float
stretchX
=
1.2f
;
int
stretchY
=
75
;
int
typeGraph
=
SIN
;
// default type of function
public
static
void
main
(
String
[]
args
) {
new
DrawGraphs
();
}
DrawGraphs
() {
setTitle
(
TITLE_OF_PROGRAM
);
setDefaultCloseOperation
(
EXIT_ON_CLOSE
);
setBounds
(
START_LOCATION
,
START_LOCATION
,
WINDOW_WIDTH
,
WINDOW_HEIGTH
);
setResizable
(
false
);
canvas
=
new
Canvas
();
canvas
.
setBackground
(
Color
.
white
);
add
(
BorderLayout
.
CENTER
,
canvas
);
addKeyListener
(
this
);
setVisible
(
true
);
}
@
Override
public
void
keyPressed
(
KeyEvent
event
) { }
@
Override
public
void
keyTyped
(
KeyEvent
event
) { }
@
Override
public
void
keyReleased
(
KeyEvent
event
) {
switch
(
event
.
getKeyCode
()) {
case
LEFT
:
stretchX
-=
0.1
;
canvas
.
repaint
();
break
;
case
RIGHT
:
stretchX
+=
0.1
;
canvas
.
repaint
();
break
;
case
UP
:
stretchY
+=
5
;
canvas
.
repaint
();
break
;
case
DOWN
:
stretchY
-=
5
;
canvas
.
repaint
();
break
;
case
SIN
:
case
COS
:
case
TAN
:
typeGraph
=
event
.
getKeyCode
();
canvas
.
repaint
();
break
;
default
:
System
.
out
.
println
(
event
.
getKeyCode
());
}
}
class
Canvas
extends
JPanel
{
// for painting
@
Override
public
void
paint
(
Graphics
g
) {
super
.
paint
(
g
);
// get the real size
Dimension
d
=
this
.
getSize
();
int
width
= (
int
)
d
.
getWidth
();
int
height
= (
int
)
d
.
getHeight
();
// drawing coordinate lines
g
.
drawLine
(
5
,
height
/
2
,
width
-
6
,
height
/
2
);
g
.
drawLine
(
width
/
2
,
5
,
width
/
2
,
height
-
6
);
// draving arrows Х
g
.
drawLine
(
width
-
6
,
height
/
2
,
width
-
6
-
7
,
height
/
2
-
3
);
g
.
drawLine
(
width
-
6
,
height
/
2
,
width
-
6
-
7
,
height
/
2
+
3
);
// draving arrows Y
g
.
drawLine
(
width
/
2
,
5
,
width
/
2
-
3
,
5
+
7
);
g
.
drawLine
(
width
/
2
,
5
,
width
/
2
+
3
,
5
+
7
);
// drawing name of coordinates
g
.
drawString
(
"X"
,
width
-
15
,
height
/
2
-
8
);
g
.
drawString
(
"Y"
,
width
/
2
-
15
,
20
);
g
.
drawString
(
"Sin | Cos | Tan"
,
10
,
height
-
15
);
// converting degrees to radians
// radians = degrees * Math.PI/180
g
.
setColor
(
Color
.
blue
);
for
(
int
x
= -(
width
/
2
-
10
),
y
=
0
;
x
<
width
/
2
-
10
;
x
++) {
double
radian
=
Math
.
toRadians
(
x
) *
stretchX
;
switch
(
typeGraph
) {
case
SIN
:
g
.
drawString
(
"SIN"
,
10
,
20
);
y
= (
int
) (
Math
.
sin
(
radian
) *
stretchY
);
break
;
case
COS
:
g
.
drawString
(
"COS"
,
10
,
20
);
y
= (
int
) (
Math
.
cos
(
radian
) *
stretchY
);
break
;
case
TAN
:
g
.
drawString
(
"TAN"
,
10
,
20
);
y
= (
int
) (
Math
.
tan
(
radian
) *
stretchY
);
}
g
.
drawRect
(
x
+
width
/
2
,
y
+
height
/
2
,
0
,
0
);
}
}
}
}
Back
|
FazBrowse Home
|
New Git URL