Essentially I'm trying to create a star rating system.
I can create the components I need using a toolbar and toolitems as follows.
ToolBar toolBar = new ToolBar(Composite, SWT.FLAT);
toolBar.layout();
ToolItem starButton1 = new ToolItem(toolBar, SWT.NONE);
starButton1.setImage("imgpath/img.png");
ToolItem starButton2 = new ToolItem(toolBar, SWT.NONE);
starButton2.setImage("imgpath/img.png");
ToolItem starButton3 = new ToolItem(toolBar, SWT.NONE);
starButton3.setImage("imgpath/img.png");
ToolItem starButton4 = new ToolItem(toolBar, SWT.NONE);
starButton4.setImage("imgpath/img.png");
ToolItem starButton5 = new ToolItem(toolBar, SWT.NONE);
starButton5.setImage("imgpath/img.png");
From here I can add listeners/etc.. to change the appearance of the stars if they are selected. However this doesn't use the behavior of a rating system (i.e. if you select the third star, the first two change to selected too).
I thought of trying to use a list, something along the lines of this that would give the stars some sort of ordering information:
List<ToolItem> stars = new LinkedList<ToolItem>();
stars.add(new ToolItem(toolBar, SWT.NONE).setImage("imgpath/img.png"));
etc...
So the problem here is that the set methods return void, keeping me from modifying the settings of the ToolItem object in the add
Is there an easy way to do this that I'm overcomplicating?
Also I would like to avoid 3rd party libraries. I'm aware of this:
opal widgets
I've written a star rating widget myself a while ago. Feel free to use it:
/**
* The StarRaring is a widget that displays a predefined number of images side
* by side to realize a "star ranking". This star ranking has listeners to
* listen to the user input and adjust the number of stars painted with full
* alpha. The remaining stars are painted with a user defined alpha value
* (default: 150)
*
* #author Sebastian Raubach
*
*/
public class StarRating extends Composite
{
private Image image;
private int hoverSelected = 0;
private int selected = 0;
private int nrOfImages = 5;
private int alpha = 150;
private int width;
private int height;
private boolean vertical = false;
/**
* <p>
* Creates a star rating widget with a default selection of 1 star
* </p>
*
* #param parent
* the hosting composite
* #param style
* the widget style
*/
public StarRating(Composite parent, int style)
{
super(parent, style);
/* Add dispose listener for the image */
addListener(SWT.Dispose, new Listener()
{
#Override
public void handleEvent(Event arg0)
{
if(image != null)
image.dispose();
}
});
/* Add custom paint listener that paints the stars */
addListener(SWT.Paint, new Listener()
{
#Override
public void handleEvent(Event e)
{
paintControl(e);
}
});
/*
* Keep track of the mouse movements and highlight possible new
* selection
*/
addListener(SWT.MouseMove, new Listener()
{
#Override
public void handleEvent(Event arg0)
{
int x = arg0.x;
int y = arg0.y;
/* Determine direction */
int step = (vertical ? height : width) + 1;
int location = vertical ? y : x;
/* Determine current index */
int current = (location / step);
/* Redraw if necessary */
if (current != hoverSelected)
{
hoverSelected = current;
redraw();
}
}
});
/* On mouse exit, reset selection */
addListener(SWT.MouseExit, new Listener()
{
#Override
public void handleEvent(Event arg0)
{
hoverSelected = selected;
redraw();
}
});
/* On mouse up, set new selection based on hover selection */
addListener(SWT.MouseUp, new Listener()
{
#Override
public void handleEvent(Event arg0)
{
selected = hoverSelected;
}
});
}
/**
* <p>
* Draws the images. Selected images are drawn with full alpha, unselected
* images with the user defined alpha value
* </p>
*
* #param event
* The source event
*/
private void paintControl(Event event)
{
GC gc = event.gc;
if (image != null)
{
int stepX = vertical ? 0 : width + 1;
int stepY = vertical ? height + 1 : 0;
for (int i = 0; i < nrOfImages; i++)
{
if (i == hoverSelected + 1)
gc.setAlpha(alpha);
gc.drawImage(image, 1 + stepX * i, 1 + stepY * i);
}
/* Reset alpha value */
gc.setAlpha(255);
}
}
/**
* <p>
* Returns the image used for the star painting
* </p>
*
* #return the image used for the star painting
*/
public Image getImage()
{
return image;
}
/**
* <p>
* Set the image used for the star painting
* </p>
*
* #param image
* the image used for the star painting
*/
public void setImage(Image image)
{
this.image = new Image(Display.getDefault(), image, SWT.IMAGE_COPY);
width = image.getBounds().width;
height = image.getBounds().height;
redraw();
}
/**
* <p>
* Set the number of stars to be shown on the star rating
* </p>
* <p>
* Minimum = 1, Maximum = unrestricted
* </p>
*
* #param nrOfStars
* the number of stars to be shown on the star rating
*/
public void setNrOfStars(int nrOfStars)
{
if (nrOfStars < 1)
throw new IllegalArgumentException("Invalid value for number of stars. Minimum: 1, Selection: " + nrOfStars);
else
nrOfImages = nrOfStars;
}
/**
* <p>
* Returns the number of stars to be shown on the star rating
* </p>
*
* #return the number of stars to be shown on the star rating
*/
public int getNrOfStars()
{
return nrOfImages;
}
/**
* <p>
* Get the number of selected stars of the star rating
* </p>
*
* #return the number of selected stars of the star rating
*/
public int getSelection()
{
return selected + 1;
}
/**
* <p>
* Set the number of selected stars of the star rating
* </p>
* <p>
* Minimum = 1, Maximum = nr. of stars available
* </p>
*
* #param selection
* the number of selected stars of the star rating
*/
public void setSelection(int selection)
{
if (selection < 0 || selection > nrOfImages)
throw new IllegalArgumentException("Invalid value for star selection. Minimum: 0, Maximum: " + nrOfImages + ", Selection: " + selection);
else
selected = selection - 1;
hoverSelected = selected;
}
/**
* <p>
* Set the alpha value used for painting the non-selected stars
* </p>
* <p>
* Minimum = 0, Maximum = 255
* </p>
*
* #param alpha
* The alpha value used for painting the non-selected stars
*/
public void setAlpha(int alpha)
{
if (alpha < 0 || alpha > 255)
throw new IllegalArgumentException("Invalid alpha value. Minimum: 0, Maximum: 255, Selection: " + alpha);
else
this.alpha = alpha;
}
/**
* <p>
* Returns the alpha value used for painting the non-selected stars
* </p>
*
* #return the alpha value used for painting the non-selected stars
*/
public int getAlpha()
{
return alpha;
}
/**
* <p>
* Set the orientation of the widget to vertical
* </p>
*
* #param vertical
* Set to true if the stars should be aligned in a vertical and
* to false if the stars should be aligned in a horizontal line
*/
public void setVertical(boolean vertical)
{
this.vertical = vertical;
}
/**
* <p>
* Returns the orientation of the widget
* </p>
*
* #return true, if the widget is vertical, false if the widget is
* horizontal
*/
public boolean getVertical()
{
return vertical;
}
#Override
public Point computeSize(int wHint, int hHint, boolean changed)
{
int overallWidth = 0;
int overallHeight = 0;
/* Determine the preferred dimensions of the widget */
if (image != null)
{
overallWidth = vertical ? width : width * nrOfImages + nrOfImages - 1;
overallHeight = vertical ? height * nrOfImages + nrOfImages - 1 : height;
}
/* Consider hints */
if (wHint != SWT.DEFAULT && wHint < overallWidth)
overallWidth = wHint;
if (hHint != SWT.DEFAULT && hHint < overallHeight)
overallHeight = hHint;
/* Return computed dimensions plus border */
return new Point(overallWidth + 2, overallHeight + 2);
}
public static void main(String[] args)
{
Display display = Display.getDefault();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, false));
StarRating star = new StarRating(shell, SWT.NONE);
star.setImage(new Image(display, "star.png"));
star.setVertical(true);
star.setNrOfStars(10);
star.setSelection(3);
star.setAlpha(100);
star.setVertical(false);
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Looks like this:
Related
I am stuck on my Flood It game. I am trying to solve the problem of getting a starting matrix and finding the same color connected to the point (0,0). For instance if my starting matrix is generated:
1 4 5
1 1 1
5 3 2
It should capture the 1's so that on the next turn lets say I choose 4 then the matrix should be:
4 4 5
4 4 4
5 3 2
However when I choose 4 now the matrix is:
4 4 5
1 1 1
5 3 2
I know this is part of any normal Flood It game but I'm stuck on implementing it.
'
import java.awt.event.*;
import javax.swing.*;
public class GameController implements ActionListener {
private GameModel model;
private GameView view;
private MyStack<DotInfo> dots;
private int size;
private DotInfo dot;
/**
* Constructor used for initializing the controller. It creates the game's view
* and the game's model instances
*
* #param size
* the size of the board on which the game will be played
*/
public GameController(int size) {
this.size = size;
model = new GameModel(size);
view = new GameView(model, this);
dots = new MyStack<DotInfo>(size*size);
/*view.reset.addActionListener(this);
view.quit.addActionListener(this);
view.grey.addActionListener(this);
view.yellow.addActionListener(this);
view.blue.addActionListener(this);
view.green.addActionListener(this);
view.violet.addActionListener(this);
view.red.addActionListener(this);*/
}
/**
* resets the game
*/
public void reset(){
model.reset();
System.out.println(model);
}
/**
* Callback used when the user clicks a button (reset or quit)
*
* #param e
* the ActionEvent
*/
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof JButton) {
JButton x = (JButton) e.getSource();
if (x.getText()=="Quit") { //Quit button
System.exit(0);
}
else if (x.getText()=="Reset") { //Reset button
reset();
view.update();
}
}
}
/**
* <b>selectColor</b> is the method called when the user selects a new color.
* If that color is not the currently selected one, then it applies the logic
* of the game to capture possible locations. It then checks if the game
* is finished, and if so, congratulates the player, showing the number of
* moves, and gives two options: start a new game, or exit
* #param color
* the newly selected color
*/
public void selectColor(int color){
model.setCurrentSelectedColor(color);
sendCapturedToStack();
equalityCheck(color);
System.out.println(model);
}
/**
* On every turn <b>sendCapturedToStack</b> will push every captured dot to the stack.
* It will also allow the colours to change every time a user picks a color.
*/
private void sendCapturedToStack() {
model.capture
for (int j=0;j<size;j++) {
for (int i=0;i<size;i++) {
if (model.isCaptured(i, j)) {
model.capture(i, j);
dots.push(model.get(i,j));
}
}
}
}
/**
* <b>equalityCheck</b> checks to see if there is a dot to the left, right, above, or below
* another dot. If there is then the dot will be captured and pushed to the top of the stack.
* #param newColor
* the newly selected color
*/
private void equalityCheck(int newColor) {
while (!dots.isEmpty()) {
dot = dots.pop();
int x = dot.getX();
int y = dot.getY();
if (y<size-1 && model.getColor(x,y+1)==newColor && !model.isCaptured(x,y+1)) {
model.capture(x, y+1);
dots.push(model.get(x,y+1));
} if (x<size-1 && model.getColor(x+1,y)==newColor && !model.isCaptured(x+1,y)) {
model.capture(x+1, y);
dots.push(model.get(x+1,y));
} if (y>0 && model.getColor(x,y-1)==newColor && !model.isCaptured(x,y-1)) {
model.capture(x, y-1);
dots.push(model.get(x,y-1));
} if (x>0 && model.getColor(x-1,y)==newColor && !model.isCaptured(x-1,y)) {
model.capture(x-1, y);
dots.push(model.get(x-1,y));
}
}
}
}
I have one method of drawing the image where the text is get included in that image. Now the text contains emojis for ex:😍 and it's not getting printed as it is in image but the rectangular box like this [].
I have used java.text. and java.awt. classes for this
private void drawMessageString(Graphics2D g, final int messageHeight) {
int x = CRUSH_PADDING + CRUSH_MESSAGE_LR_PADDING;
int y = CRUSH_PADDING + TOP_BAR_HEIGHT + CRUSH_MESSAGE_TB_PADDING;
String myString="he new guy from New Zealand who is playing tennis 😍 😍";
try {
byte ptext[] = myString.getBytes("UTF-8");
String value;
value = new String(ptext, "UTF-8");
TextRenderer.drawString(g, value, FontUtils.getInstance().getFont(FONT_PROXIMA_NOVA, 24.0f), MSG_COLOR, new Rectangle(x, y, CRUSH_MESSAGE_WIDTH, messageHeight), TextAlignment.TOP, TextFormat.FIRST_LINE_VISIBLE);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
TextRenderer is nothing but a class created which cotains code to drawString and rectangle.
what should be the solution for this??
TextRenderer.java ==>
package com.text;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.font.FontRenderContext;
import java.awt.font.LineBreakMeasurer;
import java.awt.font.TextAttribute;
import java.awt.font.TextLayout;
import java.awt.geom.Rectangle2D;
import java.text.AttributedCharacterIterator;
import java.text.AttributedString;
/**
* A class which provides static methods for rendering text using alignment.
*
* #author Chris Copeland
* #version 1.0
*/
public final class TextRenderer {
/**
* Initialize a new instance of the {#link TextRenderer} class.
*/
private TextRenderer() {
}
/**
* Draws a string onto a <code>Graphics</code> handle, using a
* <code>Font</code>, <code>Color</code> and target bounds to calculate the
* location and automatic wrapping of text. The <i>align</i> property
* determines where the text will be positioned.
*
* #param g A <code>Graphics</code> handle which is the target of the draw
* operation
* #param text A <code>String</code> containing the text to draw
* #param font The <code>Font</code> to use when drawing the text
* #param color The <code>Color</code> to use when drawing the text
* #param bounds A <code>Rectangle</code> representing the bounds of the
* text
* #return A <code>Rectangle</code> representing the bounds consumed by the
* text
*/
public static Rectangle drawString(Graphics g, String text, Font font, Color color, Rectangle bounds) {
return drawString(g, text, font, color, bounds, TextAlignment.TOP_LEFT, TextFormat.NONE);
}
/**
* Draws a string onto a <code>Graphics</code> handle, using a
* <code>Font</code>, <code>Color</code> and target bounds to calculate the
* location and automatic wrapping of text. The <i>align</i> property
* determines where the text will be positioned.
*
* #param g A <code>Graphics</code> handle which is the target of the draw
* operation
* #param text A <code>String</code> containing the text to draw
* #param font The <code>Font</code> to use when drawing the text
* #param color The <code>Color</code> to use when drawing the text
* #param bounds A <code>Rectangle</code> representing the bounds of the
* text
* #param align A <code>TextAlignment</code> value representing the location
* to draw the text, relative to the <i>bounds</i>
* #return A <code>Rectangle</code> representing the bounds consumed by the
* text
*/
public static Rectangle drawString(Graphics g, String text, Font font, Color color, Rectangle bounds, TextAlignment align) {
return drawString(g, text, font, color, bounds, align, TextFormat.NONE);
}
/**
* Draws a string onto a <code>Graphics</code> handle, using a
* <code>Font</code>, <code>Color</code> and target bounds to calculate the
* location and automatic wrapping of text. The <i>align</i> property
* determines where the text will be positioned.
*
* #param g A <code>Graphics</code> handle which is the target of the draw
* operation
* #param text A <code>String</code> containing the text to draw
* #param font The <code>Font</code> to use when drawing the text
* #param color The <code>Color</code> to use when drawing the text
* #param bounds A <code>Rectangle</code> representing the bounds of the
* text
* #param align A <code>TextAlignment</code> value representing the location
* to draw the text, relative to the <i>bounds</i>
* #param format Additional formatting flags to use when drawing (see
* <code>TextFormat</code> class)
* #return A <code>Rectangle</code> representing the bounds consumed by the
* text
*/
public static Rectangle drawString(Graphics g, String text, Font font, Color color, Rectangle bounds, TextAlignment align, int format) {
if (g == null) {
throw new NullPointerException("The graphics handle cannot be null.");
}
if (text == null) {
throw new NullPointerException("The text cannot be null.");
}
if (font == null) {
throw new NullPointerException("The font cannot be null.");
}
if (color == null) {
throw new NullPointerException("The text color cannot be null.");
}
if (bounds == null) {
throw new NullPointerException("The text bounds cannot be null.");
}
if (align == null) {
throw new NullPointerException("The text alignment cannot be null.");
}
if (text.length() == 0) {
return new Rectangle(bounds.x, bounds.y, 0, 0);
}
Graphics2D g2D = (Graphics2D) g;
AttributedString attributedString = new AttributedString(text);
attributedString.addAttribute(TextAttribute.FOREGROUND, color);
attributedString.addAttribute(TextAttribute.FONT, font);
AttributedCharacterIterator attributedCharIterator = attributedString.getIterator();
FontRenderContext fontContext = new FontRenderContext(null, !TextFormat.isEnabled(format, TextFormat.NO_ANTI_ALIASING), false);
LineBreakMeasurer lineMeasurer = new LineBreakMeasurer(attributedCharIterator, fontContext);
Point targetLocation = new Point(bounds.x, bounds.y);
int nextOffset = 0;
if (align.isMiddle() || align.isBottom()) {
if (align.isMiddle()) {
targetLocation.y = bounds.y + (bounds.height / 2);
}
if (align.isBottom()) {
targetLocation.y = bounds.y + bounds.height;
}
while (lineMeasurer.getPosition() < text.length()) {
nextOffset = lineMeasurer.nextOffset(bounds.width);
nextOffset = nextTextIndex(nextOffset, lineMeasurer.getPosition(), text);
TextLayout textLayout = lineMeasurer.nextLayout(bounds.width, nextOffset, false);
if (align.isMiddle()) {
targetLocation.y -= (textLayout.getAscent() + textLayout.getLeading() + textLayout.getDescent()) / 2;
}
if (align.isBottom()) {
targetLocation.y -= (textLayout.getAscent() + textLayout.getLeading() + textLayout.getDescent());
}
}
if (TextFormat.isEnabled(format, TextFormat.FIRST_LINE_VISIBLE)) {
targetLocation.y = Math.max(0, targetLocation.y);
}
lineMeasurer.setPosition(0);
}
if (align.isRight() || align.isCenter()) {
targetLocation.x = bounds.x + bounds.width;
}
Rectangle consumedBounds = new Rectangle(targetLocation.x, targetLocation.y, 0, 0);
while (lineMeasurer.getPosition() < text.length()) {
nextOffset = lineMeasurer.nextOffset(bounds.width);
nextOffset = nextTextIndex(nextOffset, lineMeasurer.getPosition(), text);
TextLayout textLayout = lineMeasurer.nextLayout(bounds.width, nextOffset, false);
Rectangle2D textBounds = textLayout.getBounds();
targetLocation.y += textLayout.getAscent();
consumedBounds.width = Math.max(consumedBounds.width, (int) textBounds.getWidth());
switch (align) {
case TOP_LEFT:
case MIDDLE_LEFT:
case BOTTOM_LEFT:
textLayout.draw(g2D, targetLocation.x, targetLocation.y);
break;
case TOP:
case MIDDLE:
case BOTTOM:
targetLocation.x = bounds.x + (bounds.width / 2) - (int) (textBounds.getWidth() / 2);
consumedBounds.x = Math.min(consumedBounds.x, targetLocation.x);
textLayout.draw(g2D, targetLocation.x, targetLocation.y);
break;
case TOP_RIGHT:
case MIDDLE_RIGHT:
case BOTTOM_RIGHT:
targetLocation.x = bounds.x + bounds.width - (int) textBounds.getWidth();
textLayout.draw(g2D, targetLocation.x, targetLocation.y);
consumedBounds.x = Math.min(consumedBounds.x, targetLocation.x);
break;
}
targetLocation.y += textLayout.getLeading() + textLayout.getDescent();
}
consumedBounds.height = targetLocation.y - consumedBounds.y;
return consumedBounds;
}
/**
* Calculates the next maximum index of the string that will be displayed.
*
* #param nextOffset The index calculated using a
* <code>LineBreakMeasurer</code> <i>nextOffset</i> method
* #param measurerPosition The position within a
* <code>LineBreakMeasurer</code>
* #param text The text being rendered
* #return The next maximum index within the string
*/
private static int nextTextIndex(int nextOffset, int measurerPosition, String text) {
for (int i = measurerPosition + 1; i < nextOffset; ++i) {
if (text.charAt(i) == '\n') {
return i;
}
}
return nextOffset;
}
}
I used the font OpenSansEmoji.ttf which is combination of 3 fonts and now its working fine for me. the image is getting generated with the text and respective emojis.
private void drawMessageString(Graphics2D g, final int messageHeight) {
int x = CRUSH_PADDING + CRUSH_MESSAGE_LR_PADDING;
int y = CRUSH_PADDING + TOP_BAR_HEIGHT + CRUSH_MESSAGE_TB_PADDING;
String myString="he new guy from New Zealand who is playing tennis 😍 😍";
try {
byte ptext[] = myString.getBytes("UTF-8");
String value;
value = new String(ptext, "UTF-8");
TextRenderer.drawString(g, value, FontUtils.getInstance().getFont(FONT_OPEN_EMOJIFONT, 24.0f), MSG_COLOR, new Rectangle(x, y, CRUSH_MESSAGE_WIDTH, messageHeight), TextAlignment.TOP, TextFormat.FIRST_LINE_VISIBLE);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I'm getting wrong output. I'll put the 3 codes here, they are pretty basic as I've just started to study. (The third code is the tester)
The problem: in the tester I'm getting that input (0,0,0), but I need to get (50,40,30)
I think the main problem is here:
public void setColor (RGBColor color)
{
}
I tried playing with it, and I don't have a better solution than:
_color = new RGBColor(color);
_color.setRed(color.getRed());
_color.setGreen(color.getGreen());
_color.setBlue(color.getBlue());
RGBColor class.
/**
* This program is used to represent 3 Colors: Red, Green, Blue. (RGB)
* These colors hold values between 0 and 255.
*
*
* #Author doesn't matter ;)
*/
public class RGBColor
{
/**
* attributes: red, green and blue component of a color.
*/
private int _red,_green,_blue;
/**
* final variables.
*/
private final int MAX_VALUE = 255,MIN_VALUE = 0;
private final double THIRTY_PERCENT = 0.3,FIFTY_NINE_PERCENT = 0.59,ELEVEN_PERCENT = 0.11;
/**
* Consctructor which gets 3 colors (RGB), we check here if their range is valid (0 - 255), if not we assign black to it.
*
* #param red - The red color component value.
* #param green - The green color component value.
* #param blue - The blue color component value
*/
public RGBColor(int red, int green, int blue)
{
if(isValid(red,green,blue))
{
_red = red;
_green = green;
_blue = blue;
}
else
doBlack();
}
/**
* Construct a black RGBColor. i.e. red = green = blue = 0
*/
public RGBColor()
{
doBlack();
}
/**
* Construct a new RGBColor which is a copy of the given color.
*
* #param other - The RGBColor to copy.
*/
public RGBColor(RGBColor other)
{
_red = other._red;
_green = other._green;
_blue = other._blue;
}
/**
* Returns the Red component of RGBColor.
*
* #return The red color component value of this RGBColor.
*/
public int getRed()
{
return _red;
}
/**
* Returns the Green component of RGBColor.
*
* #return The green color component value of this RGBColor.
*/
public int getGreen()
{
return _green;
}
/**
* Returns the blue component of RGBColor.
*
* #return The blue color component value of this RGBColor.
*/
public int getBlue()
{
return _blue;
}
/**
* Sets the red color component value of this RGBColor, only if the color range is valid (0-255).
*
* #param num - The red color component value to set.
*/
public void setRed(int num)
{
if(isValid(num))
_red = num;
}
/**
* Sets the green color component value of this RGBColor, only if the color range is valid (0-255).
*
* #param num - The green color component value to set.
*/
public void setGreen(int num)
{
if(isValid(num))
_green = num;
}
/**
* Sets the blue color component value of this RGBColor, only if the color range is valid (0-255).
*
* #param num - The blue color component value to set.
*/
public void setBlue(int num)
{
if(isValid(num))
_blue = num;
}
/**
* Compares the 3 RGB colors, returns true if all are equal.
*
* #return true if the RGBColors are equal; false otherwise.
*/
public boolean equals(RGBColor other)
{
return ((_red == other._red) &&
(_green == other._green) &&
(_blue == other._blue));
}
/**
* Changes this color to be a mix of this and other RGBColors, It simply takes this color and other RGBColor and makes and average of them.
* For example (255,0,0) and (0,0,255) becomes: (127,0,127). Note that it returns integer numbers and not fraction numbers.
*
* #param other is the other color.
*/
public void mix(RGBColor other)
{
_red = (_red + other._red) / 2;
_green = (_green + other._green) / 2;
_blue = (_blue + other._blue) / 2;
}
/**
* Returns the grayscale value of this RGBColor.
* Grayscale is defined by taking Red multipled by 30% plus green multiplied by 59% plus blue multipled by 11%.
*
* #return The grayscale value of this RGBColor, a double number.
*/
public double convertToGrayscale()
{
return ((THIRTY_PERCENT * _red) + (FIFTY_NINE_PERCENT * _green) + (ELEVEN_PERCENT * _blue));
}
/**
* Inverts the color of RGBColor, every spot is reduced relative to 255. For example: (10,20,30) becomes (245,235,225).
*/
public void invert()
{
_red = (MAX_VALUE - _red);
_green = (MAX_VALUE - _green);
_blue = (MAX_VALUE - _blue);
}
/**
* Here we check if the color number was entered correctly.
* It has to be an integer (whole number) between 0-255.
*
* #param nums - a component value, should be the number between 1-4
* #param return - return true if the number is between 1-4, false otherwise.
*/
private boolean isValid(int nums)
{
return ((nums >= MIN_VALUE) && (nums <= MAX_VALUE));
}
/**
* Here we check if the color number was entered correctly.
* It has to be an integer (whole number) between 0-255.
*
* #param red - the red component
* #param green - the green component
* #param blue - the red component
* #param return true if values are correct, false otherwise.
*/
private boolean isValid(int red, int green, int blue)
{
return ((red <= MAX_VALUE && red >= MIN_VALUE &&
green <= MAX_VALUE && green >= MIN_VALUE &&
blue <= MAX_VALUE && blue >= MIN_VALUE));
}
/**
* Returns RGB color string triplet with numbers between 0-255, i.e. (0,127,127)
*/
public String toString()
{
return ("(" + _red + "," + _green + "," + _blue + ")");
}
/**
* RGBColor will become the color Black. (0,0,0)
*/
private void doBlack()
{
_red = _green = _blue = 0;
}
}
LightBulb Class.
/**
* In this program we use _color to represent the color of the bulb
* And we use _switchedOn to represent whether the bulb is turned on or off.
*
* #author
* #date 20/11/2014.
*/
public class LightBulb
{
/**
instance private variables of the lightbulb's color and the switch state.
*/
private static RGBColor _color;
private boolean _switchedOn;
private int ZERO = 0;
private int MAX = 255;
/**
* Construct a new LightBulb with the given color component values.
* Check if atleast one of the color isn't in the given value range (0-255)
* if not in the range, the default color is black (0,0,0).
*/
public LightBulb(int red, int green, int blue)
{
if(isTrue(red,green,blue))
_color = new RGBColor(red,green,blue);
else
_color = new RGBColor(ZERO,ZERO,ZERO);
_switchedOn = false;
}
/**
* Construct a new LightBulb which is a copy of the given bulb, and turn switch off.
*/
public LightBulb(RGBColor color)
{
_color = new RGBColor(color);
_switchedOn = false;
}
/**
* Construct a new LightBulb which is a copy of the given LightBulb
*/
public LightBulb (LightBulb other)
{
_color = new RGBColor(other._color);
_switchedOn = other._switchedOn;
}
//Return the current color of the bulb.
public RGBColor getColor()
{
return new RGBColor(_color);
}
//Sets the color of the bulb.
public void setColor (RGBColor color)
{
_color = new RGBColor(color);
}
//Checks if the bulb is on, if it is - returns true. else, returns false.
public boolean isSwitchedOn()
{
if(_switchedOn)
return true;
else
return false;
}
//If the lightbulb is turned on, it turns it off. else,if it is off - it turns it on.
public void switchLight()
{
if(_switchedOn)
_switchedOn = false;
else
_switchedOn = true;
}
//Returns a string which prints a triplet of the color in numbers of range (0-255), and the switch state of the bulb.
// e.g. (255,255,0) On , e.g. (255,127,0) Off
public String toString()
{
_color = new RGBColor();
return ("(" +_color.getRed()+ "," +_color.getGreen()+ "," + _color.getBlue() + ")" + " " + switchState());
}
//If the switch is on - returns a string "On". else, if it is off returns a string "Off".
public String switchState()
{
if(_switchedOn)
return ("On");
else
return ("Off");
}
public boolean isTrue(int red, int green, int blue)
{
if( red >= ZERO && red <= MAX &&
green >= ZERO && green <= MAX &&
blue >= ZERO && blue <= MAX )
return true;
else
return false;
}
}
LightBulb tester class.
/**
* Write a description of class Tester here.
*
* #author
* #version (a version number or a date)
*/
public class LightBulbTester
{
/**
*
*/
public static void main(String[] args)
{
// Create two light bulb objects
LightBulb l1 = new LightBulb(127,0,127);
LightBulb l2 = new LightBulb(new RGBColor(127,0,127));
LightBulb l3 = new LightBulb(l2);
// Print (test the get method)
System.out.println("Welcome to Light Bulb tester");
System.out.println("1) color of light object is " + l1.getColor());
// Test the set method
l1.setColor(new RGBColor(50,40,30));
System.out.println("2) The new color of light is :" + l1);
// Test isSwitchedOn
System.out.println("3) Light object is switched on? " + l1.isSwitchedOn());
// Now switch on
l1.switchLight();
System.out.println("4) Light after switchLight():" + l1);
System.out.println("Good luck!");
}
}
In this code -
public String toString()
{
_color = new RGBColor();
return ("(" +_color.getRed()+ "," +_color.getGreen()+ "," + _color.getBlue() + ")" + " " + switchState());
}
you're always outputting the value of a new RGBColor object, which will always give you black. You probably don't want to set _color's value here; just remove that line (or replace it with a null check).
Also, remove the static qualifier from the _color property. You don't want to share the value between all bulbs!
I had been through this link1 link2 for an answer, but was not helpful.
Because
frame.setVisible(false);
is giving below exception,
Exception in thread "main" java.lang.NullPointerException
at Simulation.drawOcean(Simulation.java:72)
at Simulation.main(Simulation.java:191)
Application has below 2 java files.
Simulation class launches GUI.
After running Simulation.java with default command line parameters, Frame does not get close, despite finite while loop.
I could not close the Frame by clicking close button(top right - red), after the simulation is done.
Do i need to set some property for Frame?
Please help me!!!
/* Simulation.java */
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.util.Random;
/* DO NOT CHANGE THIS FILE (except as noted). */
/* (You may wish to make temporary changes or insert println() statements) */
/* while testing your code. When you're finished testing and debugging, */
/* though, make sure your code works with the original version of this file */
/**
* The Simulation class is a program that runs and animates a simulation of
* Sharks and Fish.
*
* The Simulation program takes up to four parameters. The first two specify
* the width and height of the ocean. The third parameter specifies the value
* of starveTIme. For example, if you run
*
* java Simulation 25 25 1
*
* then Simulation will animate a 25x25 ocean with a starveTime of 1. If you
* run "java Simulation" with no parameters, by default Simulation will animate
* a 50x25 ocean with a starveTime of 3. With some choices of parameters,
* the ocean quickly dies out; with others;, it teems forever.
*
* #author mohet01
*
*/
public class Simulation {
/**
* The constant cellSize determines the size of each cell on the screen
* during animation. (You may change this if you wish).
*/
private static final int cellSize = 4;
/**
* Default parameters. (You may change this of you wish).
*/
private static int i = 50; //Default ocean width
private static int j = 25; //Default ocean height
private static int starveTime = 3; //Default shark starvation time
/**
* drawOcean() adds cell contents as part of graphics
*/
private static void drawOcean(Graphics graphics, Ocean ocean){
if(ocean != null){
int width = ocean.width();
int height = ocean.height();
for(int row = 0; row < height; row++){
for(int col = 0; col < width; col++){
int contents = ocean.cellContents(row, col);
if(contents == Ocean.SHARK){
//Draw a red Shark
graphics.setColor(Color.red);
graphics.fillRect(row*cellSize, col*cellSize, cellSize, cellSize);
}else if(contents == Ocean.FISH){
// Draw a green fish
graphics.setColor(Color.green);
graphics.fillRect(row * cellSize, col * cellSize, cellSize, cellSize);
}else{
//Clear the rectangle
graphics.clearRect(row, col, cellSize, cellSize);
}
}
}
}
}
/**
* main() reads the parameters and performs the simulation and animation.
* #param args
* #throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
Ocean sea;
/**
* Read the input parameters
*/
if(args.length >0){
try{
i = Integer.parseInt(args[0]);
}catch(NumberFormatException e){
System.out.println("First argument to Simulation is not a number.");
}
}
if(args.length > 1){
try{
j = Integer.parseInt(args[1]);
}catch(NumberFormatException e){
System.out.println("Second argument to Simulation is not a number");
}
}
if(args.length > 2){
try{
starveTime = Integer.parseInt(args[2]);
}catch(NumberFormatException e){
System.out.println("Third argument to Simulation is not a number");
}
}
/**
* Create a window on your screen
*/
Frame frame = new Frame("Sharks and Fish");
frame.setSize(i*cellSize + 10, j*cellSize + 30);
frame.setVisible(true);
/**
* Create a "Canvas" we can draw upon; attach it to the window
*/
Canvas canvas = new Canvas();
canvas.setBackground(Color.white);
canvas.setSize(i*cellSize, j*cellSize);
frame.add(canvas);
Graphics graphics = canvas.getGraphics();
/**
* Create the initial ocean.
*/
sea = new Ocean(i, j, starveTime);
/**
* Visit each cell (in a roundabout order); randomnly place a fish, shark,
* or nothing in each.
*/
Random random = new Random(0);
int x = 0;
int y = 0;
for(int row = 0;row < j; row++){
//This will visit every x-coordinate once.
x = (x + 78887) %j;
if((x & 8) == 0){
for(int col = 0; col < i; col++){
//This will visit every y coordinate once.
y = (y+78887)%i;
if((y & 8) == 0){
int r = random.nextInt();
if(r < 0){
//50% of cells start with fish
//x - width, y - height
sea.addFish(x, y);
}else if(r > 1500000000){
//~15% of cells start with sharks
sea.addShark(x, y);
}
}
}
}
}
/**
* Perform timesteps forever
*/
int timeLeft = 20;
while (timeLeft > 0) {
// Wait one second (1000 milliseconds)
Thread.sleep(1000);
// Draw the current ocean
drawOcean(graphics, sea);
// For fun, you might wish to change the delay in the next line.
// If you make it too short, though, the graphics won't work properly.
// Simulate a timestep
sea = sea.timeStep();
timeLeft--;
}
}
}
/* Ocean.java */
/**
* The Ocean class defines an object that models an ocean full of sharks and
* fish. Descriptions of the methods you must implements appear below. They
* include a constructor of the form
*
* public Ocean(int i, int j, int starveTime);
*
* that creates an empty ocean having width i and height j, in which sharks
* starve after starveTime timesteps.
*
* See the README file accompanying this project for additional details.
*
* #author mohet01
*
*/
public class Ocean {
/**
* Do not rename these constants. WARNING: if you change the numbers, you
* will need to recompile Test4.java. Failure to do so will give you a very
* hard-to-find bug.
*/
public final static int EMPTY = 1;
public final static int SHARK = 2;
public final static int FISH = 3;
/**
* Define any variables associated with an Ocean object here. These
* variables MUST be private.
*
*/
private final static int UNKNOWN = -1; // for unknown return type
private int width;
private int height;
private int[][] oceanMatrix;
//TODO space optimization on below matrix
private int[][] sharkHungerLevelMatrix;
private int starveTime;
/**
* The following methods are required for Part I.
*
*/
/**
* Constructor that creates an empty ocean having width i and
* height j, in which sharks starve until after starveTime timesteps.
*
* #param width(i)
* is the width of the ocean.
* #param height(j)
* is the height of the ocean.
* #param starveTime
* is the number of timeSteps sharks survive without food.
*/
public Ocean(int i, int j, int starveTime) {
this.width = i;
this.height = j;
this.oceanMatrix = new int[j][i];
this.sharkHungerLevelMatrix = new int[j][i];
this.starveTime = starveTime;
for (int row = 0; row < j; row++) {
for (int col = 0; col < i; col++) {
oceanMatrix[row][col] = EMPTY;
}
}
for (int row = 0; row < j; row++) {
for (int col = 0; col < i; col++) {
sharkHungerLevelMatrix[row][col] = EMPTY;
}
}
}
/**
* width() returns the width of an ocean Object.
*
* #return the width of the ocean.
*
*/
public int width() {
return this.width;
}
/**
* height() returns the height of an Ocean object.
*
* #return the height of the Ocean.
*/
public int height() {
return this.height;
}
/**
* starveTime() returns the number of timesteps sharks survive without food.
*
* #return the number of timesteps sharks survive without food.
*/
public int starveTime() {
return starveTime;
}
/**
* addFish() places a fish in cell (x,y) if the cell is empty. If the cell
* is already occupied, leave the cell as it is.
*
* #param x
* is the x-coordinate of the cell to place a fish in.
* #param y
* is the y-coordinate of the cell to place a fish in.
*/
public void addFish(int x, int y) {
if (oceanMatrix[x][y] == EMPTY) {
oceanMatrix[x][y] = FISH;
}
}
/**
* addShark() (with two parameters) places a newborn shark in cell (x, y) if
* the cell is empty. A "newborn" shark is equivalent to a shark that has
* just eaten. If the cell is already occupied, leave the cell as it is.
*
* #param x
* is the x-coordinate of the cell to place a shark in.
* #param y
* is the y-coordinate of the cell to place a shark in.
*/
public void addShark(int x, int y) {
if (oceanMatrix[x][y] == EMPTY) {
oceanMatrix[x][y] = SHARK;
}
}
/**
* cellContents() returns EMPTY is cell (x,y) is empty, FISH if it contains
* a fish, and SHARK if it contains a shark.
*
* #param x
* is the x-coordinate of the cell whose contents are queried.
* #param y
* is the y-coordinate of the cell whose contents are queried.
*/
public int cellContents(int x, int y) {
return oceanMatrix[x][y];
}
/**
* isFish() checks for the existence of fish in that cell.
* #param x
* is the x-coordinate of the cell whose contents are queried.
* #param y
* is the y-coordinate of the cell whose contents are queried.
* #return the boolean value
*/
private boolean isFish(int x, int y){
return (this.oceanMatrix[x][y] == Ocean.FISH);
}
/**
* isShark() checks for the existence of shark in that cell.
* #param x
* is the x-coordinate of the cell whose contents are queried.
* #param y
* is the y-coordinate of the cell whose contents are queried.
* #return the boolean value
*/
private boolean isShark(int x, int y){
return (this.oceanMatrix[x][y] == Ocean.SHARK);
}
/**
* isSharkStarving() checks the hunger level of shark, if reached to starveTime level
* #param x
* is the x-coordinate of the cell whose contents are queried.
* #param y
* is the y-coordinate of the cell whose contents are queried.
* #return the boolean value
*/
private boolean isSharkStarving(int x, int y){
return (this.sharkHungerLevelMatrix[x][y] == (this.starveTime+1));
}
/**
* checkFish() checks the existence of atleast one fish
* surrounding shark cell
* #param x
* is the x-coordinate of the cell whose contents are queried.
* #param y
* is the y-coordinate of the cell whose contents are queried.
* #return returns true on atleast one fish exist otherwise false
*
*/
private boolean checkFish(int x, int y){
for(int i = x-1;i <= x+1; i++){
for(int j = y-1; j <= y+1; j++){
if(this.isFish(mod(i,this.height), mod(j,this.width))){
return true;
}
}
}
return false;
}
/**
* countShark() counts the number of sharks surrounding queried cell
* #param x
* is the x-coordinate of the cell whose contents are queried.
* #param y
* is the y-coordinate of the cell whose contents are queried.
* #return returns number of sharks surrounding fish cell
*/
private int countShark(int x, int y){
int neighbourSharkCount = 0;
for(int i = x-1;i <= x+1; i++){
for(int j = y-1; j <= y+1; j++){
if(this.isShark(mod(i,this.height), mod(j,this.width))){
neighbourSharkCount++;
}
} // end inner for loop
}//end outer for loop
return neighbourSharkCount;
}
/**
* countFish() counts the number of fish surrounding queried cell
* #param x
* is the x-coordinate of the cell whose contents are queried.
* #param y
* is the y-coordinate of the cell whose contents are queried.
* #return returns number of sharks surrounding queried cell
*/
private int countFish(int x, int y){
int neighbourFishCount = 0;
for(int i = x-1;i <= x+1; i++){
for(int j = y-1; j <= y+1; j++){
if(this.isFish(mod(i,this.height), mod(j,this.width))){
neighbourFishCount++;
}
} // end inner for loop
}//end outer for loop
return neighbourFishCount;
}
/**
* mod() performs the modulo operation using euclidean divison
*
* #param n
* is the numerator
* #param d
* is the denominator
* #return the remainder
*/
private int mod(int n, int d) {
if (n >= 0)
return n % d;
else
return d + ~(~n % d);
}
/**
* timeStep() performs a simulation timestep as described in README.
*
* #return an ocean representing the elapse of one timestep.
*/
public Ocean timeStep() {
Ocean sea = new Ocean(width, height, starveTime);
for (int row = 0; row < this.height; row++) {
for (int col = 0; col < this.width; col++) {
switch(this.oceanMatrix[row][col]){
case Ocean.SHARK:
boolean gotTheFish = false;
//Check all the 8 neighbors of a Shark Cell for fish
if(this.checkFish(row,col)){
gotTheFish = true;
}
//Updating Shark Cell
if(gotTheFish){
/*
* 1) If a cell contains a shark, and any of its neighbors is a fish, then the
* shark eats during the time step, and it remains in the cell at the end of the
* time step. (We may have multiple sharks sharing the same fish. This is fine;
* they all get enough to eat.)
*/
sea.oceanMatrix[row][col] = Ocean.SHARK; // for next time step
}else{
/*
* 2) If a cell contains a shark, and none of its neighbors is a fish, it gets
* hungrier during the time step. If this time step is the (starveTime + 1)th
* time step the shark has gone through without eating, then the shark dies
* (disappears). Otherwise, it remains in the cell.
*/
this.sharkHungerLevelMatrix[row][col]++;
if(this.isSharkStarving(row,col)){
this.oceanMatrix[row][col] = Ocean.EMPTY; // for this time step
this.sharkHungerLevelMatrix[row][col] = Ocean.EMPTY; // for this time step
}
sea.sharkHungerLevelMatrix[row][col] = this.sharkHungerLevelMatrix[row][col]; // for next time step
sea.oceanMatrix[row][col] = this.oceanMatrix[row][col]; // for next time step
}
break;
case Ocean.FISH:
int neighbourSharkCount=0;
//Check all the 8 neighbors of a Fish cell to count for sharks
neighbourSharkCount=countShark(row,col);
//Updating fish cell for current & next time step
if(neighbourSharkCount ==1){
/*
* 4) If a cell contains a fish, and one of its neighbors is a shark, then the
* fish is eaten by a shark, and therefore disappears.
*/
this.oceanMatrix[row][col] = Ocean.EMPTY; //fish disappears this time step
}
else if(neighbourSharkCount > 1){
/*
* 5) If a cell contains a fish, and two or more of its neighbors are sharks, then
* a new shark is born in that cell. Sharks are well-fed at birth; _after_ they
* are born, they can survive an additional starveTime time steps without eating.
*/
sea.oceanMatrix[row][col] = Ocean.SHARK; // new shark for next time step
}
else if(neighbourSharkCount < 1){
/*
* 3) If a cell contains a fish, and all of its neighbors are either empty or are
* other fish, then the fish stays where it is.
*/
sea.oceanMatrix[row][col] = FISH; //for next time step
}
break;
case Ocean.EMPTY:
int fishCount=0;
int sharkCount=0;
//Check all the 8 neighbors of an Empty cell to count sharks and Fish
fishCount = this.countFish(row,col);
sharkCount = this.countShark(row, col);
//Update Empty Cell for current & next time step.
/* (no need to handle this case)
* 6) If a cell is empty, and fewer than two of its neighbors are fish, then the
* cell remains empty.
*/
if((fishCount >= 2) && (sharkCount <=1)){
/*
* 7) If a cell is empty, at least two of its neighbors are fish, and at most one
* of its neighbors is a shark, then a new fish is born in that cell.
*/
this.oceanMatrix[row][col] = FISH;// for current time step
sea.oceanMatrix[row][col] = FISH; //for next time step
}else if((fishCount >= 2) && (sharkCount >= 2)){
/*
* 8) If a cell is empty, at least two of its neighbors are fish, and at least two
* of its neighbors are sharks, then a new shark is born in that cell. (The new
* shark is well-fed at birth, even though it hasn’t eaten a fish yet.)
*/
sea.oceanMatrix[row][col] = Ocean.SHARK; // for next time step
}
break;
}
}//end inner for loop
}//end outer for loop
return sea;
}
/**
* The following method is required for Part II.
*
*
*/
/**
* addShark() (with three parameters) places a shark in cell (x, y) if the
* cell is empty. The shark's hunger is represented by the third parameter.
* If the cell is already occupied, leave the cell as it is, You will need
* this method to help convert run-length encodings to Oceans.
*
* #param x
* is the x-coordinate of the cell to place a shark in.
* #param y
* is the y-coordinate of the cell to place a shark in.
* #param feeding
* is an integer that indicates the shark's hunger. You may
* encode it any way you want; for instance, "feeding" may be the
* last timestep the shark was fed, or the amount of time that
* has passed since the shark was last fed, or the amount of time
* left before the shark will starve. It's upto you, but be
* consistent.
*/
public void addShark(int x, int y, int feeding) {
this.oceanMatrix[x][y] = Ocean.SHARK;
this.sharkHungerLevelMatrix[x][y] = feeding;
}
/**
* The following method is required for Part III.
*/
/**
* sharkFeeding() returns an integer that indicates the hunger of the shark
* in cell (x, y), using the same "feeding" representation as the parameter
* to addShark() described above. If cell (x, y) does not contain a shark,
* then its return value is undefined--that is, anything you want. Normally,
* this method should not be called if cell (x, y) does not contain a shark.
* You will need this method to help convert Oceans to run-length encodings.
*
* #param x
* is the x-coordinate of the cell whose contents are queried.
* #param y
* is the y-coordinate of the cell whose contents are queried.
*
*/
public int sharkFeeding(int x, int y) {
if(this.isShark(x, y)){
return this.sharkHungerLevelMatrix[x][y];
}
return Ocean.UNKNOWN;
}
}
While this is not a Swing program, you can substitute JFrame for Frame to leverage EXIT_ON_CLOSE.
JFrame frame = new JFrame("Sharks and Fish");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
The AWT approach is to add a WindowListener.
frame.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Have you tried?
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
I am adding bitmap animations into my simple 2D android game engine(if you can call it one yet.. :D) and I am facing problem of cycling through frames at pre defined FPS/rate.
What I have done for now is simply use System to get currentTimeMillis and use it as reference but I am pretty sure that's not the best decision.. I was thinking I could pass current frame to the drawing method and use that to the drawing method.
Currently I have object that you get the drawable from and I use it on renderer like this:
canvas.drawBitmap(animation.getDrawable(), (int)x, (int)y, null);
and getDrawable() returns current frame of the animation or the only frame if its still image and heres the code
if(frames.size() > 1) {
long current = System.currentTimeMillis();
if (currentFrame == frames.size() - 1 && frameDirection == 1) {
frameDirection = -1;
} else if (currentFrame == 0 && frameDirection == -1) {
frameDirection = 1;
}
if ((current - lastFrame) > delay) {
currentFrame += frameDirection;
lastFrame = current;
}
}
return frames.get(currentFrame);
and in my eyes that just looks so bad and unefficient.. What can I do about this? Is the only way to go passing current frame to getDrawable or how should this be done?
If I understand this right, you want to create an animation class for you game engine?
In my Game Engine I've got a class called SpriteSheetAnimation, which basically iterates through every frame of the animation when the update method is called - I'm sure you'll understand the logic behind it and you'll find a way to use it:
public class SpriteSheetAnimation extends GameObjectTemplate {
int animationSheetLength;
int animationSheetHeight;
int columns;
int rows;
Rect srcRect;
int left;
int bottom;
int lengthOfSpriteInSheet;
int heightOfSpriteInSheet;
boolean repeatAnimation;
boolean animationCompleted;
boolean animationStarted;
Bitmap bitmap;
/**
* You input a bitmap and the number of sprites (by defining the number of rows and columns), and this class will
* take care of animating the sheet.
*
*/
public SpriteSheetAnimation(WorldTemplate world, Bitmap animationSheet, int amountOfRows, int amountOfColumns) {
super(world);
this.animationSheetLength = animationSheet.getWidth();
this.animationSheetHeight = animationSheet.getHeight();
this.columns = amountOfColumns;
this.rows = amountOfRows;
this.lengthOfSpriteInSheet = this.animationSheetLength/this.columns;
this.heightOfSpriteInSheet = this.animationSheetHeight/this.rows;
srcRect = new Rect();
left = 0;
bottom = this.heightOfSpriteInSheet;
repeatAnimation = false;
animationCompleted = false;
animationStarted = false;
this.bitmap = animationSheet;
this.drawShapesOfSprites = true;
this.gameObjectShape.addRectangle(new Rect(0, 0, 0 + this.heightOfSpriteInSheet, 0 + this.lengthOfSpriteInSheet));
}
#Override
public void defineGameObjectPositionShape() {
this.gameObjectShape.addRectangle(new Rect());
}
/**
* <b><i>public Rect getSrcRect()</i></b>
* <br>
* Since: API 1
* <br>
* <br>
* Retrieve the rect that will cover the current source of the sheet.
*
* #return
* The current rect that will cover the current source of the sheet.
*
*/
public Rect getSrcRect() {
return srcRect;
}
/**
* <b><i>public Rect getDstRect()</i></b>
* <br>
* Since: API 1
* <br>
* <br>
* Retrieve the rect where the bitmap will be scaled to fit into.
*
* #return
* The current rect where the bitmap will be scaled to fit into.
*
*/
public Rect getDstRect() {
return this.getGameObjectPositionRect();
}
/**
* <b><i>public void repeatAnimation(boolean repetition)</i></b>
* <br>
* Since: API 1
* <br>
* <br>
* This method will set if the animation should be repeated or not.
*
* #param state
* <br><br>
* 1. True = The animation will repeat once completed and keep on doing so.
* <br><br>
* 2. False = The animation will play only once.
*
*
*/
public void repeatAnimation(boolean repetition) {
this.repeatAnimation = repetition;
}
/**
* <b><i>public boolean isAnimationFinished()</i></b>
* <br>
* Since: API 1
* <br>
* <br>
* Retrieve if the animation has finished.
*
* #return
* If the animation has finished.
*
*/
public boolean isAnimationFinished() {
return animationCompleted;
}
/**
* <b><i>public boolean hasBeenStarted()</i></b>
* <br>
* Since: API 1
* <br>
* <br>
* Retrieve if the animation has started.
*
* #return
* If the animation has started.
*
*/
public boolean hasBeenStarted() {
return animationStarted;
}
/**
* <b><i>public void render()</i></b>
* <br>
* Since: API 1
* <br>
* <br>
* This method will render the animation (the current picture of the sheet).
*
*/
public void render() {
world.game.getGraphics().getCanvasGameScreen().drawBitmap(this.bitmap, this.getSrcRect(), this.getGameObjectPositionRect(), null);
}
/**
* <b><i>public void update()</i></b>
* <br>
* Since: API 1
* <br>
* <br>
* This method will update the animation.
*
*/
#Override
public void update() {
// Every update will advance to the next frame of the animation
// Set if animation has started
if(!animationStarted) {
animationStarted = true;
}
// Set the frame
srcRect.set(left, (bottom - this.heightOfSpriteInSheet), (left + this.lengthOfSpriteInSheet), bottom);
// Change the location, so the next time the frame is set, it will be set accordingly
left = left + this.lengthOfSpriteInSheet;
if(left == this.animationSheetLength && bottom == this.animationSheetHeight) {
if(repeatAnimation == true) {
left = 0;
bottom = 0;
}
else {
animationCompleted = true;
}
}
if(left == this.animationSheetLength) {
bottom = bottom + this.heightOfSpriteInSheet;
left = 0;
}
}
}
I hope this helps you and gets you started.