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
   |  
package org.gui;
 
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.HashMap;
import java.util.Vector;
 
import javax.swing.JViewport;
import javax.swing.Scrollable;
import javax.swing.SwingConstants;
 
import org.objects.Maille;
 
public class Grille extends Canvas implements Scrollable {
 
    /**
     * 
     */
    private static final long serialVersionUID = -3969910545852295647L;
    private HashMap<String,Maille> elements;
    private Vector<Maille> elem;
 
 
    public Grille(int finesse,int w,int h) {
        super();
 
        this.elem = new Vector<Maille>();
        this.elements = new HashMap<String,Maille>();
 
        for(int i=5;i<=w;i+=finesse)
        {
            for(int j=5;j<=h;j+=finesse) {
                Maille m = new Maille(i+"_"+j,i,j,5,5);
                this.elements.put(i+"_"+j,m);
                this.elem.add(m);
            }
        }
        this.setSize(w, h);
 
    }
 
    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D)g;
        g2.setColor(new Color(166,194,222));
 
        for(Maille m : this.elem)
        {
            g2.draw(m);
        }
    }
 
    public Dimension getPreferredScrollableViewportSize() {
        return getPreferredSize();
    }
 
    public int getScrollableUnitIncrement(Rectangle visibleRect,
        int orientation, int direction) {
        return 10;
    }
 
    public int getScrollableBlockIncrement(Rectangle visibleRect,
        int orientation, int direction) {
        return (orientation == SwingConstants.VERTICAL) 
                    ? visibleRect.height
                    : visibleRect.width;
    }
 
    public boolean getScrollableTracksViewportWidth() {
        if (getParent() instanceof JViewport) {
            return (((JViewport) getParent()).getWidth() > getPreferredSize().width);
        }
        return false;
    }
 
    public boolean getScrollableTracksViewportHeight() {
        if (getParent() instanceof JViewport) {
            return (((JViewport) getParent()).getHeight() > getPreferredSize().height);
        }
        return false;
    }
 
 
 
} |