I'm trying to learn Java game developing in hopes of one day going to uni. I'm currently following tutorials and learning the basics. However, after following a tutorial my render doesn't render full, it only renders half of the intended screen. The following code are the 2 classes I've used. Have I messed up with constructors, scaling, is the this.height incorrect? I cant seem to figure it out.
public class Game extends Canvas implements Runnable{
private static final long serialVersionUID = 1L;
public static int width = 300;
public static int height = width / 16 * 9;
public static int scale = 3;
private Thread thread;
private JFrame frame;
private boolean running = false;
private Screen screen;
private BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
public Game() {
Dimension size = new Dimension(width * scale, height * scale);
setPreferredSize(size);
screen = new Screen(width, height);
frame = new JFrame();
}
public synchronized void start() {
running = true;
thread = new Thread(this, "Display");
thread.start();
}
public synchronized void stop() {
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void run() {
while (running) {
update();
render();
}
}
public void update() {
}
public void render() {
BufferStrategy bs = getBufferStrategy();
if (bs == null) {
createBufferStrategy(3);
return;
}
screen.render();
for (int i = 0; i < pixels.length; i++) {
pixels[i] = screen.pixels[i];
}
Graphics g = bs.getDrawGraphics();
g.setColor(new Color(0,0,0));
g.fillRect(0, 0, getWidth(), getHeight());
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
g.dispose();
bs.show();
}
public static void main(String[] args) {
Game game = new Game();
game.frame.setResizable(false);
game.frame.setTitle("My First Game");
game.frame.add(game);
game.frame.pack();
game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.frame.setLocationRelativeTo(null);
game.frame.setVisible(true);
game.start();
}
}
public class Screen {
private int width, height;
public int[] pixels;
public Screen(int width, int height) {
this.width = width;
this.height = height;
pixels = new int[width * height];
}
public void render() {
for (int y = 0; y < height; y++ ) {
for (int x = 0; x < height; x++ ) {
pixels[x + y * width] =(0xFF00FF);
}
}
}
}
In order to render the whole screen pink you need make 2 adaptations:
1) Change the order of your operands when calculating the height on line 5, so that you multiply first and divide afterwards like this:
public static int height = width * 9 / 16;
Reason: The expression is evaluated from left to right. As all operators are integers, the result of each sub-expression is rounded down. See the difference:
300 / 16 * 9 = 18 * 9 = 162
vs
300 * 9 / 16 = 2700 / 16 = 168
2) There is a typo in the render method, where height is used twice instead of width. Therefore a rectangle is rendered. You need to change the height variable in the inner for loop to width.
public void render() {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) { // use width here instead of height
pixels[x + y * width] = (0xFF00FF);
}
}
}
Related
My partner and I are trying to remake Tetris for our final project of the year in my Computer Science class we currently have a for loop that draws individual rectangles in an overwritten paint method.
private final int spacer = 30;
public int getSpacer()
{
return spacer;
}
public void paint(Graphics g) {
setBackground(Color.GRAY);
for(int i = getHeight()/2 - (spacer * 10); i < getHeight()/2 + (spacer * 10); i += spacer) {
for(int x = getWidth()/2 - (spacer * 5); x < getWidth()/2 + (spacer * 5); x += (spacer)) {
g.drawRect(x, i, (spacer), (spacer));
}
}
setForeground(Color.black);
}
The method basically takes the width and height of the window and makes a 10 x 20 grid of boxes that are 30 units, pixels I think, wide.
We'd like to make a Grid.java class that takes in color, the spacer int, and an x and y int. The constructor for Grid.java should draw the exact same thing as the code above using the for loop, but when we tried it gave us a white screen that would not resize with the window.
private final int spacer = 30;
private static Grid[][] arr = new Grid[10][20];
public int getSpacer()
{
return spacer;
}
public void paint(Graphics g) {
setBackground(Color.GRAY);
int countY = 0;
int countX = 0;
for(int y = getHeight()/2 - (spacer * 10); y < getHeight()/2 + (spacer * 10); y += spacer) {
for(int x = getWidth()/2 - (spacer * 5); x < getWidth()/2 + (spacer * 5); x += spacer) {
arr[countX][countY] = new Grid(x, y, spacer, g);
countX++;
}
countY++;
}
setForeground(Color.black);
}
*Grid.java Class*
package Tetris_Shapes;
import javax.swing.*;
import java.awt.*;
public class Grid {
private int x;
private int y;
private int side;
private Graphics g;
public Grid(int x, int y, int side, Graphics g) {
// g.drawRect(x, y, spacer, spacer);
this.x = x;
this.y = y;
this.side = side;
this.g = g;
paint(this.g);
}
private void paint(Graphics g) {
g.drawRect(x, y, side, side);
}
}
When we try and run this we get the white box that doesn't resize. My question is does anyone know of a way to get a constructor to draw shapes. Thank you in advance, this is pretty niche so I'm also going to apologize in advance.
I trying to animate 50 bouncing dots with different colours. I am able to make the dots move but the dots drag on like it is being painted. I do not know why it turned to be like this
This is my codes for DotsPanel:
public DotsPanel(){
frameSize = 300;
setBounds (100, 100,frameSize,frameSize );
setPreferredSize (new Dimension(frameSize, frameSize));
setVisible(true);
x = 0;
y = 40;
moveX = moveY = 3;
for (int i = 0; i < dot.length; i++)
{
dot[i] = new Dot (frameSize, frameSize);
}
timer = new Timer(DELAY, new TimerListener());
timer.start();
}
private class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
for (int i = 0; i < dot.length; i++)
{
dot[i].animate();
}
repaint();
}
}
public void paintComponent(Graphics g){
for (int i = 0; i < dot.length; i++)
{
dot[i].draw(g);
}
}
public void run ()
{
while(true)
{
for (int i = 0; i < dot.length; i++)
{
dot[i].animate();
}
repaint();
}
}
This is the Dot class:
public Dot(int width, int height) {
this.width = width;
this.height = height;
x = r.nextInt(width - d);
y = r.nextInt(height - d);
speed = r.nextInt(10);
red = r.nextInt(256);
green = r.nextInt(256);
blue = r.nextInt(256);
moveX = moveY = 3;
}
public void animate ()
{
x += moveX;
y += moveY;
if (x <= 0 || x >= 300)
moveX = moveX * -1;
if (y <= 0 || y >= 300)
moveY = moveY * -1;
}
public void draw(Graphics g) {
g.setColor(new Color(red, green, blue));
//g.setColor(Color.BLUE);
g.fillOval(x, y, d, d);
}
I do not know which part went wrong. I am guessing is the draw method. I tried using paint instead of paintComponent but still the same result. And if i remove the while loop, the dots do not animate, I did this according to a tutorial i seen in youtube, the tutorial works well though.
Well, I guess it's simply because you don't "clear" the previous drawing each time a new one is drawn. As a result, you see all your previous graphics being continuously stacked !
EDIT (correct solution by camickr in comments below) :
The solution is to invoke super.paintComponent(g) at the top of the method to invoke the default painting functionality, which for a JPanel, will paint the background for you.
I have a Stratego board being drawn on a JPanel, being used to set up the player's pieces. After two different instances of these panels are correctly arranged by the players, the main board (that has the same format and layout, just will have different piece appearance and logical behavior) will show.
My issue is that the second input panel and the main board (which currently has no functionality or pieces on it) are SOMETIMES set up with extra width on the bottom and the right, causing the 10x10 grid to not take up the entire board space like it should.
The initial input panel for player 1 seems to be working fine, never having this problem. The second panel and the main panel only SOMETIMES have this issue, so I'm not entirely sure where this is stemming from.
Here is the main method which sets up the panels and stuff.
public class Core {
public static void main(String[] args) {
LogicInterpreter logic = new LogicInterpreter();
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
InputFrame inputPlayer1 = new InputFrame(logic, 1, "red", 600, 600);
inputPlayer1.setLocation(dim.width / 2 - inputPlayer1.getSize().width/2,
dim.height / 2 - inputPlayer1.getSize().height / 2);
while(!logic.isSetUp1()){
//Just to make it work
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//Now bring up board 2
InputFrame inputPlayer2 = new InputFrame(logic, 2, "blue", 600, 600);
inputPlayer2.setLocation(dim.width / 2 - inputPlayer2.getSize().width/2,
dim.height / 2 - inputPlayer2.getSize().height / 2);
while(!logic.isSetUp2()){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
openBoards(logic);
}
public static void openBoards(LogicInterpreter logic) {
try {
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
MainBoard board = new MainBoard(logic);
board.setLocation(dim.width / 2 - board.getSize().width / 2, dim.height / 2 - board.getSize().height / 2);
} catch (Exception e) {
System.exit(1);
}
}
}
Also, here is the code inside the input panels. I'm not sure what is relevant, so I can't take stuff out. Sorry. I will withhold the main board setup code because it is really the same.
public class InputFrame extends JFrame {
private static final long serialVersionUID = 1L;
private LogicInterpreter holder;
private Panel2 jp;
private int height, width;
private Map<Integer, ArrayList<Integer>> lakeCoords = new HashMap<>();
private List<Piece> pieces = new ArrayList<>();
private int playernumber;
private String playerColor;
Piece selectedPiece;
Piece secondSelectedPiece;
boolean hidePieces = false;
JButton submit = new JButton("SUBMIT");
public void addCoords() {
lakeCoords.put(3, new ArrayList<Integer>(Arrays.asList(6, 5)));
lakeCoords.put(4, new ArrayList<Integer>(Arrays.asList(6, 5)));
lakeCoords.put(7, new ArrayList<Integer>(Arrays.asList(6, 5)));
lakeCoords.put(8, new ArrayList<Integer>(Arrays.asList(6, 5)));
}
public void createPieces() {
int y = 1;
if (playernumber == 2) {
y = 7;
}
List<Integer> openValues = new ArrayList<>();
openValues.add(1);
openValues.add(2);
openValues.add(11);
openValues.add(12);
for (int x = 0; x < 2; x++) {
openValues.add(3);
}
for (int x = 0; x < 3; x++) {
openValues.add(4);
}
for (int x = 0; x < 4; x++) {
openValues.add(5);
openValues.add(6);
openValues.add(7);
}
for (int x = 0; x < 5; x++) {
openValues.add(8);
}
for (int x = 0; x < 8; x++) {
openValues.add(9);
}
for (int x = 0; x < 6; x++) {
openValues.add(10);
}
Collections.sort(openValues);
System.out.println(openValues.size());
System.out.println(pieces.size());
for (int x = 1; x <= 10; x++) {
for (int z = y; z <= y + 3; z++) {
// 1x1 Marshal
// 2x1 General
// 3x2 Colonel
// 4x3 Major
// 5x4 Captain
// 6x4 Lieutenant
// 7x4 Sergeant
// 8x5 Miner
// 9x8 Scout
// 10x6 Bomb
// 11x1 Flag
// 12x1 Spy
Piece piece = new Piece(new Coords(x, z), openValues.get(0), playerColor);
openValues.remove(0);
pieces.add(piece);
}
}
}
public InputFrame(LogicInterpreter holder, int playerNumber, String playerColor, int height, int width) {
this.height = height;
this.width = width;
playernumber = playerNumber;
this.playerColor = playerColor;
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
addCoords();
this.holder = holder;
createPieces();
jp = new Panel2(height, width);
setResizable(false);
jp.setBackground(new Color(235, 202, 158));
setTitle("Player " + playerNumber + " Arrangement GUI || Click Submit When Ready");
jp.setPreferredSize(new Dimension(600, 600));
jp.setLayout(null);
jp.addMouseListener(new HandleMouse());
if(playernumber == 1)
submit.setBounds(width / 10 * 4, height / 10 * 7, width / 10 * 2, height / 10 * 2);
else
submit.setBounds(width / 10 * 4, height / 10, width / 10 * 2, height / 10 * 2);
submit.setFont(new Font("Arial", Font.BOLD, width * 20 / 600));
submit.setBackground(Color.LIGHT_GRAY);
submit.addActionListener(new CloseListener(this));
jp.add(submit);
getContentPane().add(jp);
pack();
setVisible(true);
}
class CloseListener implements ActionListener {
private InputFrame frame;
public CloseListener(InputFrame frame) {
this.frame = frame;
}
public void actionPerformed(ActionEvent event) {
// Do the stuff here before closing
hidePieces = true;
repaint();
if (playernumber == 1) {
holder.setP1Pieces(pieces);
JOptionPane.showMessageDialog(null, "Press When Ready for Next Player");
holder.setSetUp1(true);
} else {
holder.setP2Pieces(pieces);
JOptionPane.showMessageDialog(null, "Press When Player 1 is Ready");
holder.setSetUp2(true);
}
frame.dispose();
}
}
public class Panel2 extends JPanel {
private static final long serialVersionUID = 1L;
int height = 0;
int width = 0;
public Panel2(int height, int width) {
this.height = height;
this.width = width;
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int x = 0; x < width; x += width / 10) {
for (int y = 0; y < height; y += height / 10) {
boolean fill = false;
for (Entry<Integer, ArrayList<Integer>> coords : lakeCoords.entrySet()) {
if ((coords.getKey() - 1 == x / 60 && coords.getValue().get(0) - 1 == y / 60)
|| (coords.getKey() - 1 == x / 60 && coords.getValue().get(1) - 1 == y / 60)) {
fill = true;
break;
}
}
if (fill) {
g.setColor(Color.BLUE);
g.fillRect(x, y, width / 10, height / 10);
g.setColor(Color.BLACK);
g.drawRect(x, y, width / 10, height / 10);
} else {
g.setColor(Color.BLACK);
g.drawRect(x, y, width / 10, height / 10);
}
}
}
if(hidePieces){
for (Piece piece : pieces) {
try {
g.drawImage(ImageIO.read(new File(playerColor + "_pieces/" + (playerColor.equals("blue") ? "Blue" : "Red") + "_Strat_Piece"
+ ".png")), piece.getX() * width / 10 - width / 10,
piece.getY() * height / 10 - height / 10, width / 10, height / 10, null);
} catch(Exception e){}
}
} else {
for (Piece piece : pieces) {
g.drawImage(piece.getImage(), piece.getX() * width / 10 - width / 10,
piece.getY() * height / 10 - height / 10, width / 10, height / 10, null);
}
if (selectedPiece != null) {
g.setColor(Color.BLUE);
g.drawImage(selectedPiece.getImage(), selectedPiece.getX() * width / 10 - width / 10,
selectedPiece.getY() * height / 10 - height / 10, width / 10, height / 10, null);
g.drawRect(selectedPiece.getX() * width / 10 - width / 10,
selectedPiece.getY() * height / 10 - height / 10, width / 10, height / 10);
}
}
}
}
private class HandleMouse extends MouseAdapter {
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();
Coords coordinates = holder.getClickedBox(x, y, width, height);
boolean found = false;
boolean move = false;
for (Piece piece : pieces) {
if (piece.getX() == coordinates.getX() && piece.getY() == coordinates.getY()) {
found = true;
if (selectedPiece == null) {
selectedPiece = piece;
} else {
move = true;
secondSelectedPiece = piece;
}
}
}
if (move) {
pieces.remove(selectedPiece);
pieces.remove(secondSelectedPiece);
Coords storage = selectedPiece.getCoords();
selectedPiece.setCoords(secondSelectedPiece.getCoords());
secondSelectedPiece.setCoords(storage);
pieces.add(selectedPiece);
pieces.add(secondSelectedPiece);
selectedPiece = null;
secondSelectedPiece = null;
} else if (!found) {
if (selectedPiece != null) {
selectedPiece = null;
}
}
repaint();
}
}
}
Did you check how many pixels are "sometimes" added?
When i call setResizable(true) on my jframe, i notice that "sometimes" there are 10 pixels more, both in width and height, than i expect.
If this is your case, i answered a similar question here.
I was able to solve the problem using a 'trick', calling pack more than one time.
In the answer i also linked another similar question (this one), where i wasn't able to find another solution, but there some users described the bug.
I'm trying to learn how to make a 2D Game without Game Engines, anyways I already created a background scrolling right now my goal is to make my character jump. But the thing is whenever I start my app the character is spinning up and down and it will just go away to the background.
Here's my character code
public class Deer extends GameCharacter {
private Bitmap spritesheet;
private double dya;
private boolean playing;
private long startTime;
private boolean Jump;
private Animate Animation = new Animate();
public Deer(Bitmap res, int w, int h, int numFrames) {
x = 20;
y = 400;
dy = 0;
height = h;
width = w;
Bitmap[] image = new Bitmap[numFrames];
spritesheet = res;
for (int i = 0; i < image.length; i++)
{
image[i] = Bitmap.createBitmap(spritesheet, i*width, 0, width, height);
}
Animation.setFrames(image);
Animation.setDelay(10);
startTime = System.nanoTime();
}
public void setJump(boolean b){
Jump = b;
}
public void update()
{
long elapsed = (System.nanoTime()-startTime)/1000000;
if(elapsed>100)
{
}
Animation.update();
if(Jump){
dy = (int)(dya+=5.5);
}
else{
dy = (int)(dya+=5.5);
}
if(dy>14)dy = 14;
if(dy>14)dy = -14;
y += dy*2;
dy = 0;
}
public void draw(Canvas canvas)
{
canvas.drawBitmap(Animation.getImage(),x,y,null);
}
public boolean getPlaying(){return playing;}
public void setPlaying(boolean b){playing = b;}
public void resetDYA(){dya = 0;}
}
x - character's horizontal position
y - character's vertical position
dx - character's horizontal acceleration
dy - character's vertical acceleration
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN) {
if(!deer.getPlaying()) {
deer.setPlaying(true);
}
deer.setJump(true);
return true;
}
return super.onTouchEvent(event);
}
I can't say for sure if this is the only problem because you have other suspicious code but it looks like you jump no matter what.
if(Jump){
dy = (int)(dya+=5.5);
} else {
dy = (int)(dya+=5.5);
}
If Jump is true you set the vertical acceleration. But you also set the vertical acceleration to the same value if Jump is false. You also don't show in your code where Jump is ever set to false.
Another odd bit of code is:
if(dy>14)dy = 14;
if(dy>14)dy = -14;
Here, if dy>14 you set it to 14. Then you check dy>14 immediately after. Of course, this time it's false. But because those two conditions are the same the second one will never pass since the one before it ensures it won't. The only other option is they both fail. IOW, you'll never be able to enter the second if.
All that aside, I'm not sure why you're taking this approach. You can simply rely on physics equations with constant acceleration, give an initial velocity, check for a collision with the ground (or at least the original height), and just let it run. For example:
// These are the variables you need.
int x = 200, y0 = 0, y = 0, velocity = 15;
double t = 0.0, gravity = -9.8;
// This is the statement that should run when you update the GUI.
// It is the fundamental equation for motion with constant acceleration.
// The acceleration is the gravitational constant.
y = (int) (y0 + velocity * t + .5 * gravity * t * t);
if (y < 0) {
y = y0 = 0;
//Stop jumping!
Jump = false;
} else {
// Swap the y values.
y0 = y;
// Increase the time with the frame rate.
t += frameRate;
}
// Draw the character using the y value
The best part about this is you don't need to worry about when you get to the maximum height because the equation will automatically bring you down. It also looks more natural as if the mechanics are real. Try it out.
A simple Swing example that you can play around with. Note that the values are different to deal with the way the components are drawn to the screen. Normally, you would deal with that with transformations but this will do for the task.
public class Main {
static Timer timer;
Main() {
JFrame frame = new JFrame("Hello sample");
frame.setSize(new Dimension(550, 550));
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel panel = new MyPanel();
frame.add(panel);
frame.setVisible(true);
timer = new Timer(5, (e) -> panel.repaint());
timer.start();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(Main::new);
}
class MyPanel extends JPanel {
int x = 200, y0 = 300, y = 0, w = 200, h = 200, v = -8;
double t = 0.0, gravity = 9.8;
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
y = (int) (y0 + v * t + .5 * gravity * t * t);
if (y > 300) {
y = y0 = 300;
// To prevent it from stopping comment the timer.stop() and
// uncomment the t = 0.0 statements.
//t = 0.0;
timer.stop();
} else {
y0 = y;
t += .025;
}
g.drawOval(x, y, w, h);
}
}
}
I am trying to make a bar chart. Everything goes fine; the code compiles and runs successfully. But the frame (window) is not packed perfectly. There is some space at the end of the bar chart. I just want this space removed.
public class BarChart extends JPanel{
int[] percentage;
Color color;
double barOffset;
public BarChart(int[] percentage, Color color) {
this.color = color;
this.percentage = percentage;
}
public BarChart(int[] percentage) {
this.color = Color.black;
this.percentage = percentage;
}
public BarChart() {
this.color = Color.black;
}
int w = 1,h = 1;
protected void paintComponent(Graphics g) {
super.paintComponent(g);
w = getWidth();
h = getHeight();
g.setColor(color);
barOffset = w*0.05;
int barWidth = (int)(w*0.1);
for(int i = 0; i<percentage.length; i++) {
g.fillRect((int)(barOffset),(int)(h*0.95-2*percentage[i]), barWidth, 2*percentage[i]);
if(i < percentage.length-1)
barOffset = (i+2)*w*0.05 + (i+1)*(barWidth);
}
}
}
This was not a packing error, but rather you were drawing off the edge of the component. To check for packing errors, set a background color for the container that is distinct from the component color.
For the set int[] p = new int[]{100, 5, 6, 9, 1, 0, 5, 100};, your bars are being drawn as follows:
component dimensions: width=104 height=10
bar[0]: xLeft=5 yTop=-190 barWidth=10 barHeight=200
bar[1]: xLeft=20 yTop=0 barWidth=10 barHeight=10
bar[2]: xLeft=35 yTop=-2 barWidth=10 barHeight=12
bar[3]: xLeft=50 yTop=-8 barWidth=10 barHeight=18
bar[4]: xLeft=66 yTop=7 barWidth=10 barHeight=2
bar[5]: xLeft=81 yTop=9 barWidth=10 barHeight=0
bar[6]: xLeft=96 yTop=0 barWidth=10 barHeight=10
bar[7]: xLeft=111 yTop=-190 barWidth=10 barHeight=200
I think this produces what you're looking for. Drawing components can be tricky, and the way I mitigate the complexity is to keep track of my screen locations semantically.
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class BarChart extends JPanel
{
public static void main(String[] args)
{
int[] p = new int[]{100, 5, 6, 9, 1, 0, 5, 100};
JFrame f = new JFrame();
f.setBackground(Color.BLUE);
BarChart chart = new BarChart(p);
chart.setBackground(Color.RED);
f.add(chart);
f.pack();
f.show();
}
private int[] percentage;
private Color color;
private boolean padEnds = true;
public BarChart(int[] percentage, Color color)
{
this.percentage = percentage;
this.color = color;
return;
}
public BarChart(int[] percentage)
{
this(percentage, Color.BLACK);
return;
}
public BarChart()
{
this(new int[0]);
return;
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(this.color);
int width = super.getWidth();
int height = super.getHeight();
int topPadding = Math.round(height * 0.05f);
int barCount = this.percentage.length;
int barOffset = Math.round(width * 0.025f); // 2.5% (in pixels) reserved space on both sides of each bar == 5% between bars
int totalOffsetWidth = (barOffset * 2) * barCount;
if (!this.padEnds)
{
totalOffsetWidth -= (barOffset * 2);
}
int availableWidth = width - totalOffsetWidth;
int availableHeight = height - topPadding;
int barWidth = (int) Math.floor((float) availableWidth / (float) barCount);
int xLeft = 0;
for (int i = 0; i < barCount; i++)
{
int percent = this.percentage[i];
if (this.padEnds || (i != 0))
{
xLeft += barOffset; // Add offset here to pad left side of each bar.
}
int barHeight = Math.round(((float) availableHeight) * ((float) percent / 100f));
int yTop = topPadding + (availableHeight - barHeight);
g.fillRect(xLeft, yTop, barWidth, barHeight);
xLeft += barWidth; // advance the next drawing position
if (this.padEnds || (i != (barCount - 1)))
{
xLeft += barOffset; // Add offset here to pad right side of each bar.
}
}
return;
}
}