Java remove objects - java

I have a world that is the GameScreen (20ish objects) which lays all objects as intended. However, when I get GameOver I want to be a blank canvas with just the background and some new objects(a couple objects), but all the existing objects from GameScreen carry over and I cant figure out how to stop it or delete them on the GameOver screen
public class GameScreen extends World
{
public GameScreen()
{
super(600, 400, 1);
prepare();
}
private void prepare()
{
addObjects.......
}
}
public class GameLost extends GameScreen
{
public GameLost()
{
removeObjects(GameScreen);
prepare();
}
private void prepare()
{
addObjects...
}

I'm pretty sure you don't want GameLost extends GameScreen.
Do
class GameLost {
private Background bg;
public void paint() {
paint(bg);
}
}
class GameScreen {
private Background bg;
private List<GameObjects>...
public void paint() {
paint(bg);
gameObjects.forEach(go -> paint(go));
}
}
which makes it easy to move over the background from game screen to game over screen, without the game objects.

Related

How to update custom Javafx with data in the most effiecient way

I have a working system where one Custom component can be updated by user clicking button or by timer. But I'm not sure this is the most efficient way and if it is thread safe.
Is there any better way to solve this?
I have component based on Canvas: MyBox
One class that holds the data: MyColorBox
Listening interface: MyColorBoxListener
From the main class:
pane.getChildren().add( new MyBox( myboxdata));
The data can be updated by timers or user clicking by calling:
myboxdata.update();
The user can choice Play, Stop or Next. Play and Stop will control the timer. If the timer is stoppe it will be possible to click Next.
My data class:
import javafx.scene.paint.Color;
public class MyColorBox {
Color color[] = new Color[]{Color.BLUE, Color.RED, Color.GREEN};
Color color_second;
int counter = 0;
MyColorBoxListener listener;
public MyColorBox(MyColorBoxListener listener) {
this.listener = listener;
}
public MyColorBox() {
setListener(null);
}
public void setListener(MyColorBoxListener listener) {
this.listener = listener;
}
public void update() {
if(++counter>=color.length) counter=0;
if(listener!=null)
listener.updated();
}
public Color getCurrentColor() {
return color[counter];
}
}
Listener interface:
public interface BouncingBoxListener {
public void updated();
}
My Component:
public class MyBox extends Canvas implements MyColorBoxListener {
MyColorBox box;
public MyBox( MyColorBox box) {
super(100,100);
this.box = box;
box.setListener(this);
draw();
}
public void draw() {
GraphicsContext gc = this.getGraphicsContext2D();
gc.setStroke( box.getCurrentColor());
gc.setLineWidth(2);
gc.strokeRect(0,0, this.getWidth(), this.getHeight());
}
#Override
public void updated() {
draw();
}
}

Calling different classes in Libgdx Android

I need help calling different classes.
I want to create a class for, a menu, the game, an option menu, credits etc.
I just need to know how to call a different class
//Ok i have edited my post, hope i provided enough information.
game class
//Add Screens
private MenuScreen menuScreen;
public final static int MENU=0;
public void changeScreens(int screen){
switch(screen){
case MENU:
if(menuScreen==null){
menuScreen=new MenuScreen(this);
}
}
}
//Render (When you click on the button)
if(drawplay) {
menuScreen=new MenuScreen(this);
setScreen( menuScreen);
//MenuScreen Class
ublic class MenuScreen implements Screen {
private rugby parent;
public MenuScreen(rugby rugby){
parent = rugby;
}
#Override
public void render(float delta) {
Gdx.app.log( "Play","Play" );
}

repaint method of JComponent does not work

I am working on a 2D game and now I have got a problem.
My last goal of the following codes is moving an object in a full screen window.
This is some part of my code:
public class mainTest {
public static void main(String args[])
{
DisplayMode displayMode=new DisplayMode(1024,768,64,75);
GameScreen gameScreen=new GameScreen(displayMode,"background.jpg");
Sprite s=new Sprite("some.png");
gameScreen.addSprite(s);
gameScreen.start();
}
}
I have used two kinds of start() method. One causes NullPointerException and I have commented it and the other does not work:
public class GameScreen extends JComponent{
private Screen screen;
private ArrayList<Sprite> sprites=new ArrayList<Sprite>();
private Image background;
//...(constructor)
public void addSprite(Sprite sprite)
{
sprites.add(sprite);
}
// public void draw()
// {
// Graphics g=screen.getWindow().getGraphics();
// g.drawImage(background,0,0,null);
// for(Sprite i:sprites)
// {
// g.drawImage(i.getImage(),(int)i.getX(),(int)i.getY(),null);
// }
// g.dispose();
// }
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(background,0,0,null);
for(Sprite i:sprites)
{
g.drawImage(i.getImage(),(int)i.getX(),(int)i.getY(),null);
}
g.dispose();
}
public void start()
{
//draw();
repaint();
long currentTime=System.currentTimeMillis();
while(true)
{
long elapsedTime=System.currentTimeMillis()-currentTime;
for(Sprite i:sprites)
{
i.update(elapsedTime);
}
currentTime+=elapsedTime;
//draw();
repaint();
}
}
}
And this is my Sprite class's update() method:
public void update(long elapsedTime)
{
x+=vx*elapsedTime;
y+=vy*elapsedTime;
}
While vx,vy shows velocity.
As you can see, I firstly used method getGraphicsI() to get a Graphics object and called its drawImage() method in my draw() method, but as I said I got NullPointerException.So,
1.What does it mean to getGraphics() of a window,and
2.Why I get Exception?
3.Why should I dispose() Graphics object? Or a better question:Should I dispose() it?
Then I used another way:Making my class extend JComponent, overriding paintComponent() and using repaint() in my start() method.So,
4.Is it suitable to make this class extend JComponent?Does it mean GameScreen class is a place to draw things on?
5.Why my code does not work? It enters the infinite loop, and does not exit.
But why I don't see anything on the screen?
UPDATE:
for more information, this is my Screen class:
public class Screen {
private DisplayMode displayMode;
private JFrame window;
private GraphicsDevice device;
public Screen (DisplayMode dm)
{
window=new JFrame();
device=GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
displayMode=dm;
}
public void setFullScreen()
{
device.setFullScreenWindow(window);
device.setDisplayMode(displayMode);
window.setResizable(false);
window.setUndecorated(true);
window.setBackground(Color.black);
window.setForeground(Color.white);
}
public Window getWindow()
{
return device.getFullScreenWindow();
}
}
UPDATE 2:
Stack trace for NullPointerException:
Exception in thread "main" java.lang.NullPointerException at
GameScreen.draw(GameScreen.java:25) at
GameScreen.start(GameScreen.java:46) at
mainTest.main(mainTest.java:11)
where line 25 of GameScreen class belongs to getGraphics() method.
NOTE:
My problem is not caused by infinite loop, because I changed the program so that the loop ended after two seconds; but my problem did not solve and I saw nothing on the screen.

How to create interactive graphic with Vaadin?

I want to develop a simple interactive game (like arcanoid). I already have implemented a menu and different views, and now I need to develop the actually game (draw flying ball, some movable platform) and I don't know how to do this. I need something like canvas where I can draw my graphic each frame.
I have tryed to implement this with Canvas and Timer. But it doesn't want update graphic itself, but only when user clicks on screen or similar. Also I saw com.google.gwt.canvas.client.Canvas, but I cannot understand how to use it in Vaadin application.
So my question is next: is it possible to draw some graphic each frame with high framerate in any way? If possible, how can I do this?
P.S. I use the Vaadin 7.3.3.
ADDED LATER:
Here is a link to my educational project with implementation below.
I'll be glad if it helps someone.
ORIGINAL ANSWER:
Well... I found the solution myself. First of all, I have created my own widget - "client side" component (according to this article).
Client side part:
public class GWTMyCanvasWidget extends Composite {
public static final String CLASSNAME = "mycomponent";
private static final int FRAMERATE = 30;
public GWTMyCanvasWidget() {
canvas = Canvas.createIfSupported();
initWidget(canvas);
setStyleName(CLASSNAME);
}
Connector:
#Connect(MyCanvas.class)
public class MyCanvasConnector extends AbstractComponentConnector {
#Override
public Widget getWidget() {
return (GWTMyCanvasWidget) super.getWidget();
}
#Override
protected Widget createWidget() {
return GWT.create(GWTMyCanvasWidget.class);
}
}
Server side part:
public class MyCanvas extends AbstractComponent {
#Override
public MyCanvasState getState() {
return (MyCanvasState) super.getState();
}
}
Then I just add MyCanvas component on my View:
private void createCanvas() {
MyCanvas canvas = new MyCanvas();
addComponent(canvas);
canvas.setSizeFull();
}
And now I can draw anything on Canvas (on client side in GWTMyCanvasWidget) with great performance =). Example:
public class GWTMyCanvasWidget extends Composite {
public static final String CLASSNAME = "mycomponent";
private static final int FRAMERATE = 30;
private Canvas canvas;
private Platform platform;
private int textX;
public GWTMyCanvasWidget() {
canvas = Canvas.createIfSupported();
canvas.addMouseMoveHandler(new MouseMoveHandler() {
#Override
public void onMouseMove(MouseMoveEvent event) {
if (platform != null) {
platform.setCenterX(event.getX());
}
}
});
initWidget(canvas);
Window.addResizeHandler(new ResizeHandler() {
#Override
public void onResize(ResizeEvent resizeEvent) {
resizeCanvas(resizeEvent.getWidth(), resizeEvent.getHeight());
}
});
initGameTimer();
resizeCanvas(Window.getClientWidth(), Window.getClientHeight());
setStyleName(CLASSNAME);
platform = createPlatform();
}
private void resizeCanvas(int width, int height) {
canvas.setWidth(width + "px");
canvas.setCoordinateSpaceWidth(width);
canvas.setHeight(height + "px");
canvas.setCoordinateSpaceHeight(height);
}
private void initGameTimer() {
Timer timer = new Timer() {
#Override
public void run() {
drawCanvas();
}
};
timer.scheduleRepeating(1000 / FRAMERATE);
}
private void drawCanvas() {
canvas.getContext2d().clearRect(0, 0, canvas.getCoordinateSpaceWidth(), canvas.getCoordinateSpaceHeight());
drawPlatform();
}
private Platform createPlatform() {
Platform platform = new Platform();
platform.setY(Window.getClientHeight());
return platform;
}
private void drawPlatform() {
canvas.getContext2d().fillRect(platform.getCenterX() - platform.getWidth() / 2, platform.getY() - 100, platform.getWidth(), platform.getHeight());
}
}

JME: How to get the complete screen in WHITE without buttons, etc etc

Please have a look at the following code
First, Please note I am a 100% newbie to Java Mobile.
In here, I am making the light on and vibrate on when user click the button. However, I really wanted to create a SOS application which turn the whole screen into white, and go to black, like that, in the thread. I guess I didn't achieve that by this app because even the lights are on, the buttons are still there. I tried to turn the "Form" color to "white" but it seems like JME has no "Color" class.
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class Midlet extends MIDlet{
private Form f;
private Display d;
private Command start,stop;
private Thread t;
public Midlet()
{
t = new Thread(new TurnLightOn());
}
public void startApp()
{
f = new Form("Back Light On");
d = Display.getDisplay(this);
d.setCurrent(f);
start = new Command("Turn On",Command.OK,0);
stop = new Command("Turn Off",Command.OK,1);
f.addCommand(start);
f.setCommandListener(new Action());
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional)
{
this.notifyDestroyed();
}
private class Action implements CommandListener
{
public void commandAction(Command c, Displayable dis)
{
f.append("Light is Turnning On");
t.start();
}
}
private class ActionOff implements CommandListener
{
public void commandAction(Command c, Displayable dis)
{
}
}
private class TurnLightOn implements Runnable
{
public void run()
{
f.append("Working");
for(int i=0;i<100;i++)
{
try
{
d.flashBacklight(200);
d.vibrate(200);
Thread.sleep(1000);
}
catch (InterruptedException ex)
{
ex.printStackTrace();
}
}
}
}
}
Use the javax.microedition.lcdui.Canvas instead of Form. This example can get you started
public void startApp()
{
f = new Form("Back Light On");
d = Display.getDisplay(this);
start = new Command("Turn On",Command.OK,0);
stop = new Command("Turn Off",Command.OK,1);
f.addCommand(start);
f.setCommandListener(new Action());
myCanvas = new MyCanvas();
d.setCurrent(myCanvas);
myCanvas.repaint();
}
Now create a canvas and implement paint method like this:
class MyCanvas extends Canvas {
public void paint(Graphics g) {
// create a 20x20 black square in the center
// clear the screen first
g.setColor(0xffffff);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0xffffff); // make sure it is white color
// draw the square, <b>changed to rely on instance variables</b>
<b>g.fillRect(x, y, getWidth(), getHeight());</b>
}
}

Categories

Resources