| 12
 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
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 
 | import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
 
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
 
 
public class DemoShape { 
 
		public static void main(String[] args) {
 
			JFrame frame = new JFrame();
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
 
			JPanel panel = new JPanel();
			panel.setLayout(new BorderLayout());
 
			frame.setContentPane(panel);
 
			TestCanvas canvas = new TestCanvas();
			panel.add(canvas, BorderLayout.CENTER);
 
			panel.add(createControlPanel(canvas), BorderLayout.SOUTH);
 
			frame.setSize(500,200);
			frame.setLocationRelativeTo(null);
 
			frame.setVisible(true);
 
	}
 
	private static Component createControlPanel(final TestCanvas canvas) {
			JPanel panel = new JPanel();
 
			panel.setLayout(new FlowLayout());
 
	 		// une combo pour choisir les shapes bouton pour afficher un rectangle
			DefaultComboBoxModel<AbstractShape> comboModel = new DefaultComboBoxModel<>(createShapes());
			JComboBox<AbstractShape> combo = new JComboBox<>(comboModel);
			combo.addItemListener(new ItemListener() {
 
				@Override
				public void itemStateChanged(ItemEvent e) {
					if ( e.getItem()!=null ) {
						AbstractShape shapeCreator = (AbstractShape) e.getItem();
						Shape shape = shapeCreator.create(10, 10, 100, 100);
						canvas.setShape(shape);
					}
					else {
						canvas.setShape(null);
					}
				}
 
			});
			panel.add(combo);
 
			// une checkbox ou activer ou pas le foreground
			final JCheckBox foregroundCheckbox = new JCheckBox("ForeGround");
			// on initialise un foreground
			foregroundCheckbox.setSelected(true);
			canvas.setForeGroundPaint(Color.BLACK);
			foregroundCheckbox.addChangeListener(new ChangeListener() {
 
				@Override
				public void stateChanged(ChangeEvent e) {
					if ( foregroundCheckbox.isSelected() ) {
						canvas.setForeGroundPaint(Color.BLACK);
					}
					else {
						canvas.setForeGroundPaint(null);
					}
				}
 
			});
			panel.add ( foregroundCheckbox );
 
			// une checkbox ou activer ou pas le background
			final JCheckBox backgroundCheckbox = new JCheckBox("Background");
			backgroundCheckbox.addChangeListener(new ChangeListener() {
 
				@Override
				public void stateChanged(ChangeEvent e) {
					if ( backgroundCheckbox.isSelected() ) {
						canvas.setBackGroundPaint(Color.ORANGE);
					}
					else {
						canvas.setBackGroundPaint(null);
					}
				}
 
			});
			panel.add ( backgroundCheckbox );
 
 
			return panel;
		}
 
	/**
         * Méthode que tu peux enrichir avec de nouvelles shapes...
         * @return
         */
	private static AbstractShape[] createShapes() {
		return new AbstractShape[] {
				new AbstractShape("Aucun") {
					@Override
					public Shape create(double x, double y, double w,
							double h) {
						return null;
					}
				},
				new AbstractShape("Rectangle") {
					@Override
					public Shape create(double x, double y, double w,
							double h) {
						return new Rectangle2D.Double(x, y, w, h);
					}
				},
				new AbstractShape("Cercle") {
					@Override
					public Shape create(double x, double y, double w,
							double h) {
						return new Ellipse2D.Double(x, y, w, h);
					}
				},
		};
	}
 
	private static class TestCanvas extends Canvas {
 
		private Shape shape;
		private Paint fg;
		private Paint bg;
 
		public TestCanvas() {
		}
 
		public void setShape(Shape shape) {
			this.shape=shape;
			repaint();
		}
 
		public void setForeGroundPaint(Paint fg) {
			this.fg=fg;
			repaint();
		}
 
		public void setBackGroundPaint(Paint bg) {
			this.bg=bg;
			repaint();
		}
 
		@Override
		public void paint(Graphics g) {
			super.paint(g);
			if ( shape!=null ) {
				Graphics2D g2d = (Graphics2D)g;
				if ( bg!=null ) {
					g2d.setPaint(bg);
					g2d.fill(shape);
				}
				if ( fg!=null ) {
					g2d.setPaint(fg);
					g2d.draw(shape);
				}
			}
		}
 
 
	}
 
	private static abstract class AbstractShape {
 
		private String name;
 
		public AbstractShape(String name) {
			this.name=name;
		}
 
		public abstract Shape create(double x, double y, double w, double h);
 
		public String toString() {
			return name;
		}
 
 
	}
 
} | 
Partager