ThinProgressBar: Manually paint

the old method caused it to expand randomly, which would make it show an abnormally high value. This also makes update not required, fixing the bug where it wouldn't render correctly when update was called after a layout was invalidated, but before it was recalculated.
This commit is contained in:
Max Weber
2019-01-12 15:35:00 -07:00
parent 5bdcc44493
commit a225789f0c
4 changed files with 41 additions and 43 deletions

View File

@@ -226,7 +226,6 @@ public class GrandExchangeOfferSlot extends JPanel
progressBar.setForeground(getProgressColor(newOffer)); progressBar.setForeground(getProgressColor(newOffer));
progressBar.setMaximumValue(newOffer.getTotalQuantity()); progressBar.setMaximumValue(newOffer.getTotalQuantity());
progressBar.setValue(newOffer.getQuantitySold()); progressBar.setValue(newOffer.getQuantitySold());
progressBar.update();
/* Couldn't set the tooltip for the container panel as the children override it, so I'm setting /* Couldn't set the tooltip for the container panel as the children override it, so I'm setting
* the tooltips on the children instead. */ * the tooltips on the children instead. */

View File

@@ -250,7 +250,6 @@ public class FarmingTabPanel extends TabContentPanel
panel.getProgress().setForeground(state.getCropState().getColor().darker()); panel.getProgress().setForeground(state.getCropState().getColor().darker());
panel.getProgress().setMaximumValue(stages - 1); panel.getProgress().setMaximumValue(stages - 1);
panel.getProgress().setValue(stage); panel.getProgress().setValue(stage);
panel.getProgress().update();
} }
else else
{ {

View File

@@ -144,8 +144,6 @@ public class BirdHouseTabPanel extends TabContentPanel
panel.getEstimate().setText("Unknown"); panel.getEstimate().setText("Unknown");
break; break;
} }
panel.getProgress().update();
} }
} }
} }

View File

@@ -25,69 +25,71 @@
*/ */
package net.runelite.client.ui.components; package net.runelite.client.ui.components;
import java.awt.BorderLayout;
import java.awt.Color; import java.awt.Color;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel; import javax.swing.JPanel;
import lombok.Setter; import lombok.Getter;
import net.runelite.client.ui.ColorScheme;
/** /**
* A progress bar to be displayed underneath the GE offer item panels * A progress bar to be displayed underneath the GE offer item panels
*/ */
public class ThinProgressBar extends JPanel public class ThinProgressBar extends JPanel
{ {
@Setter @Getter
private int maximumValue; private int maximumValue = 1;
@Setter @Getter
private int value; private int value;
private final JPanel topBar = new JPanel();
public ThinProgressBar() public ThinProgressBar()
{ {
setLayout(new BorderLayout()); setForeground(Color.GREEN);
setBackground(Color.GREEN.darker()); setMaximumSize(new Dimension(Integer.MAX_VALUE, 4));
setMinimumSize(new Dimension(0, 4));
topBar.setPreferredSize(new Dimension(100, 4)); setPreferredSize(new Dimension(0, 4));
topBar.setBackground(ColorScheme.PROGRESS_COMPLETE_COLOR); setSize(new Dimension(0, 4));
setOpaque(true);
add(topBar, BorderLayout.WEST);
}
/**
* Updates the UI based on the percentage progress
*/
public void update()
{
double percentage = getPercentage();
int topWidth = (int) (getSize().width * (percentage / 100));
topBar.setPreferredSize(new Dimension(topWidth, 4));
topBar.repaint();
revalidate();
repaint();
} }
public double getPercentage() public double getPercentage()
{ {
if (value == 0)
{
return 0;
}
return (value * 100) / maximumValue; return (value * 100) / maximumValue;
} }
@Override @Override
public void setForeground(Color color) public void setForeground(Color color)
{ {
if (topBar != null) super.setForeground(color);
{
topBar.setBackground(color);
}
setBackground(color.darker()); setBackground(color.darker());
} }
public void setMaximumValue(int maximumValue)
{
if (maximumValue < 1)
{
maximumValue = 1;
}
this.maximumValue = maximumValue;
repaint();
}
public void setValue(int value)
{
this.value = value;
repaint();
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int w = getWidth();
int h = getHeight();
int div = (value * w) / maximumValue;
g.setColor(getBackground());
g.fillRect(div, 0, w, h);
g.setColor(getForeground());
g.fillRect(0, 0, div, h);
}
} }