Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
B
Bunny Crossy
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bunny Dash
Bunny Crossy
Commits
a34bbabf
Commit
a34bbabf
authored
8 months ago
by
Simon Peter Marneros
Browse files
Options
Downloads
Patches
Plain Diff
client.java
parent
6d0d2765
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
Client.java
+154
-0
154 additions, 0 deletions
Client.java
with
154 additions
and
0 deletions
Client.java
0 → 100644
+
154
−
0
View file @
a34bbabf
import
javax.swing.*
;
import
java.awt.*
;
import
java.awt.event.*
;
import
java.io.*
;
import
java.net.*
;
public
class
Client
extends
JFrame
{
private
static
final
int
WIDTH
=
800
,
HEIGHT
=
600
;
private
static
final
Color
BACKGROUND_COLOR
=
new
Color
(
135
,
206
,
235
);
// Sky blue
private
Socket
socket
;
private
PrintWriter
out
;
private
BufferedReader
in
;
private
GamePanel
gamePanel
;
private
String
playerId
;
public
Client
(
String
host
,
int
port
)
{
setTitle
(
"Bunny Crossy"
);
setSize
(
WIDTH
,
HEIGHT
);
setDefaultCloseOperation
(
JFrame
.
EXIT_ON_CLOSE
);
setResizable
(
false
);
showStartPage
(
host
,
port
);
}
private
void
showStartPage
(
String
host
,
int
port
)
{
JPanel
startPanel
=
new
JPanel
()
{
@Override
protected
void
paintComponent
(
Graphics
g
)
{
super
.
paintComponent
(
g
);
g
.
setColor
(
BACKGROUND_COLOR
);
g
.
fillRect
(
0
,
0
,
getWidth
(),
getHeight
());
}
};
startPanel
.
setLayout
(
new
BoxLayout
(
startPanel
,
BoxLayout
.
Y_AXIS
));
JLabel
titleLabel
=
new
JLabel
(
"Bunny Crossy"
);
titleLabel
.
setFont
(
new
Font
(
"Arial"
,
Font
.
BOLD
,
48
));
titleLabel
.
setForeground
(
Color
.
WHITE
);
titleLabel
.
setAlignmentX
(
Component
.
CENTER_ALIGNMENT
);
JButton
singlePlayerButton
=
createStyledButton
(
"Single Player"
);
JButton
multiplayerButton
=
createStyledButton
(
"Multiplayer"
);
singlePlayerButton
.
addActionListener
(
e
->
connectToServer
(
host
,
port
,
"SINGLEPLAYER"
));
multiplayerButton
.
addActionListener
(
e
->
connectToServer
(
host
,
port
,
"MULTIPLAYER"
));
startPanel
.
add
(
Box
.
createVerticalGlue
());
startPanel
.
add
(
titleLabel
);
startPanel
.
add
(
Box
.
createRigidArea
(
new
Dimension
(
0
,
50
)));
startPanel
.
add
(
singlePlayerButton
);
startPanel
.
add
(
Box
.
createRigidArea
(
new
Dimension
(
0
,
20
)));
startPanel
.
add
(
multiplayerButton
);
startPanel
.
add
(
Box
.
createVerticalGlue
());
add
(
startPanel
);
setVisible
(
true
);
}
private
JButton
createStyledButton
(
String
text
)
{
JButton
button
=
new
JButton
(
text
);
button
.
setFont
(
new
Font
(
"Arial"
,
Font
.
BOLD
,
24
));
button
.
setAlignmentX
(
Component
.
CENTER_ALIGNMENT
);
button
.
setMaximumSize
(
new
Dimension
(
200
,
50
));
button
.
setBackground
(
new
Color
(
255
,
165
,
0
));
button
.
setForeground
(
Color
.
WHITE
);
button
.
setFocusPainted
(
false
);
button
.
setBorderPainted
(
false
);
return
button
;
}
private
void
connectToServer
(
String
host
,
int
port
,
String
mode
)
{
try
{
socket
=
new
Socket
(
host
,
port
);
out
=
new
PrintWriter
(
socket
.
getOutputStream
(),
true
);
in
=
new
BufferedReader
(
new
InputStreamReader
(
socket
.
getInputStream
()));
out
.
println
(
mode
);
new
Thread
(
this
::
receiveGameState
).
start
();
showGamePanel
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
JOptionPane
.
showMessageDialog
(
this
,
"Failed to connect to the server."
,
"Connection Error"
,
JOptionPane
.
ERROR_MESSAGE
);
}
}
private
void
showGamePanel
()
{
getContentPane
().
removeAll
();
gamePanel
=
new
GamePanel
();
add
(
gamePanel
);
addKeyListener
(
new
KeyAdapter
()
{
public
void
keyPressed
(
KeyEvent
e
)
{
sendCommand
(
e
.
getKeyCode
());
}
});
revalidate
();
repaint
();
requestFocus
();
}
private
void
sendCommand
(
int
keyCode
)
{
String
command
=
switch
(
keyCode
)
{
case
KeyEvent
.
VK_UP
->
"MOVE:UP"
;
case
KeyEvent
.
VK_DOWN
->
"MOVE:DOWN"
;
case
KeyEvent
.
VK_LEFT
->
"MOVE:LEFT"
;
case
KeyEvent
.
VK_RIGHT
->
"MOVE:RIGHT"
;
case
KeyEvent
.
VK_R
->
"RESTART"
;
default
->
null
;
};
if
(
command
!=
null
&&
out
!=
null
)
{
out
.
println
(
playerId
+
":"
+
command
);
}
}
private
void
receiveGameState
()
{
try
{
String
line
;
while
((
line
=
in
.
readLine
())
!=
null
)
{
final
String
finalLine
=
line
;
if
(
finalLine
.
startsWith
(
"ID:"
))
{
playerId
=
finalLine
.
substring
(
3
);
}
else
{
SwingUtilities
.
invokeLater
(()
->
gamePanel
.
updateGameState
(
finalLine
));
}
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
private
class
GamePanel
extends
JPanel
{
private
GameState
gameState
=
new
GameState
();
public
GamePanel
()
{
setBackground
(
Color
.
BLACK
);
}
public
void
updateGameState
(
String
state
)
{
gameState
.
fromString
(
state
);
repaint
();
}
@Override
protected
void
paintComponent
(
Graphics
g
)
{
super
.
paintComponent
(
g
);
gameState
.
render
(
g
);
}
}
public
static
void
main
(
String
[]
args
)
{
SwingUtilities
.
invokeLater
(()
->
new
Client
(
"localhost"
,
12345
));
}
}
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