How does this canvas paint method slow down my app so badly? - java

I have recently encountered the problem that the canvas.drawArc()-method on my locked SurfaceView canvas significantly slows down the loading and resuming time of my app. I am absolutely sure that the annotated line of code mainly causes this problem, but I also attached some other code that might be relevant for you.
The thread which is created in the surfaceView.surfaceCreated()-method (probably less relevant):
while(true) {
while (!running()) {
try {
sleep(50);
} catch (Exception ignore) {
}
}
try {
surfaceView.canvas = holder.lockCanvas();
synchronized(holder) {
surfaceView.draw();
}
} catch(Exception ignore) {
} finally {
try {
holder.unlockCanvasAndPost(surfaceView.canvas);
} catch(Exception ignore) {
}
}
}
And the actual draw()-method which is most likely to cause the problem:
// other draw methods, including a bitmap, ovals from float[]-Arrays, paths and text
Paint paint = new Paint();
paint.setColor(0x33d0d5c9);
paint.setStyle(Paint.Style.FILL);
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
ArrayList<float[]> particles = new ArrayList<>();
while(particles.size() < 200) {
particles.add(new float[]{x1, y1, x2, y2});
}
// completely removing the following loop prevents the problem
for(float[] p : particles) {
// draw overlapping particles on the canvas (a path would affect the look)
canvas.drawOval(new RectF(p[0], p[1], p[2], p[3]), paint);
}
I would really appreciate your suggestions on how to optimize my code to work properly.
EDIT: My solution based on lukasrozs answer
final Canvas canvas = canvas;
Thread particleThread = new Thread(new Runnable(){
#Override
public void run() {
RectF rect = new RectF();
for(float[] p : particles) {
rect.set(p[0], p[1], p[2], p[3]);
canvas.drawOval(rect, paint);
}
}
});
particleThread.start();
// some stuff
try {
particleThread.join();
} catch(Exception ignore) {
}

My best guess is that you are creating to many objects in your onDraw method. Try to move the creation of the particles outside (in the constructor, or just do it once), and only set the new x,y and size values in the draw method.
Creating new objects in the onDraw method also increases the risk of the garbage collector to be triggered, so its best to avoid it ...

You don't have to use ArrayList and float[] at all. Also, you don't have to initialize/construct new RectF every time.
// other draw methods, including a bitmap, ovals from float[]-Arrays, paths and text
Paint paint = new Paint();
paint.setColor(0x33d0d5c9);
paint.setStyle(Paint.Style.FILL);
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
RectF rect = new RectF();
for(int i = 0; i < 200) {
rect.set(x1, y1, x2, y2);
canvas.drawOval(rect, paint);
}
If you can, you should execute this method in another thread.
new Thread(new Runnable(){void run(){
// draw all the ovals on canvas
context.runOnUiThread(new Runnable(){void run(){
// this will be executed in main thread when drawing finishes
}});
}}).start();

Related

Having trouble making object move without flickering in Java

I have looked into Double Buffering and plan on implementing it eventually but as of right now I can't figure out how to use it or anything like it. I am trying to make pong so I plan on adding three objects total but for now I just want to get one object to work smoothly. I'm fairly new to graphics so I don't know entirely what I'm doing and I'm just trying to learn as I go.
Here is my code:
Pong:
public static void main(String[]args) {
JFrame window= new JFrame();
window.setTitle("Pong Game");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setPreferredSize(new Dimension(800,500));
window.pack();
window.setVisible(true);
Ball ball= new Ball();
Paddle player= new Paddle();
window.getContentPane().add(ball);
for(;;) {
ball.move();
//window.setContentPane(ball);
window.setContentPane(player);
player.move();
}
}
Paddles:
double x, y, ymove;
boolean cpu;
public Paddle() {
x=5;
y=180;
ymove=.1;
}
//passing an integer through to make the computer paddle
public Paddle(int a) {
cpu= true;
x=761;
y=180;
ymove=.1;
}
public void paint(Graphics g) {
g.setColor(Color.blue);
g.fillRect((int)x, (int)y, 18, 120);
}
public void move() {
y+=ymove;
if(y>=500-160||y<=0) {
ymove*=-1;
}
}
Ball:
double x, y, xspeed, yspeed;
public Ball() {
x=200;
y=200;
xspeed=0;
yspeed=.1;
}
public void move() {
x+=xspeed;
y+=yspeed;
if(y>=440||y<=0) {
yspeed*=-1;
}
}
public void paint(Graphics g) {
g.setColor(Color.black);
g.fillOval((int)x, (int)y, 20, 20);
}
This Answer is a very very simplified explanation! I recommend checking out this linux journal, which explores something similia.
The main issue, which causes the "flickering" is, that the draw is done "to fast".
Take your main loop:
for(;;) {
ball.move();
window.setContentPane(ball);
window.setContentPane(player);
player.move();
}
This loop updates the positions of the ball and afterwards "adds it to the content pane". While it is drawn, the next image is already added and drawn. This is causing the flickering (again, note: this is very simplified).
The simplest solution to fix the "flickering" is, to let the Thread sleep after it has drawn and "wait" until the draw is finished.
boolean running = true;
int delay = 15; // adjust the delay
while(running) {
ball.move();
player.move();
window.setContentPane(ball);
window.setContentPane(player);
try {
Thread.sleep(delay);
} catch(InterruptedException e) {
// We were interrupted while waiting
// Something "woke us up". Stop the loop.
e.printStackTrace();
running = false;
}
}
This Thread.sleep method let's the current Thread "wait" for the specified time.
The delay can be adjusted to something more practical. You could for example calculate how many frames you want and sleep for that amount.
Another way would be to "time" the updates. This could be done with a timer. Since it is more or less deprecated, i implement it using the ScheduledExecutorService
int delay = 15; // adjust the delay
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool();
scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
public void run() {
ball.move();
player.move();
}
}, delay, TimeUnit.MILLISECONDS)
As of Java8, you might write this using a Lambda like this:
scheduledExecutorService.scheduleAtFixedRate(() -> {
ball.move();
player.move();
}, delay, TimeUnit.MILLISECONDS)
To stop it, you could now call:
scheduledExecutorService.shutdown();
However: There are more sophisticated solutions. One is, as you already noted, the double buffering. But there are also multiple different techniques, that compensate more difficult problems. They use something called page flipping.
The problem you are having is that you have split your paint methods,
if you make one class that is dedicated to doing the painting and you put all of your paints in one paint method it should work without flickering.
I would also recommend looking into making your paint calls run on a timer which lets you decide the refresh rate which usually leads to a smoother experience overall.
Here is an example of my graphics class in my latest game,
class GameGraphics extends JPanel implements ActionListener {
private Timer refreshHZTimer;
private int refreshHZ = 10;
private int frameID = 0;
public GameGraphics(int width, int height) {
setBounds(0,0, width, height);
setVisible(true);
refreshHZTimer = new Timer(refreshHZ, this);
refreshHZTimer.start();
}
#Override
public void actionPerformed(ActionEvent e) {
frameID++;
if (frameID % 100 == 1)
System.out.println("Painting FrameID: " + frameID);
repaint();
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
//Init graphics
Graphics2D g2 = (Graphics2D) g;
//Paint stuff here:
}
}
And add this code to your JFrame constructor:
GameGraphics gpu = new GameGraphics(width, height);
frame.add(gpu);

Using Android canvas in different class, debug crash errors

Just starting out with Android, canvas, the works. I have existing Java code that can draw shapes on a graphics object. I am trying to use that code in an Android app with Canvas. Basically, I'm trying to avoid refactoring all of my Java code to use Canvas explicitly. So I'm in the process of making my own "Graphics" object. All it should do is call the appropriate Canvas methods to draw the specified shape.
I've read multiple posts here about not being able to use the canvas object outside of the onDraw() method. From what I understand, you can't pass a canvas object to a different class and expect it to work correctly. But I also do not have a complete understanding of how all this works.
The app crashes in the Graphics class in the drawOval method. I've read up a log about all of this, but I haven't found a good answer as to why this specifically doesn't work.
I haven't been able to find a way to get crash logs in a java friendly way (aka a stacktrace). It just throws Fatal signal 11 (SIGSEGV), code 1, fault addr 0x130 in tid 1520.
Thanks! Let me know if more details are needed. Here's my code:
MainActivity
public class MainActivity extends AppCompatActivity {
MyCanvas canvas;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
canvas = new MyCanvas(this);
setContentView(canvas);
}
}
MyCanvas View
public class MyCanvas extends View {
Graphics graphics;
List<Shape> shapes;
public MyCanvas(Context context) {
super(context);
graphics = new Graphics();
shapes = new ArrayList<>();
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
graphics.setCanvas(canvas); //Sets the canvas object in the graphics class
for (Shape shape : shapes) {
try { //this is in a try/catch block for custom exception handling
//This just calls the specific shapes render method,
//in this case, a circle from the makeShape Method
//The graphics object then calls the specific shape to
//render on the canvas
shape.render(graphics, 0, 0);
} catch (ShapeException e) {
e.printStackTrace();
}
}
invalidate();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
int x = (int) event.getX();
int y = (int) event.getY();
try { //this is in a try/catch block for custom exception handling
makeShape(x, y);
} catch (ShapeException e) {
e.printStackTrace();
}
invalidate();
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL: {
break;
}
}
invalidate();
return true;
}
public void makeShape(int x, int y) throws ShapeException{
Point p = new Point(x, y);
Shape shape = new Circle(p, 50, 50);
shapes.add(shape);
}
}
Graphics
public class Graphics {
private Canvas canvas = null;
public Graphics() {
}
public void setCanvas(Canvas canvas) {
if (this.canvas == null) {
this.canvas = canvas;
}
}
public void drawOval(int x, int y, int width, int height) {
Paint paint1 = new Paint();
paint.setColor(Color.BLACK);
canvas.drawCircle(500, 500, 50, paint1); //App crashes here
}
}
You have a slight misunderstanding about being able to pass a Canvas to different classes. There is absolutely nothing wrong with that; passing a canvas to a class method is effectively the same as passing it to a regular function. Even storing the Canvas reference in a member variable isn't going to hurt anything.
HOWEVER, the above is only true with the understanding that the Canvas cannot be used outside the bounds of the draw()/onDraw() method. That is, any method that uses the Canvas must be called from within onDraw(), or a function called by onDraw(), etc.
This is because the Canvas is initialized (by the framework) immediately before onDraw(), in order to prepare for the current drawing operation. You might wish to think of it as initializing the Canvas with a Bitmap that will serve as the drawing output surface for this particular screen frame (which is not far from the truth). Once onDraw() has returned, the framework assumes your code will no longer be using it, and it can submit the output surface/Bitmap/etc. to the screen compositor for rendering, without fear of further alterations.
And, your approach is a good one, as a technique for adapting existing code to use a novel graphics object.
So, to address the crash: SIGSEGVs should never happen in normal Android development when you're not dealing (directly) with native/JNI routines. However, it occurs to me that you're not updating the Canvas object in the event it changes. This could be causing the crash (in native code, which is why you don't get a Java stack trace). You used an old Canvas after a newer one was given to you. Remove the condition if (this.canvas == null) and you should be fine.

Java Graphic trouble when shooting multiple bullet

So i'm trying to create this java game about aircraft shooting aliens and stuff. The aircraft shoot a bullet every time mouse click. That mean the aircraft can shoot 10 or 20 or more bullets at a time. To demonstrate the bullet movement i tried Thread and Timer but the real problem is if i 1 bullet shot out that mean i created a new Thread(or Timer) and that make the game run very slow. Is there any way i can fix this problem?
Here a my code for bullet moving
public class Bullet extends JComponent implements Runnable {
int x;//coordinates
int y;
BufferedImage img = null;
Thread thr;
public Bullet(int a, int b) {
x = a;
y = b;
thr = new Thread(this);
thr.start();
}
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
try {
img = ImageIO.read(new File("bullet.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// g.drawImage(resizeImage(img, 2), x, y, this);
g.drawImage(Plane.scale(img, 2, img.getWidth(), img.getHeight(), 0.125, 0.125), x, y, this);
width = img.getWidth() / 8;
height = img.getHeight() / 8;
super.paintComponent(g);
}
public void run() {
while(true)
{
if(y<-50)break;//if the bullet isnt out of the frame yet
y-=5;//move up
repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
A bullet should NOT be on its own thread. There are several reasons for this, one of which is the one you mentioned - it is going to make your game very slow.
Try using one master thread which updates all bullets. You will need an update function in your bullet:
public class Bullet extends JComponent {
public void update() {
if(y<-50)return; //if the bullet isnt out of the frame yet
y-=5; //move up
}
//all your other code for your bullet
}
Then in your master thread have a list of bullets:
LinkedList<Bullet> bullets = new LinkedList<>();
In the run method of that thread, you can continuously update ALL bullets:
public void run() {
while(true)
{
for (Bullet b : bullets) {
b.update();
}
repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
You will need to have a method in your master thread that lets you add a new bullet:
public void addBullet(Bullet b) {
bullets.add(b);
}
Then you can call that to add a new bullet and the master thread will update that bullet along with all the others.

canvas not drawing my required object

Program reach in loadpng block....it also prints the toast "it shold draw something but it dont draw circle but draw all the remaining stuff that is in my arraylist"
#Override
public void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);
int p=0,cir=0,r=0,l=0;
if(load_png)
{
Toast_Display.long_message(getContext(),"should draw something");
canvas.drawCircle(300,300,100,get_paint(Color.WHITE,10));
invalidate();
}
for(int sequence : sequence_draw)
{
if(sequence==1)
{
int color = color_name.get(p);
int width = brush_width.get(p);
Path mypath = path_list.get(p);
Paint paint = get_paint(color,width);
canvas.drawPath(mypath, paint);
p++;
}
else if(sequence==2)
{
circle c = circles.get(cir);
canvas.drawCircle(c.getCx(),c.getCy(),c.getRadious(),get_paint2(c.getColor(),c.getWidth()));
cir++;
}
else if(sequence==3)
{
Rectangle rec = rectangles.get(r);
canvas.drawRect(rec.getCx(),rec.getCy(),rec.getDx(),rec.getDy(),get_paint2(rec.getColor(),rec.getWidth()));
r++;
}
else if(sequence==4)
{
Rectangle line = lines.get(l);
canvas.drawLine(line.getCx(),line.getCy(),line.getDx(),line.getDy(),get_paint(line.getColor(),line.getWidth()));
l++;
}
}
if(draw==true)
{
if(status.equals("circle"))
canvas.drawCircle(cx,cy,radious,get_paint2(current_circle_color,current_circle_width));
else if(status.equals("rec"))
canvas.drawRect(cx,cy,dx,dy,get_paint2(current_Rectngle_color,current_Rectngle_width));
else if(status.equals("line"))
canvas.drawLine(cx,cy,dx,dy,get_paint(current_line_color,current_line_width));
}
}
I don't know why it's not drawing my required object in load png block.... any help will be appreciated
Try removing the invalidate() call. It does not make sense to call invalidate() inside onDraw(), since it is invalidation that causes oDraw() to be called in the first place!
The fact that you are calling it in onDraw() could be causing Android to do something unexpected.

bitmap being drawn multiple times on surfaceview

I'm just trying to draw a circle on the spot where I touch the screen. When I touch the screen, a circle is drawn there, but then when I touch the screen somewhere else a new circle is drawn (the old one is supposed to appear there, not a new one). Does anyone understand why this is happening? Code:
Thread class:
public class GameThread extends Thread{
private SurfaceHolder sHolder;
private DrawingSurface dSurface;
private boolean okToRun;
Paint redPaint = new Paint();
public int x, y;
boolean myTouchEvent(MotionEvent event){
int touch = event.getAction();
switch(touch){
case MotionEvent.ACTION_DOWN:
x = (int) event.getX();
y = (int) event.getY();
}
return true;
}
public GameThread(SurfaceHolder holder, DrawingSurface surface){
sHolder = holder;
dSurface = surface;
redPaint.setARGB(255, 255, 0, 0);
}//GameThread()
public void setOkToRun(boolean status){
okToRun = status;
}//setOkToRun()
public void run(){
while(okToRun){//gameloop
Canvas canvas = null;
try{
canvas = sHolder.lockCanvas(null);
synchronized(sHolder){
try{
canvas.drawCircle(x, y, 60, redPaint);
} catch (Exception e){
}
}
} finally {
if (canvas != null) {
sHolder.unlockCanvasAndPost(canvas);
}
}
}
}//run()
}
Here are the relevant methods of my surfaceview class:
#Override
public boolean onTouchEvent(MotionEvent event) {
return drawingThread.onTouchEvent(event);
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
drawingThread = new GameThread(getHolder(), this);
drawingThread.start();
drawingThread.setOkToRun(true);
}
#Override
public void surfaceDestroyed(SurfaceHolder arg0) {
radius += 10;
drawingThread.setOkToRun(false);
try {
drawingThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
TO BE CLEAR: I want to draw a single circle, and have it appear wherever I tap the screen. It does not do that. Can you tell why that is the case in this code?
I hope I am right that you need to draw a circle at the place where the screen was touched and the problem is that circles from previous touches still appear on the screen.
In this case the problem is, that you are drawing circles on canvas without clearing it. Canvas is represented in a memory as a array. When you call Canvas.drawCircle() part of memory is rewritten by an image of circle. When you do it repeatedly, the canvas contains more circles which are being drawn on the screen. You need to repaint the whole canvas before you draw a new circle. It can be done by calling the method Canvas.drawColor() before calling the method Canvas.drawCircle(). It clears the whole Canvas with selected color.

Categories

Resources