1 /********************************************************************************
2 * InternetCafe is a software solution that helps the management of Cybercafes
3 * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005.
4 * Copyright (C) 2006 Guido Angelo Ingenito
5
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 *******************************************************************************/
20 package ui.panel;
21
22 import java.awt.Color;
23 import java.awt.Dimension;
24 import java.awt.Graphics;
25 import java.awt.Rectangle;
26 import java.util.Vector;
27
28 import javax.swing.JPanel;
29
30 @SuppressWarnings("serial")
31 public class MessagePanel extends JPanel {
32
33 private int messageId = 0;
34
35 private int xGap = 10;
36
37 private int yGap = 20;
38
39 private int lineSpace = 20;
40
41 private int maxMessageLength = 0;
42
43 private Vector<Message> message = new Vector<Message>();
44
45 public MessagePanel() {
46
47 }
48
49 public static void drawLevelLabel(String text, Rectangle levelBounds,
50 Graphics graphic) {
51 graphic.setColor(Color.WHITE);
52 graphic.drawString(text, (int) (levelBounds.x + 10),
53 (int) (levelBounds.y + 20));
54 }
55
56 @Override
57 public void paintComponent(Graphics graphics) {
58 super.paintComponent(graphics);
59 Rectangle bounds = this.getBounds();
60 for (int i = 0; i < message.size(); i++) {
61 Message m = message.get(i);
62 if (maxMessageLength < m.text.length())
63 maxMessageLength = m.text.length();
64 graphics.setColor(m.color);
65 graphics.drawString(i + " " + m.text, bounds.x + xGap, bounds.y
66 + yGap + i * (lineSpace));
67 }
68 }
69
70 @Override
71 public Dimension getPreferredSize() {
72 return new Dimension(this.getBounds().x + maxMessageLength * 10, this
73 .getBounds().y
74 + lineSpace * message.size());
75 }
76
77 public void addMessage(String text, Color color) {
78 Message m = new Message(messageId, text, color);
79 this.message.add(messageId, m);
80 messageId++;
81 updateUI();
82 }
83
84 public class Message {
85 int id = 0;
86
87 String text;
88
89 Color color;
90
91 public Message(int id, String text, Color color) {
92 this.id = id;
93 this.text = text;
94 this.color = color;
95 }
96 }
97 }