1 2 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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
| import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dialog.ModalityType;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.TexturePaint;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class DemoGrilleJava2D extends JPanel {
private static final int TAILLE_SPRITE = 32;
private static final int TAILLE_GRILLE = 10;
private static final int SPRITE_COL = 8;
private static final int SPRITE_LIG = 4;
private Sprite[][] grille;
private Sprite sprite;
private boolean showGrid=true;
private Point mouseLocation;
private Point mouseLocationStart;
public DemoGrilleJava2D( ) {
grille = new Sprite[10][10];
setPreferredSize(new Dimension(TAILLE_GRILLE*TAILLE_SPRITE,TAILLE_GRILLE*TAILLE_SPRITE));
MouseAdapter mouseAdapter = new MouseAdapter() {
private Point start;
@Override
public void mouseClicked(MouseEvent e) {
start = null;
mouseLocationStart = null;
setSprite(mouseToGrille(e.getPoint()));
}
@Override
public void mousePressed(MouseEvent e) {
start = mouseToGrille(e.getPoint());
mouseLocationStart = start;
}
@Override
public void mouseReleased(MouseEvent e) {
mouseLocationStart = null;
setSprites(start, mouseToGrille(e.getPoint()));
}
@Override
public void mouseMoved(MouseEvent e) {
Point lastLocation = mouseLocation;
mouseLocation = mouseToGrille(e.getPoint());
if ( lastLocation==null || !lastLocation.equals(mouseLocation) ) {
repaint();
}
}
@Override
public void mouseDragged(MouseEvent e) {
Point lastLocation = mouseLocation;
mouseLocation = mouseToGrille(e.getPoint());
if ( lastLocation==null || !lastLocation.equals(mouseLocation) ) {
repaint();
}
}
};
addMouseListener(mouseAdapter);
addMouseMotionListener(mouseAdapter);
}
public void setGridVisible(boolean show) {
this.showGrid=show;
repaint();
}
public boolean isGridVisible() {
return this.showGrid;
}
public void setCurrentSprite(Sprite sprite) {
this.sprite=sprite;
}
protected void setSprite(Point position) {
grille[position.x][position.y]=sprite;
repaint();
}
protected void setSprites(Point start, Point end) {
int dx = Math.min(start.x, end.x);
int fx = Math.max(start.x, end.x);
int dy = Math.min(start.y, end.y);
int fy = Math.max(start.y, end.y);
for(int i=dx; i<=fx; i++) {
for(int j=dy; j<=fy; j++) {
if ( i>=0 && i<TAILLE_GRILLE && j>=0 && j<TAILLE_GRILLE ) {
grille[i][j]=sprite;
}
}
}
repaint();
}
protected Point mouseToGrille(Point point) {
int x = point.x / TAILLE_SPRITE;
int y = point.y / TAILLE_SPRITE;
return new Point(x,y);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2dTuile = (Graphics2D)g.create();
if ( mouseLocation!=null && mouseLocationStart!=null ) {
g2dTuile.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f));
}
// on dessine les tuiles déjà posées
for(int i=0; i<grille.length; i++) {
for(int j=0; j<grille[i].length; j++) {
Sprite sprite = grille[i][j];
if ( sprite!=null ) {
sprite.draw(g2dTuile, i*TAILLE_SPRITE, j*TAILLE_SPRITE, this);
}
}
}
g2dTuile.dispose();
// on dessine une grille
if ( showGrid) {
g.setColor(Color.RED);
for(int i=0; i<grille.length; i++) {
for(int j=0; j<grille[i].length; j++) {
g.drawRect(i*TAILLE_SPRITE, j*TAILLE_SPRITE, TAILLE_SPRITE, TAILLE_SPRITE);
}
}
g.drawRect(0, 0, TAILLE_GRILLE*TAILLE_SPRITE-1, TAILLE_GRILLE*TAILLE_SPRITE-1);
}
// puis on dessine la tuile en cours
if ( mouseLocation!=null ) {
Graphics2D g2d = (Graphics2D)g.create();
g2d.setXORMode(Color.RED);
if ( mouseLocationStart!=null ) {
Point start = new Point(mouseLocationStart);
Point end = new Point(mouseLocation);
if ( start.x>end.x ) {
int x = start.x;
start.x=end.x;
end.x=x;
}
if ( start.y>end.y ) {
int y = start.y;
start.y=end.y;
end.y=y;
}
if ( sprite!=null ) {
TexturePaint texturePaint = new TexturePaint(sprite.getBufferedImage(),new Rectangle2D.Double(
start.x*TAILLE_SPRITE,start.y*TAILLE_SPRITE, TAILLE_SPRITE, TAILLE_SPRITE));
g2d.setPaint(texturePaint);
g2d.fillRect(start.x*TAILLE_SPRITE, start.y*TAILLE_SPRITE,
(end.x-start.x+1)*TAILLE_SPRITE, (end.y-start.y+1)*TAILLE_SPRITE);
}
else {
g2d.setColor(Color.BLACK);
g2d.fillRect(start.x*TAILLE_SPRITE, start.y*TAILLE_SPRITE,
(end.x-start.x+1)*TAILLE_SPRITE, (end.y-start.y+1)*TAILLE_SPRITE);
}
}
else {
if ( sprite!=null ) {
sprite.draw(g2d, mouseLocation.x*TAILLE_SPRITE, mouseLocation.y*TAILLE_SPRITE, this);
}
else {
g2d.setColor(Color.BLACK);
g2d.fillRect(mouseLocation.x*TAILLE_SPRITE, mouseLocation.y*TAILLE_SPRITE, TAILLE_SPRITE, TAILLE_SPRITE);
}
}
g2d.dispose();
}
}
public static void main(String[] args) throws IOException {
BufferedImage image = ImageIO.read(new File("fantasy-tileset.png"));
JFrame frame = new JFrame("Démo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DemoGrilleJava2D grille = new DemoGrilleJava2D();
frame.add(grille);
JPanel south = new JPanel(new BorderLayout());
JLabel label = new JLabel("Effacer");
south.add(label,BorderLayout.WEST);
JCheckBox buttonShowGrid = new JCheckBox("Grille");
buttonShowGrid.setSelected(grille.isGridVisible());
buttonShowGrid.addActionListener(e-> grille.setGridVisible(buttonShowGrid.isSelected()));
JButton buttonSprite = new JButton("Sprite");
JButton buttonEfface = new JButton("Effacer");
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING,5,5));
south.add(buttonPanel,BorderLayout.EAST);
buttonPanel.add(buttonShowGrid);
buttonPanel.add(buttonSprite);
buttonPanel.add(buttonEfface);
buttonEfface.addActionListener(e-> {
grille.setCurrentSprite(null);
label.setText("Effacer");
label.setIcon(null);
});
buttonSprite.addActionListener(e->choixSprite(grille,image,label));
frame.add(south, BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static void choixSprite(DemoGrilleJava2D grille, BufferedImage image, JLabel label) {
JList<Sprite> sprites = new JList<>();
sprites.setCellRenderer(new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected,
boolean cellHasFocus) {
return super.getListCellRendererComponent(list, ((Sprite)value).getIcon(), index, isSelected, cellHasFocus);
}
}) ;
DefaultListModel<Sprite> model = new DefaultListModel<>();
for(int i=0; i<SPRITE_LIG; i++) {
for(int j=0; j<SPRITE_COL; j++) {
model.addElement(new Sprite(image, j, i));
}
}
sprites.setModel(model);
sprites.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
sprites.setLayoutOrientation(JList.HORIZONTAL_WRAP);
JDialog dialog = new JDialog(SwingUtilities.getWindowAncestor(grille),"Choix du sprite", ModalityType.DOCUMENT_MODAL);
dialog.getContentPane().add(sprites);
sprites.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if ( !e.getValueIsAdjusting() ) {
grille.setCurrentSprite(sprites.getSelectedValue());
label.setText("");
label.setIcon(sprites.getSelectedValue().getIcon());
dialog.dispose();
}
}
});
dialog.pack();
dialog.setLocationRelativeTo(grille);
dialog.setVisible(true);
}
public static class Sprite {
private final BufferedImage image;
private final int xs;
private final int ys;
private final ImageIcon imageIcon;
private BufferedImage subImage;
public Sprite(BufferedImage image, int x, int y) {
this.image=image;
this.xs=x*TAILLE_SPRITE;
this.ys=y*TAILLE_SPRITE;
subImage = image.getSubimage(xs, ys, TAILLE_SPRITE, TAILLE_SPRITE);
imageIcon = new ImageIcon(subImage);
}
public BufferedImage getBufferedImage() {
return subImage;
}
public ImageIcon getIcon() {
return imageIcon;
}
public void draw(Graphics g, int xd, int yd, ImageObserver observer) {
g.drawImage(image, xd, yd, xd+TAILLE_SPRITE, yd+TAILLE_SPRITE, xs, ys, xs+TAILLE_SPRITE, ys+TAILLE_SPRITE, observer);
}
}
} |