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
| package draw.graphique;
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
public class JCadre extends JPanel
{
public Image image;
public int width, height;
public Graphics2D g2d;
public int x, y, x0, y0;
public JCadre(int width, int height)
{
this.width = width;
this.height = height;
setPreferredSize(new Dimension(width,height));
image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
addMouseMotionListener(new MouseMotionDessiner());
addMouseListener(new MouseDessiner());
}
public void paintComponent(Graphics g)
{
g2d = (Graphics2D)g;
super.paintComponent(g2d);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_SPEED);
g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING,RenderingHints.VALUE_COLOR_RENDER_SPEED);
g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,RenderingHints.VALUE_FRACTIONALMETRICS_OFF);
g2d.setRenderingHint(RenderingHints.KEY_DITHERING,RenderingHints.VALUE_DITHER_DISABLE);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2d.drawImage(image,0,0,this);
}
private class MouseMotionDessiner implements MouseMotionListener
{
public void mouseDragged(MouseEvent e)
{
g2d = (Graphics2D)image.getGraphics();
x = e.getX();
y = e.getY();
g2d.setColor(Color.WHITE);
g2d.setStroke(new BasicStroke(2));
g2d.drawLine(x0,y0,x,y);
x0 = x;
y0 = y;
repaint();
}
public void mouseMoved(MouseEvent e){}
}
private class MouseDessiner implements MouseListener
{
public void mousePressed(MouseEvent e)
{
x0 = e.getX();
y0 = e.getY();
}
public void mouseReleased(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
}
} |
Partager