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
| public class DemoDrawText extends JPanel {
private final List<Text> texts = new ArrayList<>();
public DemoDrawText() {
setPreferredSize(new Dimension(500,500));
setBackground(Color.WHITE);
setForeground(Color.BLACK);
}
public void addText(String text, Font font, int x, int y) {
addText(new Text(text, font, x, y));
}
public void addText(Text text) {
texts.add(text);
repaint();
}
public void reset() {
texts.clear();
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
texts.forEach(text-> text.draw(g));
}
public static class Text {
public final String text;
public final Font font;
public final int x;
public final int y;
public Text(String text, Font font, int x, int y) {
this.text=Objects.requireNonNull(text);
this.font=Objects.requireNonNull(font);
this.x=x;
this.y=y;
}
private void draw(Graphics g) {
g.setFont(font);
g.drawString(text, x, y);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Démo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DemoDrawText text = new DemoDrawText();
frame.add(text, BorderLayout.CENTER);
JPanel controlPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(2,2,2,2), 0, 0);
gbc.gridwidth=4;
JTextField textfield = new JTextField();
controlPanel.add(textfield,gbc);
Box xyPanel = new Box(BoxLayout.LINE_AXIS);
gbc.gridx=0;
gbc.gridy=1;
controlPanel.add(xyPanel, gbc);
xyPanel.add(new JLabel("x :"));
xyPanel.add(Box.createHorizontalStrut(4));
JFormattedTextField xfield = new JFormattedTextField(NumberFormat.getIntegerInstance());
xfield.setValue(0);
xyPanel.add(xfield);
xyPanel.add(Box.createHorizontalStrut(4));
xyPanel.add(new JLabel("y :"));
xyPanel.add(Box.createHorizontalStrut(4));
JFormattedTextField yfield = new JFormattedTextField(NumberFormat.getIntegerInstance());
yfield.setValue(10);
xyPanel.add(yfield);
GraphicsEnvironment grenv = GraphicsEnvironment.getLocalGraphicsEnvironment();
JComboBox<String> fontCombo = new JComboBox<>(Arrays.stream(grenv.getAllFonts())
.map(Font::getName)
.toArray(String[]::new));
fontCombo.setSelectedItem("Arial");
gbc.gridx=0;
gbc.gridy=5;
gbc.gridwidth=1;
controlPanel.add(fontCombo, gbc);
gbc.gridx=2;
JFormattedTextField sizefield = new JFormattedTextField(NumberFormat.getIntegerInstance());
sizefield.setColumns(5);
controlPanel.add(sizefield, gbc);
sizefield.setValue(12);
gbc.gridx=0;
gbc.gridy=6;
JButton buttonConfirm = new JButton("Confirmer");
controlPanel.add(buttonConfirm, gbc);
buttonConfirm.addActionListener(e-> text.addText(textfield.getText(), createFont((String)fontCombo.getSelectedItem(), (Number)sizefield.getValue()), ((Number)xfield.getValue()).intValue(), ((Number)yfield.getValue()).intValue()));
gbc.gridx=2;
JButton buttonDelete = new JButton("Effacer");
controlPanel.add(buttonDelete, gbc);
buttonDelete.addActionListener(e-> text.reset());
gbc.gridx=0;
gbc.gridy=4;
gbc.gridwidth=1;
gbc.weightx=0;
gbc.anchor=GridBagConstraints.WEST;
gbc.fill=GridBagConstraints.NONE;
controlPanel.add(new JLabel("Police"), gbc);
gbc.gridx=2;
controlPanel.add(new JLabel("Taille"), gbc);
frame.add(controlPanel, BorderLayout.EAST);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static Font createFont(String name, Number size) {
return new Font(name, Font.PLAIN, size.intValue());
}
} |
Partager