executing a forloop in a particular time - java

This is my onDraw method, which I use to create my sprites and draw them every fps =10:
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.rgb(21, 181, 195));
canvas.drawBitmap(bmp, 0, 0, null);
for (Sprite wave1 : sprites) {
wave1.onDraw(canvas);
}
}
i want to run this for loop in this code again and again ... i tried to run it with timer task but when i write this for loop inside my timer task method it say error it says i can be used only in a canvas class and also showing error on receiving the image from resource files even after trying to get resource file there it says error again saying it can be used only inside a sprite class :(
so i want a way to run this for loop here in timed interval in this same Ondraw method
: Any ideas how to do it ? try ed handler class too :

Have you tried using a Thread?
Maybe this could help you: http://obviam.net/index.php/sprite-animation-with-android/

Related

My sketch won't show anything when running in processing

I am completely new to processing, and I wanted to test it by creating a simple sketch that draws a rectangle, however, when i run the sketch, a window pops up with nothing on it. I tried filling it, putting an outline on it, and various other things, nothing happened. I don't think it's a problem with the code but a problem with the app itself, and i'm not sure how to fix it. I am using processing 3.5.4 on windows.
Code:
class Sketch {
void setup() {
size(100, 100);
}
void draw() {
rect(50, 50, 25, 25);
}
}
Expected Output: A square shows up on screen.
Output: Nothing shows up
<iframe src="https://drive.google.com/file/d/1gAQsX0hpAyH_iSbUXkyO87a8NpXscLEL/preview" width="640" height="480"></iframe>
Chiming in with the obvious (don't hurt me if I didn't get your question right):
draw() and setup() are already of Processing. In fact, those you wrote into a class won't be automatically executed, as Processing will be looking for it's own methods. That's why you get this:
Which is, incidentally, exactly the same result as if you ran an empty sketch.
To fix this, just take draw() and setup() out of the class, and Processing will then find and run them:
void setup() {
size(100, 100);
}
void draw() {
rect(50, 50, 25, 25);
}
Have fun!

Fatal signal 11 (andengine project)

I'm creating and android game powered by andengine framework the box2d extension
I'm getting "Fatal Signal 11" which seems to happen out of nowhere (I guess you can call it randomly)
there no clues (at least, I don't aware to any clues to help me solve this problem)
my guesses are:
1) I'm creating game's entities using TimerTask class
2) maybe it has something to do with concurrency?
what do you think?
thanks,
socksocket
You could still use a TimerTask, you just need to be sure to call anything relating to AndEngine (creating or deleting sprites, etc) on the AndEngine update thread - you can use
runOnUpdateThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
// do your stuff in here
}
});
I had the same issue and fixed it by taking the TimerTask out of the equation.
it says touchEvent...maybe you set your density value of your FixtureDef too high....
try to set the density value <=1000 like below:
FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(1000, 0.0f, 1.0f);
Body mGoundBody=PhysicsFactory.createBoxBody(this.mPhysicsWorld, ground, BodyType.StaticBody, wallFixtureDef);
hope this information can help you...

Change an image for a second

ImageView image = (ImageView) findViewById(R.id.imageview);
image.setImageResource(drawable.image1);
SystemClock.sleep(1000);
image.setImageResource(drawable.image2);
I am trying to change the image for a second, my code above doesn't work but not sure why?
Should I be using a thread? or does anyone have any better ideas?
EDIT
To clarify on the problem:
The image being displayed as "drawable.image2"
I want "drawable.image1" to be shown for one second then change to "drawable.image2".
EDIT2:
This code is used in the onClick. When a user clicks the image it needs to change for one second
I'd recommend using a TimerTask with a Timer. You can set it up like this:
protected void showDelayedImages() {
mImageView.setImageResource(resId1);
Timer timer = new Timer();
timer.schedule( new MyTimerTask(), 1000 );
}
private class MyTimerTask extends TimerTask {
#Override
public void run() {
runOnUiThread( new Runnable() {
#Override
public void run() {
mImageView.setImageResource(resId2);
}
} );
}
}
Thread.sleep(1000);
should do it. Although there are better ways too.
Use debug mode and set breakpoints on each call to setImageResource. Step through and see if each is getting called to see if your image is changing properly.
In real world cases, you probably want to change the image based on some user action, or for example change an icon while a thread is processing, then change it bad when complete. For this example check out AsyncTask.
use R.drawable.image1 instead of drawable.image1
It looks like you're performing the "switch" in an onCreate() method, the sleep will probably just make your Activity load slower since at this stage there isn't actually anything written to the page.
To have your image change you need to perform the switch on the UI thread and you need to perform it after the image has been inflated and added to the page.
Try adding this code in an "onClick" event.

Java repaint not displaying component correctly

im developing an GUI Application, using JSWing, i load XML file, then deserialize it, then i add all created object to the JPanel.
However, until i move the window, or click on the panel, this is how they looks like
After i move the window, they look correctly, so how to fix this issue <
I looked at this link
http://docs.oracle.com/javase/6/docs/api/javax/swing/JComponent.html#paintComponent(java.awt.Graphics)
and it might be the answer, since in the constructor of the JComponent i use
setOpaque(true);
but im still not sure how to fix the issue since that part of documentation is very hard to understand (it somehow just does not make any sense to me :-D )
by the way, the painting itselfs goes something like this
for (NetObject o : objects) {
addNewObject(o);
}
and addNewObject (not whole code)
public void addNewObject(NetObject o) {
DraggableComponent object = new DraggableComponent(o, editorIndex); //Creates a draggableComponent
this.add(object);//Adds this component to main container
object.setOverbearing(true); //On click ,this panel gains lowest z-buffer
object.setSize(48, 48);
object.setLocation(o.x - 23, o.y - 23);
object.setBackground(Color.WHITE);
this.repaint(); //this = JPanel
}
and the overriden paintComponent code
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (isOpaque()) {
if (object.type == 2) { //tarnsition
g.drawImage(transition, 0, 0, this);
} else if (object.type == 1) {
boolean test = g.drawImage(place, 0, 0, this); //place
g.drawString(object.loadTokens(), 3, 27); // 1,2,3,4...
}
}
}
i tried to call this.revalidate - after FOR EACH LOOP - didnt help, the only way that works is to move somehow with the window, strangely, this problem exists only # Windows, my collegue is developing this exact same application under Linux, and he does not experience ani graphical issues.
I know that there been an awfully lot of topics like this, but i honestly was not able to figure out the solution.
Thanks for the answer,
OSiRiS
The setBackground() API mentions that "It is up to the look and feel to honor this property, some may choose to ignore it." Set the graphics context's color explicitly in paintComponent() and invoke fillRect().

Problem with Java Applet

I'm new to Java Applets. I have a problem while reloading the applet. When I resize the applet window or open some other application and then come back to the applet, the contents on the screen is redrawn. Basically my paint method is getting called. I want the contents of the paint method to be called only once. How can I accomplish this? Can anyone please help me with this?
Thanks in advance.
You are misunderstanding how paint works - you have no real control over how many times it is called. What are you doing in the paint method that makes you think you only want to do it once and why is it a problem that it gets called again?
If you're worrying about flickering, then you might like to look at painting into a buffer. Code not directly related to painting should not be in the paint method. You can put other initialization logic in the applet's start method
If you're putting initialization code into the paint method, you might think about putting it into the init or start methods instead.
The phrase you'd be looking for is applet lifecycle.
I am new to java applet design and when running it i get one problem that says "no main"
here is my code:
import java.applet.*;
import java.awt.*;
public class abhidev extends Applet {
/** Initializes the applet abhidev */
public void init() {
try {
setBackground(Color.cyan);
}
catch(Exception e){
e.printStackTrace();
}
}
public void paint(Graphics g){
try{
g.drawString("this ais an applet window",10,30);
showStatus("this is astatus window");
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
The applet class name is: abhidev.java.

Categories

Resources