Is their anyway to check scenario lag before uploading it in the website
I m telling this because sometimes when i upload a scenario, that starts lagging in the website but it doesn't lag in Greenfoot app
Will It help me to check game lag....
public class Time extends greenfoot.Actor{
public static double MAX_DELTA_TIME = 0.08;
public static double AVERAGE_DELTA_TIME = 0.01;
long lastMillis;
double deltaTime = AVERAGE_DELTA_TIME;
public double timeScale = 1;
public boolean useStaticFramelength = false;
public double staticFramelength = 0.001;
long frameIndex = 0;
double timeSinceFpsUpdate = 0;
int frameNum;
int frameCount;
int stableFps;
public Time(){
setImage(new greenfoot.GreenfootImage(1, 1));
}
public void act(){
//TODO: Use System.nanoTime()
long currentMillis = System.currentTimeMillis();
deltaTime = (currentMillis - lastMillis) / 1000.0;
lastMillis = currentMillis;
deltaTime %= 1;
timeSinceFpsUpdate += deltaTime;
frameNum++;
frameCount += fps();
if(timeSinceFpsUpdate >= 1){
timeSinceFpsUpdate %= 1;
stableFps = (int)(frameCount / (double)frameNum);
frameNum = frameCount = 0;
}
frameIndex++;
}
/**
* Fraction of time since the last frame
*/
public double deltaTime(){
if(useStaticFramelength) return staticFramelength;
if(deltaTime < MAX_DELTA_TIME) return deltaTime * timeScale;
return MAX_DELTA_TIME * timeScale;
}
public void setTimeScale(double scale){
timeScale = scale;
}
/**
* Updated once per frame
*/
public int fps(){
if(deltaTime == 0) return 2000;
return (int)(1 / deltaTime);
}
/**
* Updated once per second
*/
public int stableFps(){
return stableFps;
}
public long frameIndex(){
return frameIndex;
}
public void resetFrameIndex(){
frameIndex = 0;
}
}
import greenfoot.*;
/**
* This is a Button class. Use it for interfaces. The Button works completly
* automaticly, you do not have to feed it any information after the
* construction. It offers selveral methods to get information about
* the users interactions with it. It may also go to a certain world when clicked
* to move through menus.
*
* @author RcCookie
* @version 1.2
*/
public class Button extends Actor
{
/**
* The number of times this button has been clicked.
*/
protected int clickCount;
/**
* The number of frames this button was held down.
*/
protected int pressTime;
/**
* Weather the button is touched by the mouse right now.
*/
protected boolean touched;
/**
* Weather the button is pressed down by the mouse right now.
*/
protected boolean pressed;
/**
* The text on the button.
*/
protected String name;
/**
* The image given as input, or null if a color was chosen.
*/
protected GreenfootImage inputImage;
protected Text inputText;
/**
* The x and y size of the button.
*/
protected int x, y;
/**
* The color used to fill the background if inputImage is null.
*/
protected Color color;
/**
* The font size for the buttons text.
*/
protected int fontSize;
/**
* Weather there should be an outline around the button.
*/
protected boolean drawFrame;
/**
* The world to switch to after the button was clicked, or null.
*/
protected World onClick;
/**
* The standart image of the button.
*/
protected GreenfootImage image;
/**
* The buttons image when hovered over it with the mouse.
*/
protected GreenfootImage hoveredImage;
/**
* The buttons image when it is being clicked.
*/
protected GreenfootImage clickedImage;
public boolean enabled = true;
/**
* Constructs a new grey Button with the given title and a default size
* of at least 90 times 35 pixels.
*
* @param title The text printed onto the button
*/
public Button(String title){
inputText = new Text(title, 25, Color.BLACK, Color.LIGHT_GRAY);
x = 90;
y = 35;
drawFrame = true;
onClick = null;
setup();
}
/**
* Constructs a new grey Button with the given title and a default size
* of at least 90 times 35 pixels. It will switch to the given world when clicked.
*
* @param title The text printed onto the button
* @param onClick The world to switch to when clicked
*/
public Button(String title, World onClick){
inputText = new Text(title, 25, Color.BLACK, Color.LIGHT_GRAY);
x = 90;
y = 35;
drawFrame = true;
this.onClick = onClick;
setup();
}
/**
* Constructs a new grey Button with the given title and a the given width
* and height.
*
* @param x The width of the button
* @param y The height of the button
* @param title The text printed onto the button
*/
public Button(int x, int y, String title){
name = title;
inputImage = null;
this.x = x;
this.y = y;
color = Color.LIGHT_GRAY;
fontSize = 25;
drawFrame = true;
onClick = null;
setup();
}
/**
* Constructs a new grey Button with the given title and a the given width
* and height. It will switch to the given world when clicked.
*
* @param x The width of the button
* @param y The height of the button
* @param title The text printed onto the button
* @param onClick The world to switch to when clicked
*/
public Button(int x, int y, String title, World onClick){
name = title;
inputImage = null;
this.x = x;
this.y = y;
color = Color.LIGHT_GRAY;
fontSize = 25;
drawFrame = true;
this.onClick = onClick;
setup();
}
/**
* Constructs a new Button with the given title in the given size and
* the given width and height using the given background colour. Unless
* onClick is null if will switch to that world when clicked on.
*
* @param x The width of the button
* @param y The height of the button
* @param title The text printed onto the button
* @param fontSize The font size for the text on the button
* @param colour The background colour of the button
* @param onClick The world to switch to when clicked
*/
public Button(int x, int y, String title, int fontSize, Color colour, World onClick){
name = title;
inputImage = null;
this.x = x;
this.y = y;
color = colour;
this.fontSize = fontSize;
drawFrame = true;
this.onClick = onClick;
setup();
}
/**
* Constructs a new Button with the given title in the given size and
* the given width and height using the given background colour. Unless
* onClick is null if will switch to that world when clicked on.
*
* @param x The width of the button
* @param y The height of the button
* @param title The text printed onto the button
* @param fontSize The font size for the text on the button
* @param colour The background colour of the button
* @param drawFrame if a black outline should be drawed around the image
* @param onClick The world to switch to when clicked
*/
public Button(int x, int y, String title, int fontSize, Color colour, boolean drawFrame, World onClick){
name = title;
inputImage = null;
this.x = x;
this.y = y;
color = colour;
this.fontSize = fontSize;
this.drawFrame = drawFrame;
this.onClick = onClick;
setup();
}
/**
* Constructs a new Button with the given image as background and
* optional with a title and a black outline. Unless
* onClick is null if will switch to that world when clicked on.
*
* @param theImage the background image
* @param title the title written centered onto the image, or null
* @param fontSize the font size of the title. Ignore if there is no title
* @param drawFrame if a black outline should be drawed around the image
* @param onClick The world to switch to when clicked
*/
public Button(GreenfootImage theImage, String title, int fontSize, boolean drawFrame, World onClick){
name = title;
inputImage = theImage;
x = theImage.getWidth();
y = theImage.getHeight();
color = Color.LIGHT_GRAY;
this.fontSize = fontSize;
this.drawFrame = drawFrame;
this.onClick = onClick;
setup();
}
/**
* Constructs a new button from the given text. Content and size
* are updated every frame.
*
* @param text The text object the button should be based on
*/
public Button(Text text){
inputText = text;
x = y = 10;
drawFrame = true;
onClick = null;
setup();
}
/**
* Constructs a new button from the given text that is at least
* as big as inputed. Content and size are updated every frame.
*
* @param text The text object the button should be based on
* @param minX The minimum width of the button
* @param minY The minimum height of the button
*/
public Button(Text text, int minX, int minY){
inputText = text;
if(minX < inputText.getImage().getWidth()) minX = inputText.getImage().getWidth();
if(minY < inputText.getImage().getHeight()) minY = inputText.getImage().getHeight();
x = minX;
y = minY;
drawFrame = true;
onClick = null;
setup();
}
/**
* Constructs a new button from the given text that is at least
* as big as inputed. It will switch to the given world unless it
* is null. Content and size are updated every frame.
*
* @param text The text object the button should be based on
* @param minX The minimum width of the button
* @param minY The minimum height of the button
* @param drawFrame If a black outline should be drawed around the button
* @param onClick The world to switch to when clicked
*/
public Button(Text text, int minX, int minY, boolean drawFrame, World onClick){
inputText = text;
x = minX;
y = minY;
this.drawFrame = drawFrame;
this.onClick = onClick;
setup();
}
/**
* Creates the image, hoveredImage and clickedImage for the button using
* the latest settings and aplies the matching image to the button.
*/
public void createImages(){
if(inputText != null){
name = inputText.getContent();
int x = this.x;
int y = this.y;
if(x < inputText.getImage().getWidth() + 6) x = inputText.getImage().getWidth() + 6;
if(y < inputText.getImage().getHeight()) y = inputText.getImage().getHeight();
image = new GreenfootImage(x, y);
if(inputText.getBackgroundColor() != null){
image.setColor(inputText.getBackgroundColor());
if(x > inputText.getImage().getWidth()){
int delta = x - inputText.getImage().getWidth();
image.fillRect(0, 0, delta / 2, image.getHeight());
image.fillRect(image.getWidth() - (delta - delta / 2), 0, delta / 2, image.getHeight());
}
if(y > inputText.getImage().getHeight()){
int deltaX = x - inputText.getImage().getWidth();
int deltaY = y - inputText.getImage().getHeight();
image.fillRect(deltaX / 2, 0, inputText.getImage().getWidth(), deltaY / 2);
image.fillRect(deltaX / 2, image.getHeight() - (deltaY - deltaY / 2), inputText.getImage().getWidth(), deltaY / 2);
}
}
image.drawImage(inputText.getImage(), (x - inputText.getImage().getWidth()) / 2, (y - inputText.getImage().getHeight()) / 2);
}
else{
if(inputImage != null){
image = new GreenfootImage(inputImage);
}
else{
image = new GreenfootImage(x, y);
image.setColor(color);
image.fill();
}
if(name!=null){
GreenfootImage temp1 = new GreenfootImage(name, fontSize, Color.BLACK, null);
image.drawImage(temp1, image.getWidth()/2-temp1.getWidth()/2, image.getHeight()/2-temp1.getHeight()/2);
}
}
image.setColor(Color.BLACK);
if(drawFrame)image.drawRect(0,0,image.getWidth()-1,image.getHeight()-1);
hoveredImage = new GreenfootImage(image);
hoveredImage.scale((int)(image.getWidth() * 1.1), (int)(image.getHeight() * 1.1));
GreenfootImage temp = new GreenfootImage(image.getWidth(), image.getHeight());
temp.setColor(Color.BLACK);
temp.fill();
temp.setTransparency(80);
clickedImage = new GreenfootImage(image);
clickedImage.drawImage(temp, 0, 0);
if(touched){
if(pressed) setImage(clickedImage);
else setImage(hoveredImage);
}
else setImage(image);
}
/**
* Applies default settings to the button
*/
private void setup(){
createImages();
touched = false;
pressed = false;
clickCount = 0;
pressTime = 0;
}
/**
* Analyses the mouse interactions each frame. Do not remove
* any code here! Use the run method instead!
*/
public void act()
{
touched = touching();
if(touched&&Greenfoot.mousePressed(null)){
pressed=true;
if(enabled) onPress();
}
else if(Greenfoot.mouseClicked(null)){
if(pressed&&touched){
clickCount++;
if(enabled) onClick();
if(enabled) onRelease();
pressed = false;
if(onClick!=null)Greenfoot.setWorld(onClick);
}
else if(pressed){
pressed = false;
if(enabled) onRelease();
}
}
if(inputText != null) createImages();
else if(touched){
if(pressed) setImage(clickedImage);
else setImage(hoveredImage);
}
else setImage(image);
if(pressed)pressTime++;
run();
}
/**
* Simulates clicking the button for the given time. The button will not
* actually animate the click, however, click count and press time will
* be updated and {@code onPress()}, {@code onClick()} and {@code onRelease()}
* will be executed.
*
* @param time The number of frames the button should (virtually) be pressed down.
*/
public void click(int time){
if(enabled) onPress();
pressTime += time >= 0 ? time : 1;
clickCount++;
if(enabled) onClick();
if(enabled) onRelease();
}
/**
* Simulates clicking the button for a single frame. The button will not
* actually animate the click, however, click count and press time will
* be updated and {@code onPress()}, {@code onClick()} and {@code onRelease()}
* will be executed.
*/
public void click(){
click(1);
}
/**
* Checkes weather the mouse is touching the button right now. Is rather
* cpu-intense so don't use it more than once per frame.
*
* @return Wheather the mouse is touching the button
*/
public boolean touching(){
try{
MouseInfo mouse = Greenfoot.getMouseInfo();
Sensor sensor = new Sensor();
getWorld().addObject(sensor, mouse.getX(), mouse.getY());
return sensor.touching(this);
//return ActorVisitor.containsPoint(this, mouse.getX(), mouse.getY());
}catch(Exception e){}
return false;
}
/**
* A simple class to test if it is touching the button.
*/
class Sensor extends Actor{
/**
* Creates a new sensor with an transparent one pixel big image.
*/
public Sensor(){
setImage(new GreenfootImage(1, 1));
}
/**
* Weather the sensor is touching the given button.
*
* @param button The button to check for
* @return Weather the given button is touched
*/
public boolean touching(Button button){
boolean out = getOneIntersectingObject(Button.class) == button;
getWorld().removeObject(this);
return out;
}
}
/**
* Executed once per frame as a replacement of the act method. Override
* it to use.
*/
public void run(){}
/**
* Executed whenever the mouse is first pressed onto the button
*/
public void onPress(){}
/**
* Executed whenever the mouse is released after holding down the button,
* also if is is not on it no more.
*/
public void onRelease(){}
/**
* Executed ehenever the mouse is released after holding down the button,
* but only when the mouse touched the button while being released.
*/
public void onClick(){}
/**
* Resets the stats of the button.
*/
public void reset(){
clickCount = 0;
pressTime = 0;
}
/**
* Sets the count of the button to the given value.
*
* @param n The new value
*/
public void setCount(int n){
clickCount = n;
}
/**
* Returns the number of times the button was clicked since the last
* reset.
*
* The Button is considered to be clicked when the mouse was pressed on
* the button and then released on it again. It still counts as a click
* if the mouse was dragged outside of the button while being pressed
* but is released on the button.
*
* @return The number of clicks
*/
public int getClickCount(){
return clickCount;
}
/**
* Returns true if the button was clicked since the last reset.
*
* The Button is considered to be clicked when the mouse was pressed on
* the button and then released on it again. It still counts as a click
* if the mouse was dragged outside of the button while being pressed
* but is released on the button.
*
* @return A boolean if the button was ever clicked at least one time
*/
public boolean clicked(){
return clickCount>0;
}
/**
* Returns if the button is being pressed right now.
*
* The Button is considered to be pressed when the mouse was pressed on
* the buttonand since than holded down. It still counts as pressed
* if the mouse was dragged outside of the button while being pressed.
*
* @return If the button is pressed right now
*/
public boolean pressed(){
return pressed;
}
/**
* Returns if the button is being touched by the mouse right now.
*
* @return If the button is touched right now
*/
public boolean touched(){
return touched;
}
/**
* Returns the number of frames the button was pressed since the last
* reset.
*
* It is considered as pressed if the boolean pressed() is true.
*
* @return the time the button was pressed
*/
public int getTimePressed(){
return pressTime;
}
/**
* Returns the title written onto the button.
*
* @return The buttons text
*/
public String getTitle(){
return name;
}
/**
* Returns the text object used in this button
*
* @return The text object of this button
*/
public Text getText(){
return inputText;
}
/**
* Override the title of the button.
*
* @param name The new title
*/
public void setTitle(String name){
this.name = name;
createImages();
}
/**
* Sets the image of the button to the given one. If image is null,
* the latest color will be used.
*
* @param image The new image
*/
public void setBackgroundImage(GreenfootImage image){
inputImage = image;
createImages();
}
/**
* Sets the background of the button to the given color.
*
* @param color the new color
*/
public void setColor(Color color){
inputImage = null;
this.color = color;
createImages();
}
/**
* Sets the images displayed to custom ones to allow more customisation.
*
* @param image The image to be showm normally
* @param hoveredImage The image to be showm when the mouse hovers over the button
* @param clickedImage The image to be shown when the button is pressed down
*/
public void setCustomImages(GreenfootImage image, GreenfootImage hoveredImage, GreenfootImage clickedImage){
this.image = image;
this.hoveredImage = hoveredImage;
this.clickedImage = clickedImage;
if(touched){
if(pressed) setImage(this.clickedImage);
else setImage(this.hoveredImage);
}
else setImage(this.image);
}
/**
* Sets the text object the button is based on to the given one.
*
* @param text The text object to set to
*/
public void setText(Text text){
inputText = text;
createImages();
}
/**
* Sets weather the outline should be drawn or not
*
* @param drawFrame Weather the outline should be drawn
*/
public void drawFrame(boolean drawFrame){
this.drawFrame = drawFrame;
createImages();
}
/**
* Sets the world to switch to on click to the given one. If onClick is
* null, the world will not be swiched.
*
* @param onClick The world to switch to, or null
*/
public void setNextWorld(World onClick){
this.onClick = onClick;
}
}
import greenfoot.*;
/**
* The text class is used to store and display some text and to input
* and modify the text in other objects.
*
* @author RcCookie
* @version 1.0
*/
public class Text extends Actor{
/**
* The text of the text.
*/
protected String content = "";
/**
* The fontSize of the dext drawn.
*/
private int fontSize = 20;
/**
* The color of the letters of the text.
*/
private Color color = Color.BLACK;
/**
* The color of the background image of the text.
*/
private Color background = null;
/**
* Constructs an empty text with default settings.
*/
public Text(){}
/**
* Constructs a new text with the given content.
*
* @param content The text of the text
*/
public Text(String content){
this.content = content;
update();
}
/**
* Constructs a new text with the given content written in the given
* font size.
*
* @param content The text of the text
* @param fontSize The font size of the text
*/
public Text(String content, int fontSize){
this.content = content;
this.fontSize = fontSize;
update();
}
/**
* Constructs a new text with the given content written in the given
* color.
*
* @param content The text of the text
* @param color The color of the text
*/
public Text(String content, Color color){
this.content = content;
this.color = color;
update();
}
/**
* Constructs a new text with the given content written in the given
* color and font size.
*
* @param content The text of the text
* @param fontSize The font size of the text
* @param color The color of the text
*/
public Text(String content, int fontSize, Color color){
this.content = content;
this.fontSize = fontSize;
this.color = color;
update();
}
/**
* Constructs a new text with the given content written in the given
* color and font size. The background will be filled with the given
* backgound color.
*
* @param content The text of the text
* @param fontSize The font size of the text
* @param color The color of the text
* @param background The color of the background
*/
public Text(String content, int fontSize, Color color, Color background){
this.content = content;
this.fontSize = fontSize;
this.color = color;
this.background = background;
update();
}
/**
* Updates the image of the text according to the current settings.
*/
protected void update(){
setImage(new GreenfootImage(content, fontSize, color, background));
}
/**
* Sets the text of the text to the given string.
*
* @param content The new text
*/
public void setContent(String content){
this.content = content;
update();
}
/**
* Sets the font size of the text (also of the already written stuff) to
* the given one.
*
* @param fontSize The new font size
*/
public void setFontSize(int fontSize){
this.fontSize = fontSize;
update();
}
/**
* Sets the color of the text (also of the already written stuff) to the
* given one.
*
* @param color The new text color
*/
public void setColor(Color color){
this.color = color;
update();
}
/**
* Sets the background color to the given one.
*
* @param background The new background color
*/
public void setBackgroundColor(Color background){
this.background = background;
update();
}
/**
* returns the text of the text.
*
* @return The text of the text
*/
public String getContent(){
return content;
}
/**
* returns the color of the text.
*
* @return The color of the text
*/
public Color getColor(){
return color;
}
/**
* Returns the background color of the text.
*
* @return The background color of the text
*/
public Color getBackgroundColor(){
return background;
}
/**
* Returns the font size of the text.
*
* @return The font size of the text
*/
public int getFontSize(){
return fontSize;
}
}
/**
* The fps display shows the current fps in one of two modes: either
* in realtime (based on only the last frame) or the average (of the
* last second) (as calculated in packages.tools.Time). When clicked
* on it it switches modes.
*
* @see packages.tools.Time
* @author RcCookie
* @version 1.0
*/
public class FpsDisplay extends Button{
/**
* The time object that actually calculats the fps.
*/
protected Time time;
/**
* Weather the output is the average(=true) or realtime (=false).
*/
boolean stableMode;
/**
* Constructs a new fps display in stable mode.
*/
public FpsDisplay(){
super(new Text("FPS: --", null), 70, 20);
time = new Time();
stableMode = true;
}
/**
* Constructs a new fps display in the given mode.
*
* @param stableMode Weather the display shows stable fps by default
*/
public FpsDisplay(boolean stableMode){
super(new Text("FPS: --", null), 70, 20);
time = new Time();
this.stableMode = stableMode;
}
/**
* Runs the time object and updates the text output.
*/
@Override
public void run(){
time.act();
getText().setContent("FPS: " + currentModeFps());
}
/**
* Returns the current realtime fps.
*
* @return The current realtime fps
*/
public int fps(){
return time.fps();
}
/**
* Returns the current average fps.
*
* @return The current average fps
*/
public int stableFps(){
return time.stableFps();
}
/**
* Returns the fps in the current mode.
* @return The current fps in the current mode
*/
public int currentModeFps(){
if(stableMode) return stableFps();
else return fps();
}
/**
* Returns the duration of the last frame as a fraction of a second.
*
* @return The duration of the last frame as a fraction of a second
* @see packages.tools.Time.deltaTime()
*/
public double deltaTime(){
return time.deltaTime();
}
/**
* Switches modes. Called by the button class whenever the button was clicked.
*/
@Override
public void onClick(){
stableMode = !stableMode;
}
}
long lastMillis;
double deltaTime;
public void act(){
long currentMillis = System.currentTimeMillis();
deltaTime = (currentMillis - lastMillis) / 1000.0;
lastMillis = currentMillis;
}
public int fps(){
if(deltaTime == 0) return 2000;
return (int)(1 / deltaTime);
}
long currentMillis = System.currentTimeMillis();
deltaTime = (currentMillis - lastMillis) / 1000.0;
lastMillis = currentMillis;
if(deltaTime == 0) return 2000;
return (int)(1 / deltaTime);
double speedPerSecond = 20;
public void act() {
move(speedPerSecond * deltaTime);
}