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
| protected void paintComponent(Graphics g)
{
double a=System.currentTimeMillis();
Graphics2D g2d = (Graphics2D) g.create();
Graphics g2 = g.create();
try
{
if (isDragging & init==false)
{
renderOffscreen(g2d,"dragging");
}
else if(isMoving & init==false)
{
renderOffscreen(g2d,"moving");
}
else
{
renderDirectly(g2);
}
}
finally
{
g2d.dispose();
g2.dispose();
}
double b=System.currentTimeMillis();
System.out.println(b-a); //en général 150ms ... 300ms parfois
}
protected void renderDirectly(Graphics g)
{
init = false;
super.paintComponent(g);
//draw the Items
for (Iterator<Item> it = alItem.iterator(); it.hasNext(); )
{
Item iItem = it.next();
iItem.drawItem(g);
}
}
//create Compatible Images for graphic optimizations
public static BufferedImage createCompatibleImg(BufferedImage pic, String loc)
{
GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
if (pic.getColorModel().equals(gc.getColorModel()))
{
return pic;
}
else
{
BufferedImage newCompatibleImage = gc.createCompatibleImage(pic.getWidth(),pic.getHeight(),pic.getTransparency());
Graphics g = newCompatibleImage.getGraphics();
g.drawImage(pic, 0, 0, null);
g.dispose();
return newCompatibleImage;
}
}
void createPaneImage()
{
Dimension size = this.getSize(); //taille du Panel, de la partie visible de la carte quoi...
if (offscreen == null || offscreen.getWidth() != size.width || offscreen.getHeight() != size.height)
{
GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
offscreen = gc.createCompatibleImage(size.width, size.height);
}
Graphics2D g = offscreen.createGraphics(); //Graphics g changé en g2D
try {
renderDirectly(g);
}
finally {
g.dispose();
}
}
protected void renderOffscreen(Graphics2D g, String mode)
{
super.paintComponent(g);
//draw rules when Drag
if(mode.compareTo("dragging")==0)
{
if (offscreen == null)
{
createPaneImage();
}
if (dragX != 0 || dragY != 0)
{
for (Iterator<Item> it = alItem.iterator(); it.hasNext(); )
{
Item iItem = it.next();
if(iItem.getVisibility()) //if visible (for the box)
{
if(iItem.getLoc()!=null)
g.drawString(iItem.getLoc(), iItem.getLocX(), iItem.getLocY());
g.drawImage(iItem.getImgToDraw(), iItem.getX(), iItem.getY(), iItem.getW(), iItem.getH(), null);
}
}
}
}
//draw rules when Move
else if(mode.compareTo("moving")==0)
{
Shape clip = g.getClip();
final int clipX = clip.getBounds().x;
final int clipY = clip.getBounds().y;
final int clipXwidth = clip.getBounds().x + clip.getBounds().width;
final int clipYheight = clip.getBounds().y + clip.getBounds().height;
for (Iterator<Item> it = alItem.iterator(); it.hasNext(); )
{
Item iItem = it.next();
Pattern pattern_Item = Pattern.compile("^./images/Item/");
Matcher matcher_Item = pattern_Item.matcher(iItem.getPath());
Pattern pattern_Box = Pattern.compile("^./images/Box/");
Matcher matcher_Box = pattern_Box.matcher(iItem.getPath());
if(clipXwidth==getIndexX(getIndex(iItem.getName()))+getIndexW(getIndex(iItem.getName())) & clipX==getIndexX(getIndex(iItem.getName())) & clipYheight==getIndexY(getIndex(iItem.getName()))+getIndexH(getIndex(iItem.getName())) & clipY==getIndexY(getIndex(iItem.getName())))
{
if(matcher_Item.find()==true)
{
//pour vérifier si la zone de clip est bonne... et OUI, elle l'est
System.out.println("clip "+iItem.getName()+" "+clipX+"x"+clipY+" "+clipXwidth+"x"+clipYheight);
g.setColor(new Color(134,55,83));
g.fillRect(clipX-space2pole+iItem.getW()/2,clipY,clipXwidth+2*space2pole+2*iItem.getW(),clipYheight);
if(iItem.getVisibility()) //if visible (for the box)
g.drawImage(iItem.getImgToDraw(), clipX, clipY, iItem.getW(), iItem.getH(), null);
}
if(matcher_Box.find()==true)
{
//draws the BoxClose/Over
g.drawImage(iItem.getImgToDraw(), iItem.getX(), iItem.getY(), null);
}
}
}
}
} |
Partager