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
| public class DemoCases extends JPanel {
private static final long serialVersionUID = 1L;
private static final int DEFAULT_NB_CASES = 3;
private final Coord coord = new Coord();
private int nbCases;
private int highlightedi=-1;
private int highlightedj=-1;
private boolean selected;
public void setNbCases(int nbCases) {
final int old = nbCases;
this.nbCases = Math.max(nbCases, 0);
if ( this.nbCases!=old ) {
repaint();
}
}
public Point getCase(Point point) {
computeCoord();
point.x-=coord.offsetX;
point.y-=coord.offsetY;
int i = point.y/coord.width;
int j = point.x/coord.width;
return new Point(i,j);
}
public void highlightCase(Point p) {
highlightCase(p.x,p.y);
}
public void highlightCase(int i, int j) {
if ( i!=this.highlightedi || j!=this.highlightedj ) {
this.highlightedi=i;
this.highlightedj=j;
repaint();
}
}
public void selectCase(boolean selected) {
if ( this.selected!=selected ) {
this.selected = selected;
repaint();
}
}
@Override
public void paint(Graphics g) {
super.paint(g);
computeCoord();
if ( coord.width!=-1 ) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
for(int i=0; i<nbCases; i++) {
for(int j=0; j<nbCases; j++) {
drawCase(g2d, i, j, coord.offsetX+j*coord.width, coord.offsetY+i*coord.width, coord.width, coord.width);
}
}
}
}
protected void drawCase(Graphics g, int i, int j, int x, int y, int width, int height) {
g.setColor(Color.ORANGE);
g.fillRoundRect(x+2, y+2, width-4, height-4, 8, 8);
if ( i==highlightedi && j==highlightedj ) {
if ( selected ) {
g.setColor(Color.YELLOW);
}
else {
g.setColor(Color.BLUE);
}
}
else {
g.setColor(Color.WHITE);
}
g.fillRoundRect(x+4, y+4, width-8, height-8, 8, 8);
g.setColor(Color.DARK_GRAY);
g.drawRoundRect(x+4, y+4, width-8, height-8, 8, 8);
g.drawRoundRect(x+2, y+2, width-4, height-4, 8, 8);
}
private void computeCoord() {
if ( nbCases>0 ) {
final Dimension size = getSize();
coord.width = Math.min( size.width/nbCases, size.height/nbCases);
final int gridWidth = coord.width * nbCases;
coord.offsetX = ( size.width - gridWidth)/2;
coord.offsetY = ( size.height - gridWidth)/2;
}
else {
coord.width = -1;
}
}
private static class Coord {
public int offsetX;
public int offsetY;
public int width;
}
public static void main(String[] args) {
JFrame frame = new JFrame("Démo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DemoCases casePanel = new DemoCases();
casePanel.setNbCases(DEFAULT_NB_CASES);
JPanel paramPanel = new JPanel(new BorderLayout());
JSpinner spinner = new JSpinner(new SpinnerNumberModel(DEFAULT_NB_CASES, 0, 10, 1) );
paramPanel.add(spinner, BorderLayout.CENTER);
spinner.addChangeListener(e-> {
casePanel.setNbCases((Integer)spinner.getValue());
casePanel.paintImmediately(casePanel.getBounds());
});
JLabel label = new JLabel("Nb case(s) :");
paramPanel.add(label, BorderLayout.WEST);
frame.add( casePanel );
frame.add( paramPanel, BorderLayout.NORTH);
MouseAdapter mouseAdapter = new MouseAdapter() {
@Override
public void mouseExited(java.awt.event.MouseEvent e) {
casePanel.selectCase(false);
casePanel.highlightCase(-1, -1);
}
@Override
public void mouseMoved(MouseEvent e) {
casePanel.highlightCase( casePanel.getCase(e.getPoint()) ) ;
}
@Override
public void mousePressed(MouseEvent e) {
if( e.getButton()==e.BUTTON1 ) {
casePanel.selectCase(true);
}
}
@Override
public void mouseReleased(MouseEvent e) {
if( e.getButton()==e.BUTTON1 ) {
casePanel.selectCase(false);
casePanel.highlightCase( casePanel.getCase(e.getPoint()) ) ;
}
}
};
casePanel.addMouseListener(mouseAdapter);
casePanel.addMouseMotionListener(mouseAdapter);
frame.setSize(300, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
} |
Partager