...
 
Commits (9)
No preview for this file type
No preview for this file type
......@@ -38,10 +38,6 @@ public class MonDessin extends JPanel {
super(layout, isDoubleBuffered);
// TODO Auto-generated constructor stub
}
public void paint(Graphics g){
g.setColor(rectangleColor);
g.fillRect(10, 10, 600, 300);
g.drawImage(img, 20,20,null);
}
}
import javax.swing.*;
import java.awt.*;
import boundary.*;
import control.*;
public class TestSwing {
......@@ -8,16 +10,15 @@ public class TestSwing {
* @param args
*/
public static void main(String[] args) {
JFrame MainFrame = new JFrame();
MonControleur Control = new MonControleur();
MaFenetre MainFrame = new MaFenetre(Control);
Dimension preferredSize = new Dimension(1000,1000);
MainFrame.setPreferredSize(preferredSize);
MainFrame.setTitle("Graphisme avec Swing");
MainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MainFrame.setVisible(true);
MonDessin dessin = new MonDessin();
MainFrame.add(dessin);
MainFrame.pack();
......
package boundary;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import control.MonControleur;
public class MaFenetre extends JFrame implements KeyListener{
MonControleur controller;
MonAfficheur afficheur;
public MaFenetre(MonControleur cont){
controller = cont;
afficheur = new MonAfficheur(controller);
this.add(afficheur);
this.addKeyListener(this);
Dimension preferredSize = new Dimension(1000,1000);
this.setPreferredSize(preferredSize);
this.setTitle("Graphisme avec Swing");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void keyPressed(KeyEvent e){}
public void keyReleased(KeyEvent e){
char pressedKey = e.getKeyChar();
if (pressedKey == 'r'){
controller.changeColor(Color.red);
}
else if (pressedKey == 'o'){
controller.changeColor(Color.orange);
}
else if (pressedKey == 'v'){
controller.changeColor(Color.green);
}
afficheur.update();
}
public void keyTyped(KeyEvent e){}
}
package boundary;
import java.awt.Graphics;
import java.awt.image.*;
import control.MonControleur;
import javax.swing.JPanel;
public class MonAfficheur extends JPanel {
MonControleur controller;
public MonAfficheur(MonControleur cont) {
controller = cont;
}
public void update(){
this.repaint();
}
public void paint(Graphics g){
g.setColor(controller.getColor());
g.fillRect(10, 10, 600, 300);
BufferedImage img = controller.getImage();
if(img!=null){
g.drawImage(img, 20,20,null);
}
}
}
package control;
import java.awt.Color;
import java.awt.image.*;
import entity.*;
public class MonControleur {
MaCouleur couleur;
MonImage image;
public MonControleur() {
couleur = new MaCouleur();
}
public void changeColor(Color color){
couleur.changeColor(color);
}
public Color getColor(){
return couleur.get();
}
public BufferedImage newImage(String path){
image = new MonImage(path);
return image.get();
}
public BufferedImage getImage(){
return image.get();
}
}
package entity;
import javax.swing.*;
import java.awt.*;
public class MaCouleur {
Color objColor;
public MaCouleur() {
objColor = Color.white;
}
public void changeColor(Color newcolor){
objColor = newcolor;
}
public Color get(){
return objColor;
}
}
package entity;
import java.awt.*;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class MonImage {
Image myImage;
BufferedImage img = null;
public MonImage(String path) {
try {
img = ImageIO.read(new File(path));
} catch (IOException e) {
}
}
public BufferedImage get(){
return img;
}
public int[] size(){
int [] arr = new int[2];
arr[0] = img.getWidth();
arr[1] = img.getHeight();
return arr;
}
}