how create a simple object animation in processing - processing

QUESTION
I'm trying to create a simple object "car" animation in Processing.
My code is:
Car car;
void setup(){
size(800, 600);
background(#818B95);
frameRate(30);
car = new Car(10,10);
}
void draw(){
//refresh background
background(#818B95);
print("-");
car.drawCar();
}
void mouseClicked() {
car.run();
}
public class Car implements Runnable{
private int pos_x, pos_y;
public Car(int pos_x, int pos_y){
this.pos_x = pos_x;
this.pos_y = pos_y;
}
public void drawCar(){
rect(pos_x,pos_y,10,10);
}
public void run(){
while(true){
pos_x += 10;
print("*");
delay(200);
}
}
}
I'm expecting to see the car/rectangle move right when I click the mouse button, but nothing happens.
I've added the two print in order to see if the draw method and my car.run are executed in parallel showing some * and - printed alternately.
What I see is a sequence of - until I click and then only * are printed.
Is it possible that starting a new object thread will stop the main draw cycle?
SOLUTION
This is just a variant of the suggested solution (by Mady Daby) without using threads.
Car car;
void setup(){
size(800, 600);
background(#818B95);
frameRate(30);
car = new Car(10,10);
}
void draw(){
//refresh background
background(#818B95);
print("-");
car.drawCar();
}
void mouseClicked() {
car.moving = true;
}
public class Car{
private int pos_x, pos_y;
boolean moving = false;
public Car(int pos_x, int pos_y){
this.pos_x = pos_x;
this.pos_y = pos_y;
}
public void drawCar(){
rect(pos_x,pos_y,10,10);
//animation
if(moving){
pos_x += 10;
print("*");
}
}
}

You could make the car move right by just introducing a boolean variable to track whether the car is supposed to be moving right moving and then increment pos_x if moving is true. You can also use the clicks to toggle between moving and not moving.
Car car;
void setup() {
size(800, 600);
background(#818B95);
frameRate(30);
car = new Car(10, 10);
}
void draw() {
//refresh background
background(#818B95);
print("-");
car.drawCar();
}
void mouseClicked() {
car.toggleMoving();
}
public class Car implements Runnable {
private int pos_x, pos_y;
private boolean moving = false;
public Car(int pos_x, int pos_y) {
this.pos_x = pos_x;
this.pos_y = pos_y;
}
public void toggleMoving() {
moving = !moving;
}
public void drawCar() {
if (moving) {
this.run();
}
rect(pos_x, pos_y, 10, 10);
}
public void run() {
pos_x += 10;
print("*");
delay(200);
}
}

Related

Trying to pick up a battery. "NullReferenceException" error

Full error is "NullReferenceException: Object reference not set to an instance of an object
battery.OnTriggerStay (UnityEngine.Collider other) (at Assets/battery.cs:32)"
heres my screen in case its something i didnt do in the inspector: http://imgur.com/a/wWSGJ
here is my code (not sure if you need my flashlight code too):
public class battery : MonoBehaviour {
public float a;
public float b;
public float c;
public float d;
public bool showText;
public int Bat;
public GameObject Flight;
public int mainBat;
public bool safeRemove;
void Start()
{
showText = false;
}
void OnTriggerStay(Collider other)
{
showText = true;
if (!safeRemove)
{
if (Input.GetKeyUp (KeyCode.E))
{
mainBat = Flight.GetComponent<flashlight> ().batLevel;
Bat = 20;
Flight.GetComponent<flashlight> ().batLevel = Bat +- mainBat;
safeRemove = true;
if (safeRemove)
{
Destroy (this.gameObject);
}
}
}
}
void OnTriggerExit(Collider other)
{
showText = false;
}
void OnGUI()
{
if (showText)
{
GUI.Box(new Rect(Screen.width/ 2.66f, Screen.height/ 3.48f, Screen.width/ 3.78f, Screen.height/ 16.1f), "Press 'E' to pick up");
}
}
}

call method from a method within the same class

I have a platformer class that creates a window and spawns platforms and a "character". It uses another class platform to make platforms. The character is supposed to jump up and land on the platforms. I use the getBounds and getTopY functions for collision detection but they only work for the first platform. How can i get them to work for multiple platforms?
public class Platformer extends JPanel {
Platform platform = new Platform(this);
Character character = new Character(this);
public Platformer() {
addKeyListener(new KeyListener() {
#Override
public void keyTyped(KeyEvent e) {
character.keyTyped(e);
}
#Override
public void keyReleased(KeyEvent e) {
character.keyReleased(e);
}
#Override
public void keyPressed(KeyEvent e) {
character.keyPressed(e);
}
});
setFocusable(true);
}
#Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
platform.Location(150,200);
platform.paint(g2d);
platform.Location(200,120);
platform.paint(g2d);
character.paint(g2d);
}
private void move(){
character.move();
}
public static void main(String args[]){
JFrame frame = new JFrame("Mini Tennis");
//create new game
Platformer platformer = new Platformer();
//add game
frame.add(platformer);
//size
frame.setSize(400, 400);
frame.setVisible(true);
//set close condition
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
while (true) {
platformer.move();
platformer.repaint();
try {
Thread.sleep(10);//sleep for 10 sec
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
}
public class Platform {
private static final int Height = 10;
private static final int Width = 60;
int x;
int Y;
private Platformer platformer;
public Platform(Platformer platformer) {
this.platformer = platformer;
}
public void Location(int xin, int yin) {
x = xin;
Y = yin;
}
public void paint(Graphics2D g) {
g.fillRect(x, Y, Width, Height);
}
public Rectangle getBounds() {
return new Rectangle(x, Y, Width, Height);
}
public int getTopPlat() {
return Y;
}
}
Actually you have only one platform. And you draw this platform twice in different places (paint function in Platformer):
platform.Location(150,200);
platform.paint(g2d);
platform.Location(200,120);
platform.paint(g2d);
Therefore I suppose you handle only one platform (with coordinates 200 and 120). You must keep all of your platforms and handle each of them separately.

JUnit testing GUI class

I've looked over the stackoverflow and the internet and I couldn't find a clear answer that helped me.
I have an assignment and it includes the following class, which is a GUI. I have Junit tested the other classes but for this I didn't know how.
import java.awt.*;
public class CruiseDisplay extends Canvas {
private int recorded = 0; //recorded speed
private boolean cruiseOn = false; //cruise control state
private final static int botY = 200;
private Font small = new Font("Helvetica",Font.BOLD,14);
private Font big = new Font("Helvetica",Font.BOLD,18);
public CruiseDisplay() {
super();
setSize(150,260);
}
Image offscreen;
Dimension offscreensize;
Graphics offgraphics;
public void backdrop() {
Dimension d = getSize();
if ((offscreen == null) || (d.width != offscreensize.width)
|| (d.height != offscreensize.height)) {
offscreen = createImage(d.width, d.height);
offscreensize = d;
offgraphics = offscreen.getGraphics();
offgraphics.setFont(small);
}
offgraphics.setColor(Color.black);
offgraphics.fillRect(0, 0, getSize().width, getSize().height);
offgraphics.setColor(Color.white);
offgraphics.drawRect(5,10,getSize().width-15,getSize().height-40);
offgraphics.setColor(Color.blue);
offgraphics.fillRect(6,11,getSize().width-17,getSize().height-42);
}
public void paint(Graphics g) {
update(g);
}
public void update(Graphics g) {
backdrop();
// display recorded speed
offgraphics.setColor(Color.white);
offgraphics.setFont(big);
offgraphics.drawString("Cruise Control",10,35);
offgraphics.setFont(small);
drawRecorded(offgraphics,20,80,recorded);
if (cruiseOn)
offgraphics.drawString("Enabled",20,botY+15);
else
offgraphics.drawString("Disabled",20,botY+15);
if (cruiseOn)
offgraphics.setColor(Color.green);
else
offgraphics.setColor(Color.red);
offgraphics.fillArc(90,botY,20,20,0,360);
g.drawImage(offscreen, 0, 0, null);
}
public void drawRecorded(Graphics g, int x, int y, int speed) {
g.drawString("Cruise Speed",x,y+10);
g.drawRect(x+20,y+20,50,20);
g.setFont(big);
g.drawString(String.valueOf(speed+20),x+30,y+37);
g.setFont(small);
}
public void enabled() {
cruiseOn = true;
repaint();
}
public void disabled() {
cruiseOn = false;
repaint();
}
public void record(int speed) {
recorded=speed;
repaint();
}
}
Can somebody help me please?

Simple animation through use of paintComponent

I'm trying to make a small square move across the top of the panel. I'm not worried about the seamlessness of the animation or flicker or anything like that. It appears that in the while-loop, repaint() isn't repeatedly calling the paintComponent. Thoughts?
public class NodeMove extends JFrame {
boolean running = true;
public NodeMove() {
widgetNode panel = new widgetNode();
add(panel);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
setVisible(true);
Runnable node = new widgetNode();
Thread thread1 = new Thread(node);
thread1.start();
}
class widgetNode extends JPanel implements Runnable {
private static final long serialVersionUID = 1L;
private int x = 30;
private int y = 30;
public widgetNode() {
}
public void run(){
while(running){
nodeUpdate();
repaint();
try {
Thread.sleep(500);
} catch (InterruptedException e) {}
}
}
public void nodeUpdate(){
x += 4;
}
protected void paintComponent(Graphics g) {
super.paintComponents(g);
g.drawRect(x, y, 30, 30);
}
}
public static void main(String[] args) {
NodeMove frame = new NodeMove();
for(int i = 0; i < 50; i++){
frame.repaint();
}
}
}

LibGDX Listener in Actor in Stage doesn't work

The input listener doesn't fire in the Actor even if the Actor was already added to the stage and the input processor was already set to the stage. What could be the problem here?
Inside GameScreen (extends Screen) class:
public GameScreen(Game game) {
this.game = game;
stage = new Stage(800, 480, true);
number = new Number();
stage.addActor(number);
}
public void show() {
Gdx.input.setInputProcessor(stage);
}
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage.act(delta);
stage.draw();
}
Constructor of Number:
public Number() {
addListener(new InputListener() {
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
System.out.println("touch down");
return true;
}
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
System.out.println("touch up");
}
public boolean mouseMoved(InputEvent event, float x, float y) {
System.out.println("mouse moved");
return true;
}
});
}

Resources