Changeset 8edd04e in lost-haven for gamegui


Ignore:
Timestamp:
Jun 7, 2020, 3:04:32 PM (4 years ago)
Author:
Dmitry Portnoy <dmitry.portnoy@…>
Branches:
master
Children:
a49176d
Parents:
155577b
Message:

Make the decompiled game code compile successfully

Location:
gamegui
Files:
1 added
15 edited

Legend:

Unmodified
Added
Removed
  • gamegui/Animation.java

    r155577b r8edd04e  
    11package gamegui;
    22
    3 import java.awt.*;
    4 import java.awt.image.*;
    5 import java.util.*;
     3import java.awt.Graphics;
     4import java.awt.image.BufferedImage;
     5import java.util.ArrayList;
    66
    7 public class Animation extends Member
    8 {
    9         ArrayList<BufferedImage> frames;
    10         int currentFrame;
    11         int drawInterval;
    12         long lastFrameChange;
    13        
    14         public Animation(String newName, int newX, int newY, int newWidth, int newHeight, int newInterval)
    15         {
    16                 super(newName, newX, newY, newWidth, newHeight);
    17                
    18                 frames = new ArrayList<BufferedImage>();
    19                 currentFrame = 0;
    20                 drawInterval = newInterval;
    21                 lastFrameChange = 0;
    22         }
    23        
    24         public void addFrame(BufferedImage newFrame)
    25         {
    26                 frames.add(newFrame);
    27         }
    28        
    29         public void draw(Graphics g)
    30         {
    31                 if(System.currentTimeMillis() - lastFrameChange > drawInterval)
    32                 {
    33                         currentFrame++;
    34                         if(currentFrame >= frames.size())
    35                                 currentFrame = 0;
    36                        
    37                         lastFrameChange = System.currentTimeMillis();
    38                 }
    39                
    40         g.drawImage(frames.get(currentFrame), getX(), getY(), null);
    41         }
     7public class Animation extends Member {
     8
     9  public ArrayList<BufferedImage> frames;
     10  public long drawInterval;
     11  public boolean wrap;
     12
     13  int currentFrame;
     14  long lastFrameChange;
     15  boolean reachedEnd;
     16
     17  public Animation(String newName, int newX, int newY, int newWidth, int newHeight, int newInterval, boolean wrap) {
     18    super(newName, newX, newY, newWidth, newHeight);
     19    this.frames = new ArrayList<BufferedImage>();
     20    this.currentFrame = 0;
     21    this.drawInterval = newInterval;
     22    this.lastFrameChange = 0L;
     23    this.reachedEnd = false;
     24    this.wrap = wrap;
     25  }
     26
     27  public Animation(Animation copy) {
     28    super(copy.getName(), copy.getX(), copy.getY(), copy.getWidth(), copy.getHeight());
     29    this.frames = copy.frames;
     30    this.currentFrame = copy.currentFrame;
     31    this.drawInterval = copy.drawInterval;
     32    this.lastFrameChange = 0L;
     33    this.reachedEnd = false;
     34    this.wrap = copy.wrap;
     35  }
     36
     37  public void addFrame(BufferedImage newFrame) {
     38    this.frames.add(newFrame);
     39    setWidth(newFrame.getWidth());
     40    setHeight(newFrame.getHeight());
     41  }
     42
     43  public void draw(Graphics g) {
     44    if (this.lastFrameChange == 0L)
     45      this.lastFrameChange = System.currentTimeMillis();
     46    if (System.currentTimeMillis() - this.lastFrameChange > this.drawInterval) {
     47      this.currentFrame++;
     48      if (this.currentFrame >= this.frames.size()) {
     49        if (this.wrap) {
     50          this.currentFrame = 0;
     51        } else {
     52          this.currentFrame--;
     53        }
     54        this.reachedEnd = true;
     55      }
     56      this.lastFrameChange = System.currentTimeMillis();
     57    }
     58    g.drawImage(this.frames.get(this.currentFrame), getX(), getY(), null);
     59  }
     60
     61  public void draw(Graphics g, int x, int y) {
     62    if (this.lastFrameChange == 0L)
     63      this.lastFrameChange = System.currentTimeMillis();
     64    if (System.currentTimeMillis() - this.lastFrameChange > this.drawInterval) {
     65      this.currentFrame++;
     66      if (this.currentFrame >= this.frames.size()) {
     67        if (this.wrap) {
     68          this.currentFrame = 0;
     69        } else {
     70          this.currentFrame--;
     71        }
     72        this.reachedEnd = true;
     73      }
     74      this.lastFrameChange = System.currentTimeMillis();
     75    }
     76    g.drawImage(this.frames.get(this.currentFrame), getX() + x - ((BufferedImage)this.frames.get(this.currentFrame)).getWidth() / 2, getY() + y - ((BufferedImage)this.frames.get(this.currentFrame)).getHeight(), null);
     77  }
     78
     79  public boolean reachedEnd() {
     80    return this.reachedEnd;
     81  }
     82
     83  public void reset() {
     84    this.reachedEnd = false;
     85    this.currentFrame = 0;
     86    this.lastFrameChange = 0L;
     87  }
     88
     89  public int getWidth() {
     90    return ((BufferedImage)this.frames.get(this.currentFrame)).getWidth();
     91  }
     92
     93  public int getHeight() {
     94    return ((BufferedImage)this.frames.get(this.currentFrame)).getHeight();
     95  }
    4296}
  • gamegui/Button.java

    r155577b r8edd04e  
    22
    33import java.awt.*;
     4import java.awt.image.BufferedImage;
    45
    5 public class Button extends Member
    6 {
    7         private String text;
    8         private Font font;
    9        
    10         public Button(String newName, int newX, int newY, int newWidth, int newHeight, String newText, Font newFont)
    11         {
    12                 super(newName, newX, newY, newWidth, newHeight);
    13                
    14                 text = newText;
    15                 font = newFont;
    16         }
    17        
    18         public void draw(Graphics g)
    19         {
    20                 FontMetrics metrics = g.getFontMetrics(font);
    21                
    22                 g.setColor(Color.red);
    23                 g.drawRect(getX(), getY(), getWidth(), getHeight());
    24                
    25                 g.setColor(Color.green);
    26                 g.setFont(font);
    27                 g.drawString(text, getX() + (getWidth() - metrics.stringWidth(text))/2, getY() + (getHeight() + metrics.getHeight())/2 - 2);
    28         }
    29        
    30         public boolean isClicked(int xCoord, int yCoord)
    31         {
    32                 if(xCoord < getX() || getX() + getWidth() < xCoord)
    33                         return false;
    34                 if(yCoord < getY() || getY() + getHeight() < yCoord)
    35                         return false;
    36                
    37                         return true;
    38         }
     6public class Button extends Member {
     7
     8  private String text; 
     9  private Font font;
     10  private BufferedImage img;
     11  private Align alignment;
     12
     13  public Button(String newName, int newX, int newY, int newWidth, int newHeight, String newText, Font newFont) {
     14    super(newName, newX, newY, newWidth, newHeight);
     15    this.text = newText;
     16    this.font = newFont;
     17    this.img = null;
     18    this.alignment = Align.Left;
     19  }
     20
     21  public Button(String newName, int newX, int newY, int newWidth, int newHeight, String newText, Font newFont, Align alignment) {
     22    super(newName, newX, newY, newWidth, newHeight);
     23    this.text = newText;
     24    this.font = newFont;
     25    this.img = null;
     26    this.alignment = alignment;
     27  }
     28
     29  public Button(String newName, int newX, int newY, int newWidth, int newHeight, BufferedImage img) {
     30    super(newName, newX, newY, newWidth, newHeight);
     31    this.text = "";
     32    this.font = null;
     33    this.img = img;
     34    this.alignment = Align.Left;
     35  }
     36
     37  public void draw(Graphics g) {
     38    if (this.img == null) {
     39      FontMetrics metrics = g.getFontMetrics(this.font);
     40      g.setColor(Color.red);
     41      g.drawRect(getX(), getY(), getWidth(), getHeight());
     42      g.setColor(Color.green);
     43      g.setFont(this.font);
     44      switch (this.alignment) {
     45        case Center:
     46          g.drawString(this.text, getX() + (getWidth() - metrics.stringWidth(this.text)) / 2, getY() + (getHeight() + metrics.getHeight()) / 2 - 2);
     47          break;
     48        case Right:
     49          g.drawString(this.text, getX() + getWidth() - metrics.stringWidth(this.text), getY() + (getHeight() + metrics.getHeight()) / 2 - 2);
     50          break;
     51        case Left:
     52          g.drawString(this.text, getX() + (getWidth() - metrics.stringWidth(this.text)) / 2, getY() + (getHeight() + metrics.getHeight()) / 2 - 2);
     53          break;
     54      }
     55    } else {
     56      g.drawImage(this.img, getX() + (getWidth() - this.img.getWidth()) / 2, getY() + (getHeight() - this.img.getHeight()) / 2 - 2, null);
     57    }
     58  }
    3959}
  • gamegui/Label.java

    r155577b r8edd04e  
    33import java.awt.*;
    44
    5 public class Label extends Member
    6 {
    7         private String text;
    8         private Font font;
    9         private boolean centered;
    10         private boolean fixed;
    11        
    12         public Label(String newName, int newX, int newY, int newWidth, int newHeight, String newText, Font newFont, boolean centered) {
    13                 super(newName, newX, newY, newWidth, newHeight);
    14                
    15                 text = newText;
    16                 font = newFont;
    17                 this.centered = centered;
    18                 fixed = false;
    19         }
    20        
    21         public Label(String newName, int newX, int newY, int newWidth, int newHeight, String newText, Font newFont, boolean centered, boolean fixed) {
    22                 super(newName, newX, newY, newWidth, newHeight);
    23                
    24                 text = newText;
    25                 font = newFont;
    26                 this.centered = centered;
    27                 this.fixed = fixed;
    28         }
    29        
    30         public void setText(String s) {
    31                 text = s;
    32         }
    33        
    34         public void draw(Graphics g) {
    35                 FontMetrics metrics = g.getFontMetrics(font);
    36                
    37                 g.setColor(Color.green);
    38                 g.setFont(font);
    39                
    40                 if(centered)
    41                         g.drawString(text, getX() + (getWidth() - metrics.stringWidth(text))/2, getY() + (getHeight() + metrics.getHeight())/2 - 2);
    42                 else if(fixed)
    43                         g.drawString(text, getX(), getY());
    44                 else
    45                         g.drawString(text, getX(), getY() + (getHeight() + metrics.getHeight())/2 - 2);
    46         }
     5public class Label extends Member {
     6
     7  private String text; 
     8  private Font font;
     9  private Align alignment;
     10
     11  public Label(String newName, int newX, int newY, int newWidth, int newHeight, String newText, Font newFont) {
     12    super(newName, newX, newY, newWidth, newHeight);
     13    this.text = newText;
     14    this.font = newFont;
     15    this.alignment = Align.Center;
     16  }
     17
     18  public Label(String newName, int newX, int newY, int newWidth, int newHeight, String newText, Font newFont, Align alignment) {
     19    super(newName, newX, newY, newWidth, newHeight);
     20    this.text = newText;
     21    this.font = newFont;
     22    this.alignment = alignment;
     23  }
     24
     25  public void setText(String s) {
     26    this.text = s;
     27  }
     28
     29  public void draw(Graphics g) {
     30    FontMetrics metrics = g.getFontMetrics(this.font);
     31    g.setColor(Color.green);
     32    g.setFont(this.font);
     33    switch (this.alignment) {
     34      case Center:
     35        g.drawString(this.text, getX() + (getWidth() - metrics.stringWidth(this.text)) / 2, getY() + (getHeight() + metrics.getHeight()) / 2 - 2);
     36        break;
     37      case Right:
     38        g.drawString(this.text, getX() + getWidth() - metrics.stringWidth(this.text), getY() + (getHeight() + metrics.getHeight()) / 2 - 2);
     39        break;
     40      case Left:
     41        g.drawString(this.text, getX(), getY() + (getHeight() + metrics.getHeight()) / 2 - 2);
     42        break;
     43    }
     44  }
    4745}
  • gamegui/Listable.java

    r155577b r8edd04e  
    11package gamegui;
    22
    3 import java.awt.*;
     3import java.awt.Graphics;
    44
    55public interface Listable {
    6         public void drawListString(int x, int y, Graphics g);
     6
     7  void draw(int paramInt1, int paramInt2, Graphics paramGraphics);
     8  int getHeight();
     9  int getWidth();
     10  int getXOffset();
     11  int getYOffset();
    712}
  • gamegui/Member.java

    r155577b r8edd04e  
    11package gamegui;
    22
    3 import java.awt.*;
    4 import java.awt.event.*;
     3import java.awt.Graphics;
     4import java.awt.event.MouseEvent;
    55
    66public class Member {
    7         private String name;
    8         private int x;
    9         private int y;
    10         private int width;
    11         private int height;
    12         private ScrollBar scrollbar;
    13        
    14         public Member(String newName, int newX, int newY, int newWidth, int newHeight) {
    15                 name = newName;
    16                 x = newX;
    17                 y = newY;
    18                 width = newWidth;
    19                 height = newHeight;
    20         }
    21        
    22         public void draw(Graphics g) {
    237
    24         }
    25        
    26         public boolean handleEvent(MouseEvent e) {
    27                 return false;
    28         }
    29        
    30         public boolean isClicked(int xCoord, int yCoord) {
    31                 return x <= xCoord && xCoord <= x+width && y <= yCoord && yCoord <= y+height;
    32         }
    33        
    34         public void clear() {
    35                
    36         }
    37        
    38         public String getName() {
    39                 return name;
    40         }
    41        
    42         public int getX() {
    43                 return x;
    44         }
    45        
    46         public int getY() {
    47                 return y;
    48         }
    49        
    50         public int getWidth() {
    51                 return width;
    52         }
    53        
    54         public int getHeight() {
    55                 return height;
    56         }
    57        
    58         public ScrollBar getScrollBar() {
    59                 return scrollbar;
    60         }
     8  private String name; 
     9  private int x;
     10  private int y;
     11  private int width;
     12  private int height;
     13  private ScrollBar scrollbar;
    6114
    62         public void addScrollBar(ScrollBar newBar) {
    63                 newBar.offset(x, y);
    64                 scrollbar = newBar;
    65         }
    66        
    67         protected void offset(int xOffset, int yOffset) {
    68                 x += xOffset;
    69                 y += yOffset;
    70                
    71                 if(scrollbar != null)
    72                         scrollbar.offset(xOffset, yOffset);
    73         }
     15  public Member(String newName, int newX, int newY, int newWidth, int newHeight) {
     16    this.name = newName;
     17    this.x = newX;
     18    this.y = newY;
     19    this.width = newWidth;
     20    this.height = newHeight;
     21  }
     22
     23  public void draw(Graphics g) {}
     24
     25  public boolean handleEvent(MouseEvent e) {
     26    return false;
     27  }
     28
     29  public boolean isClicked(int xCoord, int yCoord) {
     30    return (this.x <= xCoord && xCoord <= this.x + this.width && this.y <= yCoord && yCoord <= this.y + this.height);
     31  }
     32
     33  public void clear() {
     34  }
     35
     36  public String getName() {
     37    return this.name;
     38  }
     39
     40  public int getX() {
     41    return this.x;
     42  }
     43
     44  public int getY() {
     45    return this.y;
     46  }
     47
     48  public int getWidth() {
     49    return this.width;
     50  }
     51
     52  public int getHeight() {
     53    return this.height;
     54  }
     55
     56  public ScrollBar getScrollBar() {
     57    return this.scrollbar;
     58  }
     59
     60  public void setWidth(int width) {
     61    this.width = width;
     62  }
     63
     64  public void setHeight(int height) {
     65    this.height = height;
     66  }
     67
     68  public void addScrollBar(ScrollBar newBar) {
     69    newBar.offset(this.x, this.y);
     70    this.scrollbar = newBar;
     71  }
     72
     73  protected void offset(int xOffset, int yOffset) {
     74    this.x += xOffset;
     75    this.y += yOffset;
     76    if (this.scrollbar != null) {
     77      this.scrollbar.offset(xOffset, yOffset);
     78    }
     79  }
    7480}
  • gamegui/Menu.java

    r155577b r8edd04e  
    11package gamegui;
    22
    3 import java.awt.*;
    4 import java.awt.event.*;
    5 import java.util.*;
     3import java.awt.Color;
     4import java.awt.Font;
     5import java.awt.FontMetrics;
     6import java.awt.Graphics;
     7import java.awt.event.MouseEvent;
     8import java.util.ArrayList;
    69
    710public class Menu extends Member {
    8         private ArrayList<String> items;
    9         private int selectedIndex;
    10         private String label;
    11         private Font font;
    12         private boolean open;   //determines if the menu is pulled down
    13         private FontMetrics metrics;
    14        
    15         public Menu(String newName, int newX, int newY, int newWidth, int newHeight, String newLabel, Font newFont) {
    16                 super(newName, newX, newY, newWidth, newHeight);
    17                
    18                 items = new ArrayList<String>();
    19                 selectedIndex = -1;
    20                 label = newLabel;
    21                 font = newFont;
    22                 open = false;
    23         }
    24        
    25         public boolean handleEvent(MouseEvent e) {
    26                 if(getX()+metrics.stringWidth(label)+4 <= e.getX() && e.getX() <= getX()+getWidth() && getY()+getHeight() <= e.getY() && e.getY() <= getY()+getHeight()+15*items.size() && open) {
    27                         selectedIndex = (e.getY()-getY()-getHeight())/15;
    28                         open = false;
    29                         return true;
    30                 }
    31                
    32                 if(getX()+getWidth()-getHeight() <= e.getX() && e.getX() <= getX()+getWidth() && getY() <= e.getY() && e.getY() <= getY()+getHeight()) {
    33                         open = !open;
    34                         return true;
    35                 }
    36        
    37                 return false;
    38         }
    39        
    40         public void clear() {
    41                 if(selectedIndex != -1)
    42                         selectedIndex = 0;
    43         }
    44        
    45         public void draw(Graphics g)
    46         {
    47                 metrics = g.getFontMetrics(font);
    48                
    49                 g.setColor(Color.black);
    50         g.fillRect(getX(), getY(), getWidth(), getHeight());
    51        
    52         g.setColor(Color.red);
    53                 g.drawRect(getX(), getY(), getWidth(), getHeight());
    54                
    55                 g.drawLine(getX()+metrics.stringWidth(label)+4, getY(), getX()+metrics.stringWidth(label)+4, getY()+getHeight());
    56                 g.drawLine(getX()+getWidth()-getHeight(), getY(), getX()+getWidth()-getHeight(), getY()+getHeight());
    57        
    58                 g.drawLine(getX()+getWidth()-getHeight()*17/20, getY()+getHeight()*3/20, getX()+getWidth()-getHeight()*3/20, getY()+getHeight()*3/20);
    59                 g.drawLine(getX()+getWidth()-getHeight()*17/20, getY()+getHeight()*3/20, getX()+getWidth()-getHeight()/2, getY()+getHeight()*17/20);
    60                 g.drawLine(getX()+getWidth()-getHeight()/2, getY()+getHeight()*17/20, getX()+getWidth()-getHeight()*3/20, getY()+getHeight()*3/20);
    61                
    62                 g.setColor(Color.green);
    63                 g.setFont(font);
    64                 g.drawString(label, getX()+2, getY()+(getHeight()+metrics.getHeight())/2-2);
    65                 g.drawString(items.get(selectedIndex), getX()+metrics.stringWidth(label)+8, getY()+(getHeight()+metrics.getHeight())/2-2);
    66                
    67                 if(open) {
    68                         g.setColor(Color.black);
    69                 g.fillRect(getX()+metrics.stringWidth(label)+4, getY()+getHeight(), getWidth()-metrics.stringWidth(label)-4, items.size()*15);
    70                
    71                 g.setColor(Color.red);
    72                         g.drawRect(getX()+metrics.stringWidth(label)+4, getY()+getHeight(), getWidth()-metrics.stringWidth(label)-4, items.size()*15);
    73                
    74                 if(selectedIndex != -1) {
    75                                 g.setColor(Color.blue);
    76                         g.fillRect(getX()+metrics.stringWidth(label)+5, getY()+getHeight()+1+15*selectedIndex, getWidth()-metrics.stringWidth(label)-5, 14);
    77                         }
    78                        
    79                 g.setColor(Color.green);
    80                         for(int x=0; x<items.size(); x++)
    81                                 g.drawString(items.get(x), getX()+metrics.stringWidth(label)+8, getY()+(getHeight()+metrics.getHeight())/2+15*(x+1));
    82                 }
    83         }
    84        
    85         public void add(String newString) {
    86                 selectedIndex = 0;
    87                 items.add(newString);
    88         }
    89        
    90         public String getSelected() {
    91                 return items.get(selectedIndex);
    92         }
     11
     12  private ArrayList<String> items;
     13  private int selectedIndex;
     14  private String label;
     15  private Font font;
     16  private boolean open;
     17  private FontMetrics metrics;
     18
     19  public Menu(String newName, int newX, int newY, int newWidth, int newHeight, String newLabel, Font newFont) {
     20    super(newName, newX, newY, newWidth, newHeight);
     21    this.items = new ArrayList<String>();
     22    this.selectedIndex = -1;
     23    this.label = newLabel;
     24    this.font = newFont;
     25    this.open = false;
     26  }
     27
     28  public boolean handleEvent(MouseEvent e) {
     29    if (getX() + this.metrics.stringWidth(this.label) + 4 <= e.getX() && e.getX() <= getX() + getWidth() && getY() + getHeight() <= e.getY() && e.getY() <= getY() + getHeight() + 15 * this.items.size() && this.open) {
     30      this.selectedIndex = (e.getY() - getY() - getHeight()) / 15;
     31      this.open = false;
     32      return true;
     33    }
     34    if (getX() + getWidth() - getHeight() <= e.getX() && e.getX() <= getX() + getWidth() && getY() <= e.getY() && e.getY() <= getY() + getHeight()) {
     35      this.open = !this.open;
     36      return true;
     37    }
     38    return false;
     39  }
     40
     41  public void clear() {
     42    if (this.selectedIndex != -1)
     43      this.selectedIndex = 0;
     44  }
     45
     46  public void draw(Graphics g) {
     47    this.metrics = g.getFontMetrics(this.font);
     48    g.setColor(Color.black);
     49    g.fillRect(getX(), getY(), getWidth(), getHeight());
     50    g.setColor(Color.red);
     51    g.drawRect(getX(), getY(), getWidth(), getHeight());
     52    g.drawLine(getX() + this.metrics.stringWidth(this.label) + 4, getY(), getX() + this.metrics.stringWidth(this.label) + 4, getY() + getHeight());
     53    g.drawLine(getX() + getWidth() - getHeight(), getY(), getX() + getWidth() - getHeight(), getY() + getHeight());
     54    g.drawLine(getX() + getWidth() - getHeight() * 17 / 20, getY() + getHeight() * 3 / 20, getX() + getWidth() - getHeight() * 3 / 20, getY() + getHeight() * 3 / 20);
     55    g.drawLine(getX() + getWidth() - getHeight() * 17 / 20, getY() + getHeight() * 3 / 20, getX() + getWidth() - getHeight() / 2, getY() + getHeight() * 17 / 20);
     56    g.drawLine(getX() + getWidth() - getHeight() / 2, getY() + getHeight() * 17 / 20, getX() + getWidth() - getHeight() * 3 / 20, getY() + getHeight() * 3 / 20);
     57    g.setColor(Color.green);
     58    g.setFont(this.font);
     59    g.drawString(this.label, getX() + 2, getY() + (getHeight() + this.metrics.getHeight()) / 2 - 2);
     60    g.drawString(this.items.get(this.selectedIndex), getX() + this.metrics.stringWidth(this.label) + 8, getY() + (getHeight() + this.metrics.getHeight()) / 2 - 2);
     61    if (this.open) {
     62      g.setColor(Color.black);
     63      g.fillRect(getX() + this.metrics.stringWidth(this.label) + 4, getY() + getHeight(), getWidth() - this.metrics.stringWidth(this.label) - 4, this.items.size() * 15);
     64      g.setColor(Color.red);
     65      g.drawRect(getX() + this.metrics.stringWidth(this.label) + 4, getY() + getHeight(), getWidth() - this.metrics.stringWidth(this.label) - 4, this.items.size() * 15);
     66      if (this.selectedIndex != -1) {
     67        g.setColor(Color.blue);
     68        g.fillRect(getX() + this.metrics.stringWidth(this.label) + 5, getY() + getHeight() + 1 + 15 * this.selectedIndex, getWidth() - this.metrics.stringWidth(this.label) - 5, 14);
     69      }
     70      g.setColor(Color.green);
     71      for (int x = 0; x < this.items.size(); x++)
     72        g.drawString(this.items.get(x), getX() + this.metrics.stringWidth(this.label) + 8, getY() + (getHeight() + this.metrics.getHeight()) / 2 + 15 * (x + 1));
     73    }
     74  }
     75
     76  public void add(String newString) {
     77    this.selectedIndex = 0;
     78    this.items.add(newString);
     79  }
     80
     81  public String getSelected() {
     82    return this.items.get(this.selectedIndex);
     83  }
    9384}
  • gamegui/MultiTextbox.java

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

    r155577b r8edd04e  
    55
    66public class ProgressBar extends Member {
    7         private int max;
    8         private int current;
    9        
    10         public ProgressBar(String newName, int newX, int newY, int newWidth, int newHeight) {
    11                 super(newName, newX, newY, newWidth, newHeight);
    12                
    13                 max = 1;
    14                 current = 0;
    15         }
    16        
    17         public int getMax() {
    18                 return max;
    19         }
    20        
    21         public int getCurrent() {
    22                 return current;
    23         }
    24        
    25         public void setMax(int max) {
    26                 this.max = max;
    27         }
    28        
    29         public void setCurrent(int current) {
    30                 this.current = current;
    31         }
    32        
    33         public void draw(Graphics g) {
    34                 g.setColor(Color.black);
    35         g.fillRect(getX(), getY(), getWidth(), getHeight());
    36        
    37         g.setColor(Color.blue);
    38                 g.fillRect(getX(), getY(), getWidth()*current/max, getHeight());
    39                 g.setColor(Color.red);
    40                 g.drawRect(getX(), getY(), getWidth(), getHeight());
    41         }
     7
     8  private int max;
     9  private int current;
     10
     11  public ProgressBar(String newName, int newX, int newY, int newWidth, int newHeight) {
     12    super(newName, newX, newY, newWidth, newHeight);
     13    this.max = 1;
     14    this.current = 0;
     15  }
     16
     17  public int getMax() {
     18    return this.max;
     19  }
     20
     21  public int getCurrent() {
     22    return this.current;
     23  }
     24
     25  public void setMax(int max) {
     26    this.max = max;
     27  }
     28
     29  public void setCurrent(int current) {
     30    this.current = current;
     31  }
     32
     33  public void draw(Graphics g) {
     34    g.setColor(Color.black);
     35    g.fillRect(getX(), getY(), getWidth(), getHeight());
     36    g.setColor(Color.blue);
     37    g.fillRect(getX(), getY(), getWidth() * this.current / this.max, getHeight());
     38    g.setColor(Color.red);
     39    g.drawRect(getX(), getY(), getWidth(), getHeight());
     40  }
    4241}
  • gamegui/RadioButton.java

    r155577b r8edd04e  
    11package gamegui;
    22
    3 import java.awt.*;
     3import java.awt.Color;
     4import java.awt.Font;
     5import java.awt.FontMetrics;
     6import java.awt.Graphics;
    47
    5 public class RadioButton extends Member
    6 {
    7         private String text;
    8         private Font font;
    9         private boolean textAfter;
    10         private boolean selected;
    11        
    12         public RadioButton(String newName, int newX, int newY, int newWidth, int newHeight, String newText, Font newFont, boolean isTextAfter)
    13         {
    14                 super(newName, newX, newY, newWidth, newHeight);
    15                
    16                 text = newText;
    17                 font = newFont;
    18                 textAfter = isTextAfter;
    19         }
    20        
    21         public void draw(Graphics g)
    22         {
    23                 FontMetrics metrics = g.getFontMetrics(font);
    24                
    25                 g.setColor(Color.red);
    26                 g.drawOval(getX(), getY(), getWidth(), getHeight());
    27                
    28                 if(selected)
    29                 {
    30                         g.setColor(Color.green);
    31                         g.fillOval(getX() + 5, getY() + 5, getWidth() - 10, getHeight() - 10);
    32                 }
    33                
    34                 g.setColor(Color.green);
    35                 g.setFont(font);
    36                
    37                 if(textAfter)
    38                         g.drawString(text, getX() + getWidth() + 7, getY() + (getHeight() + metrics.getHeight())/2 - 2);
    39                 else
    40                         g.drawString(text, getX() - metrics.stringWidth(text) - 7, getY() + (getHeight() + metrics.getHeight())/2 - 2);
    41         }
    42        
    43         public void clear()
    44         {       
    45                 selected = false;
    46         }
    47        
    48         public String getLabel() {
    49                 return text;
    50         }
    51        
    52         public boolean isClicked(int xCoord, int yCoord)
    53         {
    54                 double distance = Math.sqrt(Math.pow(getX() + getWidth()/2 - xCoord, 2) + Math.pow(getY() +getHeight()/2 - yCoord, 2));
    55                
    56                 return !(distance > getWidth() / 2);
    57         }
    58        
    59         public boolean isSelected()
    60         {
    61                 return selected;
    62         }
    63        
    64         public void setSelected(boolean isSelected)
    65         {
    66                 selected = isSelected;
    67         }
     8public class RadioButton extends Member {
     9
     10  private String text;
     11  private Font font;
     12  private boolean textAfter;
     13  private boolean selected;
     14
     15  public RadioButton(String newName, int newX, int newY, int newWidth, int newHeight, String newText, Font newFont, boolean isTextAfter) {
     16    super(newName, newX, newY, newWidth, newHeight);
     17    this.text = newText;
     18    this.font = newFont;
     19    this.textAfter = isTextAfter;
     20  }
     21
     22  public void draw(Graphics g) {
     23    FontMetrics metrics = g.getFontMetrics(this.font);
     24    g.setColor(Color.red);
     25    g.drawOval(getX(), getY(), getWidth(), getHeight());
     26    if (this.selected) {
     27      g.setColor(Color.green);
     28      g.fillOval(getX() + 5, getY() + 5, getWidth() - 10, getHeight() - 10);
     29    }
     30    g.setColor(Color.green);
     31    g.setFont(this.font);
     32    if (this.textAfter) {
     33      g.drawString(this.text, getX() + getWidth() + 7, getY() + (getHeight() + metrics.getHeight()) / 2 - 2);
     34    } else {
     35      g.drawString(this.text, getX() - metrics.stringWidth(this.text) - 7, getY() + (getHeight() + metrics.getHeight()) / 2 - 2);
     36    }
     37  }
     38
     39  public void clear() {
     40    this.selected = false;
     41  }
     42
     43  public String getLabel() {
     44    return this.text;
     45  }
     46
     47  public boolean isClicked(int xCoord, int yCoord) {
     48    double distance = Math.sqrt(Math.pow((getX() + getWidth() / 2 - xCoord), 2.0D) + Math.pow((getY() + getHeight() / 2 - yCoord), 2.0D));
     49    return !(distance > (getWidth() / 2));
     50  }
     51
     52  public boolean isSelected() {
     53    return this.selected;
     54  }
     55
     56  public void setSelected(boolean isSelected) {
     57    this.selected = isSelected;
     58  }
    6859}
  • gamegui/RadioGroup.java

    r155577b r8edd04e  
    11package gamegui;
    22
    3 import java.awt.*;
    4 import java.awt.event.*;
    5 import java.util.*;
     3import java.awt.Color;
     4import java.awt.Font;
     5import java.awt.FontMetrics;
     6import java.awt.Graphics;
     7import java.awt.event.MouseEvent;
     8import java.util.ArrayList;
    69
     10public class RadioGroup extends Member {
    711
    8 public class RadioGroup extends Member
    9 {
    10         private ArrayList<RadioButton> buttons;
    11         private RadioButton selected;
    12         private String text;
    13         private Font font;
    14        
    15         public RadioGroup(String newName, int newX, int newY, int newWidth, int newHeight, String newText, Font newFont) {
    16                 super(newName, newX, newY, newWidth, newHeight);
    17                
    18                 buttons = new ArrayList<RadioButton>();
    19                 selected = null;
    20                 text = newText;
    21                 font = newFont;
    22         }
    23        
    24         public boolean handleEvent(MouseEvent e) {
    25                 if(selected != null)
    26                         selected.clear();
    27                
    28                 for(int x=0; x < buttons.size(); x++)
    29                         if(((RadioButton)buttons.get(x)).isClicked(e.getX(), e.getY())) {
    30                                 selected = buttons.get(x);
    31                                 selected.setSelected(true);
    32                                 return true;
    33                         }
    34                
    35                 return false;
    36         }
    37        
    38         public void draw(Graphics g)
    39         {
    40                 FontMetrics metrics = g.getFontMetrics(font);
    41                
    42                 g.setColor(Color.green);
    43                 g.setFont(font);
    44                
    45                 g.drawString(text, getX() - metrics.stringWidth(text) - 10, getY() + (getHeight() + metrics.getHeight())/2 - 2);
    46                
    47         for(int x=0; x < buttons.size(); x++)
    48                 ((RadioButton)(buttons.get(x))).draw(g);
    49         }
    50        
    51         public void clear()
    52         {       
    53                 for(int x=0; x < buttons.size(); x++)
    54                 ((RadioButton)(buttons.get(x))).clear();
    55         }
    56        
    57         public void add(RadioButton aButton) {
    58                 buttons.add(aButton);
    59         }
    60        
    61         public RadioButton getButton(String aName) {
    62                 for(int x=0; x < buttons.size(); x++)
    63                 if(buttons.get(x).getName().equals(aName))
    64                         return (RadioButton)buttons.get(x);
    65                
    66                 return null;
    67         }
    68        
    69         public String getSelected() {
    70                 if(selected != null)
    71                         return selected.getName();
    72                 else
    73                         return "None";
    74         }
    75        
    76         public void setSelected(String button) {
    77                 clear();
    78                
    79                 for(int x=0; x < buttons.size(); x++)
    80                 if(buttons.get(x).getName().equals(button))
    81                         buttons.get(x).setSelected(true);
    82         }
     12  private ArrayList<RadioButton> buttons;
     13  private RadioButton selected;
     14  private String text;
     15  private Font font;
     16
     17  public RadioGroup(String newName, int newX, int newY, int newWidth, int newHeight, String newText, Font newFont) {
     18    super(newName, newX, newY, newWidth, newHeight);
     19    this.buttons = new ArrayList<RadioButton>();
     20    this.selected = null;
     21    this.text = newText;
     22    this.font = newFont;
     23  }
     24
     25  public boolean handleEvent(MouseEvent e) {
     26    if (this.selected != null)
     27      this.selected.clear();
     28    for (int x = 0; x < this.buttons.size(); x++) {
     29      if (((RadioButton)this.buttons.get(x)).isClicked(e.getX(), e.getY())) {
     30        this.selected = this.buttons.get(x);
     31        this.selected.setSelected(true);
     32        return true;
     33      }
     34    }
     35    return false;
     36  }
     37
     38  public void draw(Graphics g) {
     39    FontMetrics metrics = g.getFontMetrics(this.font);
     40    g.setColor(Color.green);
     41    g.setFont(this.font);
     42    g.drawString(this.text, getX() - metrics.stringWidth(this.text) - 10, getY() + (getHeight() + metrics.getHeight()) / 2 - 2);
     43    for (int x = 0; x < this.buttons.size(); x++) {
     44      ((RadioButton)this.buttons.get(x)).draw(g);
     45    }
     46  }
     47
     48  public void clear() {
     49    for (int x = 0; x < this.buttons.size(); x++) {}
     50      ((RadioButton)this.buttons.get(x)).clear();
     51    }
     52  }
     53
     54  public void add(RadioButton aButton) {
     55    this.buttons.add(aButton);
     56  }
     57
     58  public RadioButton getButton(String aName) {
     59    for (int x = 0; x < this.buttons.size(); x++) {
     60      if (((RadioButton)this.buttons.get(x)).getName().equals(aName))
     61        return this.buttons.get(x);
     62    }
     63    return null;
     64  }
     65
     66  public String getSelected() {
     67    if (this.selected != null) {
     68      return this.selected.getName();
     69    }
     70    return "None";
     71  }
     72
     73  public void setSelected(String button) {
     74    clear();
     75    for (int x = 0; x < this.buttons.size(); x++) {
     76      if (((RadioButton)this.buttons.get(x)).getName().equals(button))
     77        ((RadioButton)this.buttons.get(x)).setSelected(true);
     78    }
     79  }
    8380}
  • gamegui/ScrollBar.java

    r155577b r8edd04e  
    22
    33import java.awt.*;
    4 import java.awt.event.*;
     4import java.awt.event.MouseEvent;
    55
    66public class ScrollBar extends Member {
    7         int size;
    8         int position;
    9         int scrollSpeed;
    10        
    11         public ScrollBar(String newName, int newX, int newY, int newWidth, int newHeight, int newScrollSpeed) {
    12                 super(newName, newX, newY, newWidth, newHeight);
    13                
    14                 size = 0;
    15                 position = 0;
    16                 scrollSpeed = newScrollSpeed;
    17         }
    18        
    19         public void clear() {
    20                 size = 0;
    21                 position = 0;
    22         }
    23        
    24         public boolean handleEvent(MouseEvent e) {
    25                 if(!(getX() < e.getX() && e.getX() < getX()+getWidth() && getY() < e.getY() && e.getY() < getY()+getHeight()))
    26                         return false;
    27                 else
    28                         return true;
    29         }
    30        
    31         public void draw(Graphics g) {
    32                 g.setColor(Color.black);
    33         g.fillRect(getX(), getY(), getWidth(), getHeight());
    34        
    35         g.setColor(Color.red);
    36                 g.drawRect(getX(), getY(), getWidth(), getHeight());
    37                
    38                 g.drawLine(getX(), getY()+getWidth(), getX()+getWidth(), getY()+getWidth());
    39                 g.drawLine(getX(), getY()+getHeight()-getWidth(), getX()+getWidth(), getY()+getHeight()-getWidth());
    40        
    41                 g.drawLine(getX(), getY()+getWidth()+position, getX()+getWidth(), getY()+getWidth()+position);
    42                 g.drawLine(getX(), getY()+getWidth()+position+size, getX()+getWidth(), getY()+getWidth()+position+size);
    43                
    44                 g.drawLine(getX()+getWidth()*3/20, getY()+getWidth()*17/20, getX()+getWidth()*17/20, getY()+getWidth()*17/20);
    45                 g.drawLine(getX()+getWidth()*17/20, getY()+getWidth()*17/20, getX()+getWidth()/2, getY()+getWidth()*3/20);
    46                 g.drawLine(getX()+getWidth()/2, getY()+getWidth()*3/20, getX()+getWidth()*3/20, getY()+getWidth()*17/20);
    47                
    48                 g.drawLine(getX()+getWidth()*3/20, getY()+getHeight()-getWidth()*17/20, getX()+getWidth()*17/20, getY()+getHeight()-getWidth()*17/20);
    49                 g.drawLine(getX()+getWidth()*17/20, getY()+getHeight()-getWidth()*17/20, getX()+getWidth()/2, getY()+getHeight()-getWidth()*3/20);
    50                 g.drawLine(getX()+getWidth()/2, getY()+getHeight()-getWidth()*3/20, getX()+getWidth()*3/20, getY()+getHeight()-getWidth()*17/20);
    51         }
    527
    53         public int getPosition() {
    54                 return position;
    55         }
     8  int size;
     9  int position;
     10  int scrollSpeed;
    5611
    57         public int getScrollSpeed() {
    58                 return scrollSpeed;
    59         }
     12  public ScrollBar(String newName, int newX, int newY, int newWidth, int newHeight, int newScrollSpeed) {
     13    super(newName, newX, newY, newWidth, newHeight);
     14    this.size = 0;
     15    this.position = 0;
     16    this.scrollSpeed = newScrollSpeed;
     17  }
    6018
    61         public int getSize() {
    62                 return size;
    63         }
    64        
    65         public int getMaxSize() {
    66                 return getHeight()-2*getWidth();
    67         }
     19  public void clear() {
     20    this.size = 0;
     21    this.position = 0;
     22  }
    6823
    69         public void setPosition(int position) {
    70                 this.position = position;
    71         }
    72        
    73         public void setSize(int size) {
    74                 this.size = size;
    75         }
     24  public boolean handleEvent(MouseEvent e) {
     25    if (getX() >= e.getX() || e.getX() >= getX() + getWidth() || getY() >= e.getY() || e.getY() >= getY() + getHeight()) {
     26      return false;
     27    }
     28    return true;
     29  }
     30
     31  public void draw(Graphics g) {
     32    g.setColor(Color.black);
     33    g.fillRect(getX(), getY(), getWidth(), getHeight());
     34    g.setColor(Color.red);
     35    g.drawRect(getX(), getY(), getWidth(), getHeight());
     36    g.drawLine(getX(), getY() + getWidth(), getX() + getWidth(), getY() + getWidth());
     37    g.drawLine(getX(), getY() + getHeight() - getWidth(), getX() + getWidth(), getY() + getHeight() - getWidth());
     38    g.drawLine(getX(), getY() + getWidth() + this.position, getX() + getWidth(), getY() + getWidth() + this.position);
     39    g.drawLine(getX(), getY() + getWidth() + this.position + this.size, getX() + getWidth(), getY() + getWidth() + this.position + this.size);
     40    g.drawLine(getX() + getWidth() * 3 / 20, getY() + getWidth() * 17 / 20, getX() + getWidth() * 17 / 20, getY() + getWidth() * 17 / 20);
     41    g.drawLine(getX() + getWidth() * 17 / 20, getY() + getWidth() * 17 / 20, getX() + getWidth() / 2, getY() + getWidth() * 3 / 20);
     42    g.drawLine(getX() + getWidth() / 2, getY() + getWidth() * 3 / 20, getX() + getWidth() * 3 / 20, getY() + getWidth() * 17 / 20);
     43    g.drawLine(getX() + getWidth() * 3 / 20, getY() + getHeight() - getWidth() * 17 / 20, getX() + getWidth() * 17 / 20, getY() + getHeight() - getWidth() * 17 / 20);
     44    g.drawLine(getX() + getWidth() * 17 / 20, getY() + getHeight() - getWidth() * 17 / 20, getX() + getWidth() / 2, getY() + getHeight() - getWidth() * 3 / 20);
     45    g.drawLine(getX() + getWidth() / 2, getY() + getHeight() - getWidth() * 3 / 20, getX() + getWidth() * 3 / 20, getY() + getHeight() - getWidth() * 17 / 20);
     46  }
     47
     48  public int getPosition() {
     49    return this.position;
     50  }
     51
     52  public int getScrollSpeed() {
     53    return this.scrollSpeed;
     54  }
     55
     56  public int getSize() {
     57    return this.size;
     58  }
     59
     60  public int getMaxSize() {
     61    return getHeight() - 2 * getWidth();
     62  }
     63
     64  public void setPosition(int position) {
     65    this.position = position;
     66  }
     67
     68  public void setSize(int size) {
     69    this.size = size;
     70  }
    7671}
  • gamegui/ScrollList.java

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

    r155577b r8edd04e  
    11package gamegui;
    22
    3 import java.awt.*;
    4 import java.awt.event.*;
    5 import java.util.*;
     3import java.awt.Color;
     4import java.awt.Font;
     5import java.awt.FontMetrics;
     6import java.awt.Graphics;
     7import java.awt.event.MouseEvent;
     8import java.util.ArrayList;
    69
    710public class TabbedWindow extends Member {
    8         private ArrayList<Window> windows;
    9         private ArrayList<String> windowLabels;
    10         private Window activeWindow;
    11         private int tabHeight;
    12         private Font tabFont;
    13        
    14         public TabbedWindow(String newName, int newX, int newY, int newWidth, int newHeight, int newTabHeight, Font newFont) {
    15                 super(newName, newX, newY, newWidth, newHeight);
    16                
    17                 windows = new ArrayList<Window>();
    18                 windowLabels = new ArrayList<String>();
    19                 tabHeight = newTabHeight;
    20                 activeWindow = null;
    21                 tabFont = newFont;
    22         }
    23        
    24         public void add(Window newWindow, String name) {
    25                 newWindow.offset(getX(), getY()+tabHeight);
    26                
    27                 if(activeWindow == null)
    28                         activeWindow = newWindow;
    29                 windows.add(newWindow);
    30                 windowLabels.add(name);
    31         }
    32        
    33         public void clear() {
    34                 activeWindow = windows.get(0);
    35                 for(int x=0; x < windows.size(); x++)
    36                 windows.get(x).clear();
    37         }
    38        
    39         public void offset(int xOffset, int yOffset) {
    40                 super.offset(xOffset, yOffset);
    41                
    42                 for(int x=0; x < windows.size(); x++)
    43                 windows.get(x).offset(xOffset, yOffset);
    44         }
    45        
    46         public Window getWindow(String aName) {
    47                 for(int x=0; x < windows.size(); x++)
    48                 if(windows.get(x).getName().equals(aName))
    49                         return (Window)windows.get(x);
    50                
    51                 return null;
    52         }
    53        
    54         public boolean handleEvent(MouseEvent e) {
    55                 for(int x=0; x < windows.size(); x++) {
    56                 if(isClicked(getX() + x*getWidth()/windows.size(), getY(), getWidth()/windows.size(), tabHeight, e.getX(), e.getY())) {
    57                         activeWindow = windows.get(x);
    58                         return true;
    59                         }
    60                 }
    61                        
    62                 return activeWindow.handleEvent(e);
    63         }
    64        
    65         private boolean isClicked(int x, int y, int width, int height, int mouseX, int mouseY) {
    66                 return x <= mouseX && mouseX <= x+width && y <= mouseY && mouseY <= y+height;
    67         }
    68        
    69         public void draw(Graphics g) {
    70                 FontMetrics metrics = g.getFontMetrics(tabFont);
    71                
    72                 g.setColor(Color.black);
    73         g.fillRect(getX(), getY(), getWidth(), getHeight());
    74        
    75                 g.setFont(tabFont);
    76         for(int x=0; x < windows.size(); x++)
    77         {
    78                 g.setColor(Color.green);
    79                 g.drawString(windowLabels.get(x), getX()+x*getWidth()/windows.size()+(getWidth()/windows.size()-metrics.stringWidth(windowLabels.get(x)))/2, getY() + (tabHeight + metrics.getHeight())/2 - 2);
    80                
    81                 g.setColor(Color.red);
    82                         g.drawLine(getX()+x*getWidth()/windows.size(), getY(), getX()+x*getWidth()/windows.size(), getY()+tabHeight);
    83                        
    84                 if(windows.get(x).equals(activeWindow)) {
    85                         ((Member)(windows.get(x))).draw(g);
    86                         g.setColor(Color.red);
    87                         g.drawRect(getX(), getY(), getWidth(), getHeight());
    88                 g.drawLine(getX(), getY()+tabHeight, getX()+x*getWidth()/windows.size(), getY()+tabHeight);
    89                 g.drawLine(getX()+(x+1)*getWidth()/windows.size(), getY()+tabHeight, getX()+getWidth(), getY()+tabHeight);
    90                 }
    91         }
    92         }
    93        
    94         public String getActive() {
    95                 if(activeWindow != null)
    96                         return activeWindow.getName();
    97                 else
    98                         return "";
    99         }
     11
     12  private ArrayList<Window> windows; 
     13  private ArrayList<String> windowLabels;
     14  private Window activeWindow;
     15  private int tabHeight;
     16  private Font tabFont;
     17
     18  public TabbedWindow(String newName, int newX, int newY, int newWidth, int newHeight, int newTabHeight, Font newFont) {
     19    super(newName, newX, newY, newWidth, newHeight);
     20    this.windows = new ArrayList<Window>();
     21    this.windowLabels = new ArrayList<String>();
     22    this.tabHeight = newTabHeight;
     23    this.activeWindow = null;
     24    this.tabFont = newFont;
     25  }
     26
     27  public void add(Window newWindow, String name) {
     28    newWindow.offset(getX(), getY() + this.tabHeight);
     29    if (this.activeWindow == null) {
     30      this.activeWindow = newWindow;
     31    }
     32    this.windows.add(newWindow);
     33    this.windowLabels.add(name);
     34  }
     35
     36  public void clear() {
     37    this.activeWindow = this.windows.get(0);
     38    for (int x = 0; x < this.windows.size(); x++) {
     39      ((Window)this.windows.get(x)).clear();
     40    }
     41  }
     42
     43  public void offset(int xOffset, int yOffset) {
     44    super.offset(xOffset, yOffset);
     45    for (int x = 0; x < this.windows.size(); x++) {
     46      ((Window)this.windows.get(x)).offset(xOffset, yOffset);
     47    }
     48  }
     49
     50  public Window getWindow(String aName) {
     51    for (int x = 0; x < this.windows.size(); x++) {
     52      if (((Window)this.windows.get(x)).getName().equals(aName)) {
     53        return this.windows.get(x);
     54      }
     55    }
     56    return null;
     57  }
     58
     59  public boolean handleEvent(MouseEvent e) {
     60    for (int x = 0; x < this.windows.size(); x++) {
     61      if (isClicked(getX() + x * getWidth() / this.windows.size(), getY(), getWidth() / this.windows.size(), this.tabHeight, e.getX(), e.getY())) {
     62        this.activeWindow = this.windows.get(x);
     63        return true;
     64      }
     65    }
     66    return this.activeWindow.handleEvent(e);
     67  }
     68
     69  private boolean isClicked(int x, int y, int width, int height, int mouseX, int mouseY) {
     70    return (x <= mouseX && mouseX <= x + width && y <= mouseY && mouseY <= y + height);
     71  }
     72
     73  public void draw(Graphics g) {
     74    FontMetrics metrics = g.getFontMetrics(this.tabFont);
     75    g.setColor(Color.black);
     76    g.fillRect(getX(), getY(), getWidth(), getHeight());
     77    g.setFont(this.tabFont);
     78    for (int x = 0; x < this.windows.size(); x++) {
     79      g.setColor(Color.green);
     80      g.drawString(this.windowLabels.get(x), getX() + x * getWidth() / this.windows.size() + (getWidth() / this.windows.size() - metrics.stringWidth(this.windowLabels.get(x))) / 2, getY() + (this.tabHeight + metrics.getHeight()) / 2 - 2);
     81      g.setColor(Color.red);
     82      g.drawLine(getX() + x * getWidth() / this.windows.size(), getY(), getX() + x * getWidth() / this.windows.size(), getY() + this.tabHeight);
     83      if (((Window)this.windows.get(x)).equals(this.activeWindow)) {
     84        ((Member)this.windows.get(x)).draw(g);
     85        g.setColor(Color.red);
     86        g.drawRect(getX(), getY(), getWidth(), getHeight());
     87        g.drawLine(getX(), getY() + this.tabHeight, getX() + x * getWidth() / this.windows.size(), getY() + this.tabHeight);
     88        g.drawLine(getX() + (x + 1) * getWidth() / this.windows.size(), getY() + this.tabHeight, getX() + getWidth(), getY() + this.tabHeight);
     89      }
     90    }
     91  }
     92
     93  public String getActive() {
     94    if (this.activeWindow != null) {
     95      return this.activeWindow.getName();
     96    }
     97    return "";
     98  }
    10099}
  • gamegui/Textbox.java

    r155577b r8edd04e  
    22
    33import java.awt.*;
    4 import java.awt.image.*;
    5 import java.awt.event.*;
     4import java.awt.event.KeyEvent;
     5import java.awt.image.BufferedImage;
    66
    7 public class Textbox extends Member
    8 {
    9         private String label;
    10         private String text;
    11         private Font font;
    12         private int textStart;
    13         private boolean selected;
    14         private int cursorState;
    15         private int blinkInterval;
    16         private long lastCursorChange;
    17         private boolean password;
    18        
    19         public Textbox(String newName, int newX, int newY, int newWidth, int newHeight, String newLabel, Font newFont, boolean isPass) {
    20                 super(newName, newX, newY, newWidth, newHeight);
    21                
    22                 label = new String(newLabel);
    23                 text = new String();
    24                 font = newFont;
    25                 textStart = 0;
    26                 selected = false;
    27                 cursorState = 0;
    28                 blinkInterval = 1000/2;
    29                 password = isPass;
    30         }
    31        
    32         public void handleEvent(KeyEvent e) {
    33                 if(32 <= e.getKeyCode() && e.getKeyCode() <= 127)
    34                 {
    35                         if(e.getKeyCode() == 127)
    36                         {
    37                                 if(text.length() > 0)
    38                                         text = text.substring(0, text.length() - 1);
    39                         }
    40                         else
    41                                 text = text + Character.toString(e.getKeyChar());
     7public class Textbox extends Member {
    428
    43                 }
    44                 else if(e.getKeyCode() == 8)
    45                 {
    46                         if(text.length() > 0)
    47                                 text = text.substring(0, text.length() - 1);
    48                 }
    49         }
    50        
    51         public void draw(Graphics g) {
    52                 String drawnString = new String();
    53                 FontMetrics metrics = g.getFontMetrics(font);
    54                
    55                 GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
    56         GraphicsDevice device = env.getDefaultScreenDevice();
    57         GraphicsConfiguration gc = device.getDefaultConfiguration();
    58        
    59         BufferedImage source = gc.createCompatibleImage(getWidth(), getHeight());
    60         Graphics2D srcGraphics = source.createGraphics();
    61        
    62         if(selected && System.currentTimeMillis() - lastCursorChange > blinkInterval)
    63                 {
    64                         if(cursorState == 0)
    65                                 cursorState = 1;
    66                         else
    67                                 cursorState = 0;
    68                        
    69                         lastCursorChange = System.currentTimeMillis();
    70                 }
    71                
    72         if(password)
    73         {
    74                 for(int x=0;x<text.length();x++)
    75                                 drawnString+="*";
     9  private String label; 
     10  private String text;
     11  private Font font;
     12  private int textStart;
     13  private boolean selected;
     14  private int cursorState;
     15  private int blinkInterval;
     16  private long lastCursorChange;
     17  private boolean password;
     18  protected boolean noBorder;
     19
     20  public Textbox(String newName, int newX, int newY, int newWidth, int newHeight, String newLabel, Font newFont, boolean isPass) {
     21    super(newName, newX, newY, newWidth, newHeight);
     22    this.label = new String(newLabel);
     23    this.text = new String();
     24    this.font = newFont;
     25    this.textStart = 0;
     26    this.selected = false;
     27    this.cursorState = 0;
     28    this.blinkInterval = 500;
     29    this.password = isPass;
     30    this.noBorder = false;
     31  }
     32
     33  public void setBorder(boolean b) {
     34    this.noBorder = !b;
     35  }
     36
     37  public void handleEvent(KeyEvent e) {
     38    if (32 <= e.getKeyCode() && e.getKeyCode() <= 127) {
     39      if (e.getKeyCode() == 127) {
     40        if (this.text.length() > 0) {
     41          this.text = this.text.substring(0, this.text.length() - 1);
    7642        }
    77         else
    78                 drawnString = text;
    79                
    80         if(metrics.stringWidth(drawnString) + 9 > getWidth())
    81                 textStart = metrics.stringWidth(drawnString)+9-getWidth();
    82         else
    83                 textStart = 0;
    84        
    85         g.setColor(Color.green);
    86                 g.setFont(font);
    87                 srcGraphics.setColor(Color.green);
    88                 srcGraphics.setFont(font);
     43      } else {
     44        this.text = String.valueOf(this.text) + Character.toString(e.getKeyChar());
     45      }
     46    } else if (e.getKeyCode() == 8) {
     47      if (this.text.length() > 0) {
     48        this.text = this.text.substring(0, this.text.length() - 1);
     49      }
     50    }
     51  }
    8952
    90                 srcGraphics.drawString(drawnString, 5-textStart, (getHeight() + metrics.getHeight())/2 - 2);
    91                
    92                 g.drawImage(source, getX(), getY(), null);
    93                 g.drawString(label, getX() - metrics.stringWidth(label) - 10, getY() + (getHeight() + metrics.getHeight())/2 - 2);
    94                
    95                 if(selected && cursorState == 1)
    96                         g.drawLine(getX() + metrics.stringWidth(drawnString) - textStart + 6, getY() + 5, getX() + metrics.stringWidth(drawnString) - textStart + 6, getY() + getHeight() - 5);
    97                
    98                 g.setColor(Color.red);
    99                 g.drawRect(getX(), getY(), getWidth(), getHeight());
    100         }
    101        
    102         public boolean isClicked(int xCoord, int yCoord) {
    103                 if(xCoord < getX() || getX() + getWidth() < xCoord)
    104                         return false;
    105                 if(yCoord < getY() || getY() + getHeight() < yCoord)
    106                         return false;
    107                
    108                         return true;
    109         }
    110        
    111         public void clear() {
    112                 text = "";
    113                 textStart = 0;
    114                 selected = false;
    115         }
     53  public void draw(Graphics g) {
     54    String drawnString = new String();
     55    FontMetrics metrics = g.getFontMetrics(this.font);
     56    GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
     57    GraphicsDevice device = env.getDefaultScreenDevice();
     58    GraphicsConfiguration gc = device.getDefaultConfiguration();
     59    BufferedImage source = gc.createCompatibleImage(getWidth(), getHeight());
     60    Graphics2D srcGraphics = source.createGraphics();
     61    if (this.selected && System.currentTimeMillis() - this.lastCursorChange > this.blinkInterval) {
     62      if (this.cursorState == 0) {
     63        this.cursorState = 1;
     64      } else {
     65        this.cursorState = 0;
     66      }
     67      this.lastCursorChange = System.currentTimeMillis();
     68    }
     69    if (this.password) {
     70      for (int x = 0; x < this.text.length(); x++) {
     71        drawnString = String.valueOf(drawnString) + "*";
     72      }
     73    } else {
     74      drawnString = this.text;
     75    }
     76    if (metrics.stringWidth(drawnString) + 9 > getWidth()) {
     77      this.textStart = metrics.stringWidth(drawnString) + 9 - getWidth();
     78    } else {
     79      this.textStart = 0;
     80    }
     81    g.setColor(Color.green);
     82    g.setFont(this.font);
     83    srcGraphics.setColor(Color.green);
     84    srcGraphics.setFont(this.font);
     85    srcGraphics.drawString(drawnString, 5 - this.textStart, (getHeight() + metrics.getHeight()) / 2 - 2);
     86    g.drawImage(source, getX(), getY(), null);
     87    g.drawString(this.label, getX() - metrics.stringWidth(this.label) - 10, getY() + (getHeight() + metrics.getHeight()) / 2 - 2);
     88    if (this.selected && this.cursorState == 1) {
     89      g.drawLine(getX() + metrics.stringWidth(drawnString) - this.textStart + 6, getY() + 5, getX() + metrics.stringWidth(drawnString) - this.textStart + 6, getY() + getHeight() - 5);
     90    }
     91    g.setColor(Color.red);
     92    if (!this.noBorder) {
     93      g.drawRect(getX(), getY(), getWidth(), getHeight());
     94    }
     95  }
    11696
    117         public int getBlinkInterval() {
    118                 return blinkInterval;
    119         }
     97  public boolean isClicked(int xCoord, int yCoord) {
     98    if (xCoord < getX() || getX() + getWidth() < xCoord) {
     99      return false;
     100    }
     101    if (yCoord < getY() || getY() + getHeight() < yCoord) {
     102      return false;
     103    }
     104    return true;
     105  }
    120106
    121         public int getCursorState() {
    122                 return cursorState;
    123         }
     107  public void clear() {
     108    this.text = "";
     109    this.textStart = 0;
     110    this.selected = false;
     111  }
    124112
    125         public Font getFont() {
    126                 return font;
    127         }
     113  public int getBlinkInterval() {
     114    return this.blinkInterval;
     115  }
    128116
    129         public String getLabel() {
    130                 return label;
    131         }
     117  public int getCursorState() {
     118    return this.cursorState;
     119  }
    132120
    133         public long getLastCursorChange() {
    134                 return lastCursorChange;
    135         }
     121  public Font getFont() {
     122    return this.font;
     123  }
    136124
    137         public boolean isSelected() {
    138                 return selected;
    139         }
     125  public String getLabel() {
     126    return this.label;
     127  }
    140128
    141         public String getText() {
    142                 return text;
    143         }
     129  public long getLastCursorChange() {
     130    return this.lastCursorChange;
     131  }
    144132
    145         public int getTextStart() {
    146                 return textStart;
    147         }
    148        
    149         public void setText(String newText) {
    150                 text = newText;
    151         }
    152        
    153         public void setSelected(boolean isSelected) {
    154                 selected = isSelected;
    155         }
     133  public boolean isSelected() {
     134    return this.selected;
     135  }
    156136
    157         public void setTextStart(int textStart) {
    158                 this.textStart = textStart;
    159         }
    160        
    161         public void setCursorState(int cursorState) {
    162                 this.cursorState = cursorState;
    163         }
     137  public String getText() {
     138    return this.text;
     139  }
    164140
    165         public void setLastCursorChange(long lastCursorChange) {
    166                 this.lastCursorChange = lastCursorChange;
    167         }
     141  public int getTextStart() {
     142    return this.textStart;
     143  }
     144
     145  public void setText(String newText) {
     146    this.text = newText;
     147  }
     148
     149  public void setSelected(boolean isSelected) {
     150    this.selected = isSelected;
     151  }
     152
     153  public void setTextStart(int textStart) {
     154    this.textStart = textStart;
     155  }
     156
     157  public void setCursorState(int cursorState) {
     158    this.cursorState = cursorState;
     159  }
     160
     161  public void setLastCursorChange(long lastCursorChange) {
     162    this.lastCursorChange = lastCursorChange;
     163  }
    168164}
  • gamegui/Window.java

    r155577b r8edd04e  
    33import java.awt.*;
    44import java.awt.event.MouseEvent;
    5 import java.util.*;
     5import java.util.ArrayList;
    66
    7 public class Window extends Member
    8 {
    9         ArrayList<Member> members;
    10         boolean fullscreen;
    11        
    12         public Window(String newName, int newX, int newY, int newWidth, int newHeight) {
    13                 super(newName, newX, newY, newWidth, newHeight);
    14                
    15                 members = new ArrayList<Member>();
    16         }
    17        
    18         public Window(String newName, int newX, int newY, int newWidth, int newHeight, boolean full) {
    19                 super(newName, newX, newY, newWidth, newHeight);
    20                
    21                 members = new ArrayList<Member>();
    22                 fullscreen = full;
    23         }
    24        
    25         public void draw(Graphics g) {
    26                 g.setColor(Color.black);
    27         g.fillRect(getX(), getY(), getWidth(), getHeight());
    28        
    29         if(!fullscreen)
    30         {
    31                 g.setColor(Color.red);
    32                 g.drawRect(getX(), getY(), getWidth(), getHeight());
    33         }
    34        
    35         for(int x=0; x < members.size(); x++)
    36                 members.get(x).draw(g);
    37         }
    38        
    39         public boolean handleEvent(MouseEvent e) {
    40                 boolean val = false;
    41                
    42                 for(int x=0; x < members.size(); x++)
    43                 val = val || members.get(x).handleEvent(e);
    44                
    45                 return val;
    46         }
    47        
    48         public void clear() {
    49                 for(int x=0; x < members.size(); x++)
    50                 members.get(x).clear();
    51         }
    52        
    53         public void add(Member aMember) {
    54                 aMember.offset(getX(), getY());
    55                
    56                 members.add(aMember);
    57         }
    58        
    59         public void offset(int xOffset, int yOffset) {
    60                 super.offset(xOffset, yOffset);
    61                
    62                 for(int x=0; x < members.size(); x++)
    63                         members.get(x).offset(xOffset, yOffset);
    64         }
    65        
    66         public Member getMember(String aName) {
    67                 for(int x=0; x < members.size(); x++)
    68                 if(members.get(x).getName().equals(aName))
    69                         return (Member)members.get(x);
    70                
    71                 return null;
    72         }
     7public class Window extends Member {
     8
     9  ArrayList<Member> members;
     10  boolean fullscreen;
     11
     12  public Window(String newName, int newX, int newY, int newWidth, int newHeight) {
     13    super(newName, newX, newY, newWidth, newHeight);
     14    this.members = new ArrayList<Member>();
     15  }
     16
     17  public Window(String newName, int newX, int newY, int newWidth, int newHeight, boolean full) {
     18    super(newName, newX, newY, newWidth, newHeight);
     19    this.members = new ArrayList<Member>();
     20    this.fullscreen = full;
     21  }
     22
     23  public void draw(Graphics g) {
     24    g.setColor(Color.black);
     25    g.fillRect(getX(), getY(), getWidth(), getHeight());
     26    if (!this.fullscreen) {
     27      g.setColor(Color.red);
     28      g.drawRect(getX(), getY(), getWidth(), getHeight());
     29    }
     30    for (int x = 0; x < this.members.size(); x++) {
     31      ((Member)this.members.get(x)).draw(g);
     32    }
     33  }
     34
     35  public boolean handleEvent(MouseEvent e) {
     36    boolean val = false;
     37    for (int x = 0; x < this.members.size(); x++) {
     38      val = !(!val && !((Member)this.members.get(x)).handleEvent(e));
     39    }
     40    return val;
     41  }
     42
     43  public void clear() {
     44    for (int x = 0; x < this.members.size(); x++) {
     45      ((Member)this.members.get(x)).clear();
     46    }
     47  }
     48
     49  public void add(Member aMember) {
     50    aMember.offset(getX(), getY());
     51    this.members.add(aMember);
     52  }
     53
     54  public void offset(int xOffset, int yOffset) {
     55    super.offset(xOffset, yOffset);
     56    for (int x = 0; x < this.members.size(); x++) {
     57      ((Member)this.members.get(x)).offset(xOffset, yOffset);
     58    }
     59  }
     60
     61  public Member getMember(String aName) {
     62    for (int x = 0; x < this.members.size(); x++) {
     63      if (((Member)this.members.get(x)).getName().equals(aName)) {
     64        return this.members.get(x);
     65      }
     66    }
     67    return null;
     68  }
    7369}
Note: See TracChangeset for help on using the changeset viewer.