Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Aparapi Examples
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
We are moving to Forgejo!
You are on a read-only GitLab instance.
Show more breadcrumbs
Marco Stefanetti
Aparapi Examples
Commits
bd0732cf
Commit
bd0732cf
authored
5 years ago
by
Marco Stefanetti
Browse files
Options
Downloads
Patches
Plain Diff
JFrame, JPanel and mouse handling
parent
d8f33bfe
Loading
Loading
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/com/aparapi/examples/mandel/AfGUI.java
+178
-0
178 additions, 0 deletions
src/main/java/com/aparapi/examples/mandel/AfGUI.java
with
178 additions
and
0 deletions
src/main/java/com/aparapi/examples/mandel/AfGUI.java
0 → 100644
+
178
−
0
View file @
bd0732cf
package
com.aparapi.examples.mandel
;
import
java.awt.Color
;
import
java.awt.Graphics
;
import
java.awt.event.MouseAdapter
;
import
java.awt.event.MouseEvent
;
import
java.awt.event.MouseMotionAdapter
;
import
java.awt.event.MouseWheelEvent
;
import
java.awt.event.MouseWheelListener
;
import
java.awt.event.WindowAdapter
;
import
java.awt.event.WindowEvent
;
import
java.awt.image.BufferedImage
;
import
javax.swing.JFrame
;
import
javax.swing.JPanel
;
public
class
AfGUI
{
private
AfMain
main
;
private
JFrame
frame
;
private
int
__COLORS
=
128
;
private
int
__BLACK
=
Color
.
BLACK
.
getRGB
();
private
int
[]
colors
=
new
int
[
__COLORS
];
private
BufferedImage
image
;
private
JPanel
panel
;
boolean
dragging
=
false
;
private
int
px
;
private
int
py
;
public
AfGUI
(
AfMain
_main
)
{
main
=
_main
;
for
(
int
c
=
0
;
c
<
__COLORS
;
c
++)
{
float
hue
=
(
float
)
c
/
(
float
)
__COLORS
;
float
saturation
=
1.0f
;
float
brightness
=
1.0f
;
Color
color
=
Color
.
getHSBColor
(
hue
,
saturation
,
brightness
);
colors
[
c
]
=
color
.
getRGB
();
}
frame
=
new
JFrame
(
"Fractals"
);
frame
.
setSize
(
main
.
W
,
main
.
H
);
frame
.
setBackground
(
Color
.
BLACK
);
frame
.
addWindowListener
(
new
WindowAdapter
()
{
public
void
windowClosing
(
WindowEvent
windowEvent
)
{
System
.
exit
(
0
);
}
});
panel
=
new
JPanel
()
{
private
static
final
long
serialVersionUID
=
-
2006337199526432552L
;
public
void
paint
(
Graphics
g
)
{
g
.
drawImage
(
image
,
0
,
0
,
this
);
}
};
panel
.
setBackground
(
Color
.
BLACK
);
panel
.
setSize
(
main
.
W
,
main
.
H
);
image
=
new
BufferedImage
(
main
.
W
,
main
.
H
,
BufferedImage
.
TYPE_INT_RGB
);
frame
.
add
(
panel
);
frame
.
addMouseWheelListener
(
new
MouseWheelListener
()
{
@Override
public
void
mouseWheelMoved
(
MouseWheelEvent
e
)
{
e
.
consume
();
int
amount
=
(
e
.
getScrollType
()
==
MouseWheelEvent
.
WHEEL_UNIT_SCROLL
)
?
e
.
getUnitsToScroll
()
:
(
e
.
getWheelRotation
()
<
0
?
-
1
:
1
);
System
.
out
.
printf
(
"wheel %d \n"
,
amount
);
float
zoom
=
1
f
+
((
float
)
amount
*
5
/
100
f
);
main
.
move
(
main
.
W
/
2
,
main
.
H
/
2
,
zoom
);
main
.
refresh
();
}
});
frame
.
addMouseListener
(
new
MouseAdapter
()
{
@Override
public
void
mouseClicked
(
MouseEvent
e
)
{
if
(
e
.
getClickCount
()
==
2
&&
!
e
.
isConsumed
())
{
e
.
consume
();
System
.
out
.
println
(
"Double Click"
);
main
.
move
(
e
.
getPoint
().
x
,
main
.
H
-
e
.
getPoint
().
y
,
1
f
);
main
.
refresh
();
}
}
@Override
public
void
mousePressed
(
MouseEvent
e
)
{
System
.
out
.
printf
(
"mouse pressed %d %d \n"
,
e
.
getPoint
().
x
,
e
.
getPoint
().
y
);
px
=
e
.
getPoint
().
x
;
py
=
e
.
getPoint
().
y
;
dragging
=
true
;
}
@Override
public
void
mouseReleased
(
MouseEvent
event
)
{
dragging
=
false
;
}
});
frame
.
addMouseMotionListener
(
new
MouseMotionAdapter
()
{
@Override
public
void
mouseDragged
(
MouseEvent
e
)
{
if
(
dragging
)
{
int
nx
=
e
.
getPoint
().
x
;
int
ny
=
e
.
getPoint
().
y
;
int
dx
=
px
-
nx
;
int
dy
=
py
-
ny
;
System
.
out
.
printf
(
"mouse drag %d,%d \n"
,
dx
,
dy
);
main
.
move
(
main
.
W
/
2
+
dx
,
main
.
H
/
2
-
dy
,
1
f
);
main
.
refresh
();
px
=
nx
;
py
=
ny
;
}
}
});
frame
.
setVisible
(
true
);
int
cw
=
panel
.
getWidth
();
int
ch
=
panel
.
getHeight
();
frame
.
setSize
(
2
*
main
.
W
-
cw
,
2
*
main
.
H
-
ch
);
panel
.
setSize
(
main
.
W
,
main
.
H
);
}
public
void
setColor
(
int
i
,
int
j
,
int
t
)
{
int
c
=
colors
[
t
%
__COLORS
];
if
(
t
>=
main
.
max_iterations
)
c
=
__BLACK
;
image
.
setRGB
(
i
,
main
.
H
-
j
-
1
,
c
);
}
public
void
refresh
()
{
for
(
int
k
=
0
;
k
<
main
.
rgb
.
length
;
k
++)
setColor
(
k
/
main
.
W
,
k
%
main
.
W
,
main
.
rgb
[
k
]);
panel
.
repaint
();
System
.
out
.
printf
(
"canvas refreshed : %d x %d \n"
,
panel
.
getWidth
(),
panel
.
getHeight
());
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment