source: java-rpg-client/gamegui/MultiTextbox.java@ 92d1b2a

Last change on this file since 92d1b2a was 55522be, checked in by dportnoy <dmp1488@…>, 17 years ago

[svn r8]

  • Property mode set to 100644
File size: 4.7 KB
Line 
1package gamegui;
2
3import java.awt.*;
4import java.awt.event.*;
5import java.awt.image.*;
6import java.util.*;
7
8public class MultiTextbox extends Textbox {
9 ArrayList<String> lstStrings;
10 FontMetrics metrics;
11 boolean active;
12
13 public MultiTextbox(String newName, int newX, int newY, int newWidth, int newHeight, String newLabel, boolean isActive, Font newFont, FontMetrics newMetrics) {
14 super(newName, newX, newY, newWidth, newHeight, newLabel, newFont, false);
15
16 lstStrings = new ArrayList<String>();
17 metrics = newMetrics;
18 active = isActive;
19
20 splitString();
21 }
22
23 public void append(String str) {
24 if(getText().equals(""))
25 setText(str);
26 else
27 setText(getText() + "\n" + str);
28
29 splitString();
30
31 if(lstStrings.size()*15+6 > getHeight())
32 getScrollBar().setSize(getScrollBar().getMaxSize()*getHeight()/(lstStrings.size()*15+6));
33 else
34 getScrollBar().setSize(getScrollBar().getMaxSize());
35
36 getScrollBar().setPosition(getScrollBar().getMaxSize()-getScrollBar().getSize());
37 }
38
39 public void clear() {
40 super.clear();
41 lstStrings = new ArrayList<String>();
42 }
43
44 public boolean handleEvent(MouseEvent e) {
45 if(!getScrollBar().handleEvent(e))
46 return false;
47
48 if(e.getY() < getY()+getWidth()) {
49 changeTextStart(-30);
50 }else if(getY()+getHeight()-getWidth() < e.getY()) {
51 changeTextStart(30);
52 }
53
54 return true;
55 }
56
57 public void handleEvent(KeyEvent e) {
58 if(!active)
59 return;
60
61 super.handleEvent(e);
62
63 splitString();
64
65 if(lstStrings.size()*15+6 > getHeight())
66 getScrollBar().setSize(getScrollBar().getMaxSize()*getHeight()/(lstStrings.size()*15+6));
67 else
68 getScrollBar().setSize(getScrollBar().getMaxSize());
69
70 getScrollBar().setPosition(getScrollBar().getMaxSize()-getScrollBar().getSize());
71 }
72
73 private void changeTextStart(int increment) {
74 setTextStart(getTextStart()+increment);
75
76 if(lstStrings.size()*15+6>getHeight() && getTextStart() >= lstStrings.size()*15+6-getHeight()) {
77 setTextStart(lstStrings.size()*15+6-getHeight());
78 getScrollBar().setPosition(getScrollBar().getMaxSize()-getScrollBar().getSize());
79 }else if(getTextStart() < 0 || lstStrings.size()*15+6<=getHeight()) {
80 setTextStart(0);
81 getScrollBar().setPosition(0);
82 }else
83 getScrollBar().setPosition(getTextStart()*getScrollBar().getMaxSize()/(lstStrings.size()*15+6));
84 }
85
86 private void splitString() {
87 String drawnString = getText();
88
89 ArrayList<String> lstTemp = new ArrayList<String>();
90 do {
91 int x = 0;
92 while(x<drawnString.length() && metrics.stringWidth(drawnString.substring(0, x+1))<=getWidth()-10 && !drawnString.substring(x, x+1).equals("\n")) {
93 x++;
94 }
95
96 lstTemp.add(drawnString.substring(0, x));
97
98 if(drawnString.length()>x && drawnString.substring(x, x+1).equals("\n"))
99 drawnString = drawnString.substring(x+1);
100 else
101 drawnString = drawnString.substring(x);
102 }while(metrics.stringWidth(drawnString)>0);
103
104 if(lstTemp.size()*15-getHeight()+6 > 0)
105 setTextStart(lstTemp.size()*15-getHeight()+6);
106 else
107 setTextStart(0);
108
109 lstStrings = lstTemp;
110 }
111
112 public void draw(Graphics g) {
113 GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
114 GraphicsDevice device = env.getDefaultScreenDevice();
115 GraphicsConfiguration gc = device.getDefaultConfiguration();
116
117 BufferedImage source = gc.createCompatibleImage(getWidth(), getHeight());
118 Graphics2D srcGraphics = source.createGraphics();
119
120 if(isSelected() && System.currentTimeMillis() - getLastCursorChange() > getBlinkInterval())
121 {
122 if(getCursorState() == 0)
123 setCursorState(1);
124 else
125 setCursorState(0);
126
127 setLastCursorChange(System.currentTimeMillis());
128 }
129
130 srcGraphics.setColor(Color.green);
131 srcGraphics.setFont(getFont());
132
133 int x;
134 for(x=0; x<lstStrings.size(); x++)
135 srcGraphics.drawString(lstStrings.get(x), 5, metrics.getHeight()+x*15-getTextStart());
136
137 x--;
138 if(isSelected() && getCursorState() == 1)
139 srcGraphics.drawLine(metrics.stringWidth(lstStrings.get(x))+6, 5+x*15-getTextStart(), metrics.stringWidth(lstStrings.get(x))+6, metrics.getHeight()+x*15-getTextStart());
140
141 g.setColor(Color.green);
142 g.setFont(getFont());
143
144 g.drawImage(source, getX(), getY(), null);
145 g.drawString(getLabel(), getX() - metrics.stringWidth(getLabel()) - 10, getY() + (getHeight() + metrics.getHeight())/2 - 2);
146
147 g.setColor(Color.red);
148 g.drawRect(getX(), getY(), getWidth(), getHeight());
149
150 getScrollBar().draw(g);
151 }
152}
Note: See TracBrowser for help on using the repository browser.