java, image, et Rectangle!
Bonjour, je suis un débutant en java et j'ai un petit soussi, j'aimrai bien si vous m'aidez, j'ai fais un petit programme qui dessine des rectangle de type Rectangle, et je veux mettre une image gif ou jpeg comme texture de cet rectangle au lieu de le colorer par la methode: setColor(Color); y a-t-il une possibilité pour faire ca? et merci d'avance.
voici le programme initiale:
Code:
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
| import java.awt.*;
import javax.swing.*;
public class JCanvas extends JPanel {
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.RED);
g.fillRect(10,10,80,80);
g.setColor(Color.BLUE);
g.fillOval(150,50,80,80);
g.setColor(c);
}
}
**********************************
public class Demo1 {
public static void main(String[] args) {
JCanvas jc = new JCanvas();
jc.setBackground(Color.WHITE);
jc.setPreferredSize(new Dimension(400,200));
GUIHelper.showOnFrame(jc,"test");
}
}
**************************************
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class GUIHelper {
public static void showOnFrame(JComponent component, String frameName) {
JFrame frame = new JFrame(frameName);
WindowAdapter wa = new WindowAdapter() {
public void windowClosing(WindowEvent e) { System.exit(0);
}
};
frame.addWindowListener(wa);
frame.getContentPane().add(component); frame.pack(); frame.setVisible(true);
}
} |