source: java-rpg-client/gamegui/ScrollList.java@ 55522be

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

[svn r8]

  • Property mode set to 100644
File size: 3.5 KB
Line 
1package gamegui;
2
3import java.awt.*;
4import java.awt.event.*;
5import java.awt.image.BufferedImage;
6import java.util.*;
7
8public class ScrollList extends Member {
9 private ArrayList<Listable> lstObjects;
10 private Listable selectedItem;
11 private Font font;
12 private FontMetrics metrics;
13 private int textStart;
14 private boolean change;
15
16 public ScrollList(String newName, int newX, int newY, int newWidth, int newHeight, Font font, FontMetrics metrics) {
17 super(newName, newX, newY, newWidth, newHeight);
18
19 lstObjects = new ArrayList<Listable>();
20 selectedItem = null;
21 this.font = font;
22 this.metrics = metrics;
23 textStart = 0;
24 change = false;
25 }
26
27 public ArrayList<Listable> getList() {
28 return lstObjects;
29 }
30
31 public Listable getSelected() {
32 return selectedItem;
33 }
34
35 public boolean isChanged() {
36 return change;
37 }
38
39 public void changeHandled() {
40 change = false;
41 }
42
43 public void deselect() {
44 selectedItem = null;
45 }
46
47 public void clear() {
48 lstObjects = new ArrayList<Listable>();
49 selectedItem = null;
50 textStart = 0;
51 changeHandled();
52 }
53
54 public boolean handleEvent(MouseEvent e) {
55 if(getX() < e.getX() && e.getX() < getX()+getWidth()) {
56 if(getY() < e.getY() && getY()-textStart+2 < e.getY() && e.getY() < getY()+getHeight() && e.getY() < getY()+lstObjects.size()*15-textStart+2) {
57 selectedItem = lstObjects.get((e.getY()-getY()+textStart-2)/15);
58 change = true;
59 return true;
60 }
61 }
62
63 if(!getScrollBar().handleEvent(e))
64 return false;
65
66 if(e.getY() < getY()+getScrollBar().getWidth()) {
67 changeTextStart(-30);
68 }else if(getY()+getHeight()-getScrollBar().getWidth() < e.getY()) {
69 changeTextStart(30);
70 }
71
72 return true;
73 }
74
75 private void changeTextStart(int increment) {
76 textStart += increment;
77
78 if(lstObjects.size()*15+6>getHeight() && textStart >= lstObjects.size()*15+6-getHeight()) {
79 textStart = lstObjects.size()*15+6-getHeight();
80 getScrollBar().setPosition(getScrollBar().getMaxSize()-getScrollBar().getSize());
81 }else if(textStart < 0 || lstObjects.size()*15+6<=getHeight()) {
82 textStart = 0;
83 getScrollBar().setPosition(0);
84 }else
85 getScrollBar().setPosition(textStart*getScrollBar().getMaxSize()/(lstObjects.size()*15+6));
86 }
87
88 public void draw(Graphics g) {
89 GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
90 GraphicsDevice device = env.getDefaultScreenDevice();
91 GraphicsConfiguration gc = device.getDefaultConfiguration();
92
93 BufferedImage source = gc.createCompatibleImage(getWidth(), getHeight());
94 Graphics2D srcGraphics = source.createGraphics();
95
96 srcGraphics.setColor(Color.green);
97 srcGraphics.setFont(font);
98
99 for(int x=0; x<lstObjects.size(); x++) {
100 if(selectedItem != null && selectedItem.equals(lstObjects.get(x))) {
101 srcGraphics.setColor(Color.blue);
102 srcGraphics.fillRect(0, x*15-textStart+3, getWidth(), 15);
103 srcGraphics.setColor(Color.green);
104 }
105
106 lstObjects.get(x).drawListString(5, metrics.getHeight()+x*15-textStart, srcGraphics);
107 }
108
109 g.drawImage(source, getX(), getY(), null);
110
111 g.setColor(Color.red);
112 g.drawRect(getX(), getY(), getWidth(), getHeight());
113
114 if(lstObjects.size()*15+6 > getHeight())
115 getScrollBar().setSize(getScrollBar().getMaxSize()*getHeight()/(lstObjects.size()*15+6));
116 else
117 getScrollBar().setSize(getScrollBar().getMaxSize());
118
119 getScrollBar().draw(g);
120 }
121}
Note: See TracBrowser for help on using the repository browser.