Skip to content
GitLab
Explore
Sign in
Register
Commits on Source (2)
Add MainWindow and CalculWindow
· 8ac4f263
mickael plichard
authored
Sep 18, 2024
8ac4f263
19/09 08h55
· de20f3ec
mickael plichard
authored
Sep 19, 2024
de20f3ec
Show whitespace changes
Inline
Side-by-side
pom.xml
View file @
de20f3ec
...
...
@@ -5,6 +5,13 @@
<artifactId>
jeux_enfants
</artifactId>
<version>
1.0-SNAPSHOT
</version>
<packaging>
jar
</packaging>
<dependencies>
<dependency>
<groupId>
org.mariadb.jdbc
</groupId>
<artifactId>
mariadb-java-client
</artifactId>
<version>
3.4.1
</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
<maven.compiler.source>
17
</maven.compiler.source>
...
...
src/main/java/beans/Question.java
0 → 100644
View file @
de20f3ec
package
beans
;
import
java.io.Serializable
;
/**
*
* @author Mickael
*/
public
class
Question
implements
Serializable
{
private
static
final
long
serialVersionUID
=
1L
;
private
int
id
;
private
int
number
;
private
String
question
;
private
String
answer
;
public
int
getId
()
{
return
id
;
}
public
void
setId
(
int
id
)
{
this
.
id
=
id
;
}
public
int
getNumber
()
{
return
number
;
}
public
void
setNumber
(
int
number
)
{
this
.
number
=
number
;
}
public
String
getQuestion
()
{
return
question
;
}
public
void
setQuestion
(
String
question
)
{
this
.
question
=
question
;
}
public
String
getAnswer
()
{
return
answer
;
}
public
void
setAnswer
(
String
answer
)
{
this
.
answer
=
answer
;
}
@Override
public
String
toString
()
{
return
"Question{"
+
"id="
+
id
+
", number="
+
number
+
", question="
+
question
+
", answer="
+
answer
+
'}'
;
}
}
src/main/java/groupe3/jeux_enfants/CalculWindow.java
0 → 100644
View file @
de20f3ec
package
groupe3.jeux_enfants
;
import
java.awt.GridLayout
;
import
javax.swing.*
;
import
java.awt.event.ActionEvent
;
import
java.awt.event.ActionListener
;
import
java.util.Random
;
/**
*
* @author Mickael
*/
public
class
CalculWindow
{
private
final
JPanel
calculPanel
;
private
final
JLabel
label
;
private
final
JTextField
txtCalcul
;
private
JTextField
txtAnswer
;
private
final
JButton
btnVerify
,
btnSolution
,
btnNewCalcul
;
private
int
resultat
;
// CONSTRUCTOR
public
CalculWindow
()
{
//GUI ITEMS CREATION
calculPanel
=
new
JPanel
();
calculPanel
.
setLayout
(
new
GridLayout
(
6
,
0
));
label
=
new
JLabel
(
"Combien font:"
);
txtCalcul
=
new
JTextField
();
txtAnswer
=
new
JTextField
();
btnVerify
=
new
JButton
(
"Vérifier"
);
btnSolution
=
new
JButton
(
"Solution"
);
btnNewCalcul
=
new
JButton
(
"Autre Calcul"
);
//ADD GUI ITEM TO THE PANEL
calculPanel
.
add
(
label
);
calculPanel
.
add
(
txtCalcul
);
calculPanel
.
add
(
txtAnswer
);
calculPanel
.
add
(
btnVerify
);
calculPanel
.
add
(
btnSolution
);
calculPanel
.
add
(
btnNewCalcul
);
generateCalcul
();
// EVENTS MANAGEMENT
btnVerify
.
addActionListener
(
new
ActionListener
()
{
@Override
public
void
actionPerformed
(
ActionEvent
e
)
{
verifyAnswer
();
}
});
btnSolution
.
addActionListener
(
new
ActionListener
()
{
@Override
public
void
actionPerformed
(
ActionEvent
e
)
{
txtAnswer
.
setText
(
String
.
valueOf
(
resultat
));
}
});
btnNewCalcul
.
addActionListener
(
new
ActionListener
()
{
@Override
public
void
actionPerformed
(
ActionEvent
e
)
{
generateCalcul
();
}
});
}
private
void
generateCalcul
()
{
// SELECT RANDOM NUMBERS
Random
random
=
new
Random
();
int
a
=
random
.
nextInt
(
10
);
int
b
=
random
.
nextInt
(
10
);
boolean
addition
=
random
.
nextBoolean
();
if
(
addition
)
{
txtCalcul
.
setText
(
a
+
" + "
+
b
);
resultat
=
a
+
b
;
}
else
{
txtCalcul
.
setText
(
a
+
" - "
+
b
);
resultat
=
a
-
b
;
}
txtAnswer
.
setText
(
""
);
}
private
void
verifyAnswer
()
{
try
{
int
response
=
Integer
.
parseInt
(
txtAnswer
.
getText
());
if
(
response
==
resultat
)
{
JOptionPane
.
showMessageDialog
(
calculPanel
,
"Bonne réponse !"
);
}
else
{
JOptionPane
.
showMessageDialog
(
calculPanel
,
"Mauvaise réponse."
);
}
}
catch
(
NumberFormatException
e
)
{
JOptionPane
.
showMessageDialog
(
calculPanel
,
"Veuillez entrer un nombre valide."
);
}
}
public
JPanel
getPanel
()
{
return
calculPanel
;
}
}
src/main/java/groupe3/jeux_enfants/DrawWindow.java
0 → 100644
View file @
de20f3ec
package
groupe3.jeux_enfants
;
import
java.awt.BorderLayout
;
import
java.awt.Color
;
import
java.awt.Dimension
;
import
java.awt.FlowLayout
;
import
java.awt.Graphics
;
import
java.awt.event.ActionEvent
;
import
java.awt.event.ActionListener
;
import
java.awt.event.MouseAdapter
;
import
java.awt.event.MouseEvent
;
import
java.awt.event.MouseMotionAdapter
;
import
javax.swing.JButton
;
import
javax.swing.JLabel
;
import
javax.swing.JPanel
;
public
class
DrawWindow
{
int
x
=
0
,
y
=
0
;
private
final
JPanel
container
;
//window
private
final
JLabel
label
;
//position
private
final
JPanel
drawPanel
;
//dessin
private
final
JButton
btnReset
,
btnBlueColor
,
btnRedColor
;
public
DrawWindow
()
{
container
=
new
JPanel
();
label
=
new
JLabel
();
drawPanel
=
new
JPanel
();
drawPanel
.
setLayout
(
new
FlowLayout
(
FlowLayout
.
CENTER
));
drawPanel
.
setPreferredSize
(
new
Dimension
(
800
,
450
));
drawPanel
.
setBackground
(
Color
.
white
);
btnReset
=
new
JButton
(
"Effacer"
);
btnBlueColor
=
new
JButton
(
"Bleu"
);
btnRedColor
=
new
JButton
(
"Rouge"
);
container
.
setLayout
(
new
BorderLayout
());
//container.add(btnReset, BorderLayout.NORTH);
container
.
add
(
btnBlueColor
,
BorderLayout
.
NORTH
);
//container.add(btnRedColor, BorderLayout.NORTH);
container
.
add
(
label
,
BorderLayout
.
SOUTH
);
container
.
add
(
drawPanel
,
BorderLayout
.
CENTER
);
// EVENTS MANAGEMENT
container
.
addMouseListener
(
new
MouseAdapter
()
{
@Override
public
void
mousePressed
(
MouseEvent
e
)
{
x
=
e
.
getX
();
y
=
e
.
getY
();
setLabel
(
x
,
y
);
}
});
container
.
addMouseMotionListener
(
new
MouseMotionAdapter
()
{
@Override
public
void
mouseDragged
(
MouseEvent
e
)
{
Graphics
g
=
drawPanel
.
getGraphics
();
g
.
drawLine
(
x
,
y
,
e
.
getX
(),
e
.
getY
());
x
=
e
.
getX
();
y
=
e
.
getY
();
setLabel
(
x
,
y
);
}
});
setLabel
(
x
,
y
);
btnReset
.
addActionListener
(
new
ActionListener
()
{
@Override
public
void
actionPerformed
(
ActionEvent
e
)
{
resetDrawPanel
();
}
});
}
private
void
setLabel
(
int
x
,
int
y
)
{
label
.
setText
(
"x="
+
x
+
", y="
+
y
);
}
public
JPanel
getPanel
()
{
return
container
;
}
private
void
resetDrawPanel
()
{
drawPanel
.
removeAll
();
drawPanel
.
revalidate
();
drawPanel
.
repaint
();
}
}
src/main/java/groupe3/jeux_enfants/Jeux_enfants.java
View file @
de20f3ec
...
...
@@ -4,6 +4,10 @@
package
groupe3.jeux_enfants
;
import
java.sql.Connection
;
import
javax.swing.JFrame
;
import
sql.MariaDbConnection
;
/**
*
* @author
...
...
@@ -11,6 +15,10 @@ package groupe3.jeux_enfants;
public
class
Jeux_enfants
{
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
"Hello World!"
);
Connection
connection
=
MariaDbConnection
.
getInstance
();
System
.
out
.
println
(
"Connexion établie"
);
MariaDbConnection
.
closeConnection
();
System
.
out
.
println
(
"Connexion nettoyée"
);
JFrame
mainwindow
=
new
MainWindow
();
}
}
src/main/java/groupe3/jeux_enfants/MainWindow.java
0 → 100644
View file @
de20f3ec
package
groupe3.jeux_enfants
;
import
java.awt.BorderLayout
;
import
java.awt.Dimension
;
import
java.awt.FlowLayout
;
import
java.awt.event.ActionEvent
;
import
java.awt.event.ActionListener
;
import
javax.swing.JFrame
;
import
javax.swing.JMenu
;
import
javax.swing.JMenuBar
;
import
javax.swing.JMenuItem
;
import
javax.swing.JPanel
;
/**
*
* @author Mickael
*/
public
class
MainWindow
extends
JFrame
{
//Creation of an activity array to manage the panels
//index 0: Drawing panel
//index 1: Calcul panel
//index 2: Questions and Answers panel"
private
JPanel
[]
activities
=
new
JPanel
[
3
];
private
final
JPanel
mainPanel
;
// CONSTRUCTOR
public
MainWindow
()
{
setTitle
(
"JEUX D'ENFANTS"
);
setSize
(
800
,
600
);
setDefaultCloseOperation
(
JFrame
.
EXIT_ON_CLOSE
);
initActivities
();
createMenu
();
mainPanel
=
new
JPanel
();
mainPanel
.
setLayout
(
new
FlowLayout
(
FlowLayout
.
CENTER
));
setContentPane
(
mainPanel
);
setVisible
(
true
);
}
private
void
initActivities
()
{
//Store each panel in the activity list
activities
[
0
]
=
new
DrawWindow
().
getPanel
();
activities
[
1
]
=
new
CalculWindow
().
getPanel
();
//activities[2] = new QuestionWindow().getPanel();
}
private
JMenuBar
createMenu
()
{
JMenuBar
menuBar
=
new
JMenuBar
();
menuBar
.
setPreferredSize
(
new
Dimension
(
800
,
40
));
setJMenuBar
(
menuBar
);
JMenu
menuActivities
=
new
JMenu
(
"Activités"
);
// DRAWING TAB
JMenuItem
menuItemDrawing
=
new
JMenuItem
(
"Ardoise Magique"
);
// DRAWING TAB EVENT
menuItemDrawing
.
addActionListener
(
new
ActionListener
()
{
@Override
public
void
actionPerformed
(
ActionEvent
e
)
{
changeActivity
(
0
);
}
});
// CALCUL TAB
JMenuItem
menuItemCalcul
=
new
JMenuItem
(
"Calcul"
);
// CALCUL TAB EVENT
menuItemCalcul
.
addActionListener
(
new
ActionListener
()
{
@Override
public
void
actionPerformed
(
ActionEvent
e
)
{
changeActivity
(
1
);
}
});
// QUESTION TAB
JMenuItem
menuItemQuestion
=
new
JMenuItem
(
"Questions-Réponses"
);
// QUESTION TAB EVENT
menuItemQuestion
.
addActionListener
(
new
ActionListener
()
{
@Override
public
void
actionPerformed
(
ActionEvent
e
)
{
changeActivity
(
2
);
}
});
//QUIT BUTTON
JMenuItem
menuQuit
=
new
JMenuItem
(
"Quitter"
);
// QUIT BUTTON EVENT
menuQuit
.
addActionListener
(
new
ActionListener
()
{
@Override
public
void
actionPerformed
(
ActionEvent
e
)
{
System
.
exit
(
0
);
// Ferme l'application
}
});
// ADD TAB TO MENU
menuActivities
.
add
(
menuItemDrawing
);
menuActivities
.
add
(
menuItemCalcul
);
menuActivities
.
add
(
menuItemQuestion
);
menuBar
.
add
(
menuActivities
);
// Ajout du menu Activités
menuBar
.
add
(
menuQuit
);
// Ajout du menu Fichier
return
menuBar
;
// Retourne la barre de menu complète
}
private
void
changeActivity
(
int
index
)
{
//Switch from one panel to another by navigating through the array using the index
mainPanel
.
removeAll
();
mainPanel
.
add
(
activities
[
index
],
BorderLayout
.
CENTER
);
mainPanel
.
revalidate
();
mainPanel
.
repaint
();
}
}
src/main/java/sql/MariaDbConnection.java
0 → 100644
View file @
de20f3ec
package
sql
;
import
java.sql.Connection
;
import
java.sql.DriverManager
;
import
java.sql.SQLException
;
import
java.util.logging.Level
;
import
java.util.logging.Logger
;
/**
* Classe implémentant le pattern singleton pour la connexion à la DB.
*
* @author Herbert Caffarel
*/
public
final
class
MariaDbConnection
{
private
static
Connection
instance
;
private
MariaDbConnection
()
{
}
public
static
Connection
getInstance
()
throws
RuntimeException
{
if
(
instance
==
null
)
{
// La connexion n'existe pas encore, je la crée
try
{
// charger le driver. Inutile en principe...
Class
.
forName
(
"org.mariadb.jdbc.Driver"
);
}
catch
(
ClassNotFoundException
ex
)
{
throw
new
RuntimeException
(
"Driver introuvable"
);
}
String
server
=
"wp.ldnr.fr"
;
String
port
=
"3306"
;
String
dbname
=
"cda202401_jse_g3"
;
String
user
=
"cda202401_jse_g3"
;
String
pwd
=
"vCvpleBaXVewh0Ch"
;
String
url
=
"jdbc:mariadb://"
+
server
+
":"
+
port
+
"/"
+
dbname
;
try
{
instance
=
DriverManager
.
getConnection
(
url
,
user
,
pwd
);
}
catch
(
SQLException
ex
)
{
throw
new
RuntimeException
(
"Connexion à la base de donnée impossible avec le message : "
+
ex
.
getMessage
());
}
}
return
instance
;
}
public
static
void
closeConnection
()
{
if
(
instance
!=
null
)
{
try
{
instance
.
close
();
}
catch
(
SQLException
ex
)
{
Logger
.
getLogger
(
MariaDbConnection
.
class
.
getName
()).
log
(
Level
.
SEVERE
,
null
,
ex
);
}
}
}
}