Bonjour,

Débutant en java, j'essaie de comprendre mes erreurs dans le bout de programme ci dessous. En fait, si je déclare x,y en static alors ça ne marche pas et si je ne les met pas alors le programme (eclipse) me demande de les mettre !! si vous pouvez m'aiguiller ou me donner un lien simple pour comprendre l'histoire des static/déclaration/fonctions.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
import java.io.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.swing.*;
 
 
 
public class Example extends Applet implements MouseListener, MouseMotionListener{
	int x,y,xd,yd;
 
 
    public static BufferedImage createTextImage(String text, Font font) {
        //You may want to change these setting, or make them parameters
        boolean isAntiAliased = true;
        boolean usesFractionalMetrics = false;
        FontRenderContext frc = new FontRenderContext(null, isAntiAliased, usesFractionalMetrics);
        TextLayout layout = new TextLayout(text, font, frc);
        Rectangle2D bounds = layout.getBounds();
        int w = (int) Math.ceil(bounds.getWidth());
        int h = (int) Math.ceil(bounds.getHeight());
        BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY); //for example;
        Graphics2D g = image.createGraphics();
        g.setColor(Color.BLACK);
        g.fillRect(0,0,w,h);
        g.setColor(Color.WHITE);
        g.setFont(font);
        Object antiAliased = isAntiAliased?
            RenderingHints.VALUE_TEXT_ANTIALIAS_ON : RenderingHints.VALUE_TEXT_ANTIALIAS_OFF;
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, antiAliased);
        Object fractionalMetrics = usesFractionalMetrics?
            RenderingHints.VALUE_FRACTIONALMETRICS_ON : RenderingHints.VALUE_FRACTIONALMETRICS_OFF;
        g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, fractionalMetrics);
        g.drawString(text, (float) - bounds.getX(), (float) - bounds.getY());
        g.dispose();
        return image;
 
 
    }
 
    public static void main(String[] args) throws IOException {
 
        BufferedImage image = drawLL();
        /*ImageIO.write(image, "jpeg", new File("example.jpeg"));
        JLabel label = new JLabel(new ImageIcon(image));
        label.setBorder(BorderFactory.createTitledBorder("Here is the image, bounds brought to you by TextLayout.getBounds"));
 */
        //drawLL();
        ImageIO.write(image, "jpeg", new File("example.jpeg"));
        JLabel label2 = new JLabel(new ImageIcon(image));
        label2.setBorder(BorderFactory.createTitledBorder("Here is the image, bounds brought to you by TextLayout.getBounds"));
 
 
        final JFrame f = new JFrame("Example");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container cp = f.getContentPane();
        cp.add(label2);
        f.pack();
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                f.setLocationRelativeTo(null);
                f.setVisible(true);
            }
        });
    }
    private  BufferedImage drawLL () {
		// TODO Auto-generated method stub
    	System.out.println("a");
    	boolean isAntiAliased = true;
        boolean usesFractionalMetrics = false;
    	FontRenderContext frc = new FontRenderContext(null, isAntiAliased, usesFractionalMetrics);
        TextLayout layout = new TextLayout("toto", new Font("Lucida Bright", Font.ITALIC, 42), frc);
        Rectangle2D bounds = layout.getBounds();
    	int w = (int) Math.ceil(bounds.getWidth());
        int h = (int) Math.ceil(bounds.getHeight());
    	BufferedImage image = new BufferedImage(1000,800, BufferedImage.TYPE_BYTE_GRAY); //for example;
        Graphics2D g = image.createGraphics();
        g.setColor(Color.white);
        g.setStroke(new BasicStroke(10.f, BasicStroke.JOIN_ROUND, BasicStroke.JOIN_ROUND, 5.f));
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, // Anti-alias!
                RenderingHints.VALUE_ANTIALIAS_ON);
        g.drawLine(10, 10, 200, 300);
 
        g.drawLine(xd,yd,x,y);
        System.out.println("x=" + x + "y=" + y);
        xd = x; yd = y;
 
        g.dispose();
        return image;
	}
 
    public void paint(Graphics g) {
 
    	g.drawLine(xd,yd,x,y);
 
        xd = x; yd = y;
    }
 
		@Override
		public void mouseClicked(MouseEvent arg0) {
			// TODO Auto-generated method stub
			xd = arg0.getX(); yd = arg0.getY();
		}
 
		@Override
		public void mouseEntered(MouseEvent arg0) {
			// TODO Auto-generated method stub
 
		}
 
		@Override
		public void mouseExited(MouseEvent arg0) {
			// TODO Auto-generated method stub
 
		}
 
		@Override
		public void mousePressed(MouseEvent arg0) {
			// TODO Auto-generated method stub
 
		}
 
		@Override
		public void mouseReleased(MouseEvent arg0) {
			// TODO Auto-generated method stub
 
		}
 
		@Override
		public void mouseDragged(MouseEvent arg0) {
			// TODO Auto-generated method stub
			x = arg0.getX(); y = arg0.getY();
	    	//repaint(5000);
		}
 
		@Override
		public void mouseMoved(MouseEvent arg0) {
			// TODO Auto-generated method stub
 
		}
 
}

à mon avis c'est un truc super important que j'ai pas compris !
merci d'avance