Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6188601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:15:40+00:00 2026-05-24T02:15:40+00:00

I’m making my first game using Java on Android. I need to draw a

  • 0

I’m making my first game using Java on Android. I need to draw a lot of pixels which together should create a line. My first approach was to make a large array of booleans, create a loop, and draw a pixel when the associated boolean was true.

It wasn’t a good idea of course (the array is about 200×300). Now I remember only the position of the first pixel of the line, and every next pixel has to remember his follower. It works pretty well, but when the line gets longer (but still not very long), the efficiency is bad (<20 fps after 4000 frames).

This is the function that I use to draw a line (only one for now). Can anybody help me improve its efficiency?

public void drawLine(Canvas canvas, int beginx, int beginy) {
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.RED);
    paint.setStrokeWidth(3);

    int x = beginx;
    int y = beginy;
    while(C.mGrid[x][y].nx != -1) {
        //canvas.drawLine(x, y, C.mGrid[x][y].nx, C.mGrid[x][y].ny, paint);
        canvas.drawPoint(x, y, paint);
        Grid temp = C.mGrid[x][y];
        if ((C.mGrid[x][y].nx == x) && (C.mGrid[x][y].ny == y)) break;
        x = temp.nx;
        y = temp.ny;
    }
}

and Grid.java:

package com.qwak.achtung;

public float x = 0,y = 0;
public int px = -1, py = -1, nx = -1, ny = -1;

public Grid(float x, float y) {
    this.x = x;
    this.y = y;
}

public void set(int px, int py, int nx, int ny) {
    this.px = px;
    this.py = py;
    this.nx = nx;
    this.ny = ny;
}

public void setp(int px, int py) {
    this.px = px;
    this.py = py;
}

public void setn(int nx, int ny) {
    this.nx = nx;
    this.ny = ny;
}

PS: It looks like this http://c.wrzuta.pl/wi10559/11f7d10b00110e504e25ebd3/0/andek 14 is fps (on my phone (samsung Spica) it run better – 40 but after a while it decreases to 20 and even less) and 983 is number of frames at all.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T02:15:42+00:00Added an answer on May 24, 2026 at 2:15 am

    There is a drawLine method in the canvas object.

    Use the example here: How to draw a line in android

    canvas.drawLine(0, 0, 20, 20, paint);
    

    If you want to draw a curve. Find the function of the curve. A Parabola for example is x=y^2. You can get points from the curve: 1 = 1, 2 = 4, 3 = 9, 4 = 16… etc.. If your drawing pixel by pixel you can plug in your x and get your y and draw it.

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.RED);
    paint.setStrokeWidth(3);

    for(int i = beginx; i < CanvasWidth; i++)
    {
        int x = i;
        int y = i * i; //x=y^2
        canvas.drawPoint(x, y, paint);
    }
    

    To keep a record of points that were visited you could do the following:

    class Point
    {
        int x;
        int y;
    }
    
    
    List<Point> points = new List<Point>();
    
    onMove(int newX, int newY)
    {
        Point p = new Point();
        p.x = newX;
        p.y = newY;
    
        points.add(p);
    }
    
    
    onDraw()
    {
        for(Point p : points)
        {
            canvas.drawPoint(p.x, p.y, paint);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.