Update of /cvsroot/arianne/stendhal/src/games/stendhal/client/gui
In directory vz-cvs-4.sog:/tmp/cvs-serv9229/src/games/stendhal/client/gui
Added Files:
LinearScalingModel.java ScalingModel.java
StatusDisplayBar.java
Log Message:
Added a component for the various colour bars in the gui
--- NEW FILE: LinearScalingModel.java ---
/***************************************************************************
* (C) Copyright 2003-2012 - Stendhal *
***************************************************************************
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
package games.stendhal.client.gui;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/**
* A {@link ScalingModel} that implements a linearly displayed value with a
* known maximum. The representation will have value 0 at value 0, and the
* maximum representation when the value is the specified maximum.
*/
public class LinearScalingModel implements ScalingModel {
private final List<ChangeListener> listeners = new CopyOnWriteArrayList<ChangeListener>();
private double maxValue;
private int maxRepresentation;
private double scale;
private double value;
private int representation;
/**
* Create a LinearScalingModel with maximum value of 1.0, and maximum
* representation 1.
*/
public LinearScalingModel() {
this(1.0, 1);
}
/**
* Create a LinearScalingModel.
*
* @param maxValue maximum value of the variable
* @param maxRepresentation the maximum of the presentation of the value
*/
public LinearScalingModel(double maxValue, int maxRepresentation) {
this.maxValue = maxValue;
this.maxRepresentation = maxRepresentation;
calculateScale();
}
@Override
public void addChangeListener(ChangeListener listener) {
listeners.add(listener);
}
/**
* Set the value without calling change listeners.
*
* @param value new value
*/
private void setValueNoNotify(double value) {
/*
* Let the internal value overflow, but keep the representation
* reasonable. Users of the model may need to modify the values coming
* from RPObject property changes, and the order of their arrival does
* not necessarily follow the constraints.
*/
this.value = value;
representation = Math.min((int) Math.round(this.value * scale), maxRepresentation);
}
@Override
public void setValue(double value) {
int oldRepr = representation;
setValueNoNotify(value);
// Avoid needles notifications
if (representation != oldRepr) {
fireChanged();
}
}
@Override
public int getRepresentation() {
return representation;
}
@Override
public void setMaxRepresentation(int max) {
if (max != maxRepresentation) {
maxRepresentation = max;
calculateScale();
// Avoid multiple change events
setValueNoNotify(value);
fireChanged();
}
}
/**
* Set the maximum value for the model, ie. the value that corresponds to
* the maximum representation.
*
* @param maxValue new maximum value
*/
public void setMaxValue(double maxValue) {
this.maxValue = maxValue;
calculateScale();
// Fires changed event, if needs be
setValue(value);
}
/**
* Calculate the scaling factor.
*/
private void calculateScale() {
scale = maxRepresentation / maxValue;
}
/**
* Notify change listeners.
*/
private void fireChanged() {
ChangeEvent e = new ChangeEvent(this);
for (ChangeListener listener : listeners) {
listener.stateChanged(e);
}
}
}
--- NEW FILE: ScalingModel.java ---
/***************************************************************************
* (C) Copyright 2003-2012 - Stendhal *
***************************************************************************
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
package games.stendhal.client.gui;
import javax.swing.event.ChangeListener;
/**
* A model interface for values that need a representation as values [0, max],
* where the representation is not necessarily the same as the original value,
* for example the length of a health bar.
*/
public interface ScalingModel {
/**
* Add a listener that should be notified when the representation of the
* value changes.
*
* @param listener change listener
*/
void addChangeListener(ChangeListener listener);
/**
* Set the internal value.
*
* @param value new value
*/
void setValue(double value);
/**
* Get the representation value.
*
* @return representation
*/
int getRepresentation();
/**
* Set the maximum representation value.
*
* @param max new maximum
*/
void setMaxRepresentation(int max);
}
--- NEW FILE: StatusDisplayBar.java ---
/***************************************************************************
* (C) Copyright 2003-2012 - Stendhal *
***************************************************************************
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
package games.stendhal.client.gui;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JComponent;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/**
* A component for drawing the various color bars.
*/
public class StatusDisplayBar extends JComponent {
/** Preferred height of the bar. */
private final int PREFERRED_HEIGHT = 10;
/** Value model. */
private final ScalingModel model;
/** Default bar color. */
private Color color = Color.WHITE;
/**
* Create a StatusDisplayBar.
*
* @param model Scaling model. representation corresponds to the length
* of the color bar. The StatusDisplayBar will take care of the maximum
* representation value
*/
public StatusDisplayBar(final ScalingModel model) {
this.model = model;
model.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
repaint();
}
});
this.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
Insets insets = getInsets();
int barWidth = getWidth() - insets.left - insets.right - 2;
model.setMaxRepresentation(barWidth);
}
});
this.setPreferredSize(new Dimension(2, PREFERRED_HEIGHT));
}
/**
* Set the color of the bar.
*
* @param color new color
*/
public void setBarColor(Color color) {
this.color = color;
}
@Override
public void paintComponent(Graphics g) {
Insets insets = getInsets();
// Paint black what is not covered by the colored bar
g.setColor(Color.BLACK);
g.fillRect(insets.left, insets.top, getWidth() - insets.left - insets.right,
getHeight() - insets.top - insets.bottom);
if (color != null) {
g.setColor(color);
g.fillRect(insets.left + 1, insets.top + 1, model.getRepresentation(), getHeight() - insets.top - insets.bottom - 2);
}
}
}
|