I mean I want to drawline but slowly.
I write this code to draw line in ondraw method.
.
.
.
.
caneta.setARGB(255, 255, 0,0);caneta.setStrokeWidth(10);
canvas.drawLine(0, ys * 1/2, this.getWidth(), ys * 1/2, caneta);
.
.
.
how I did it slowly?
That's nearly like a gameloop works:
-invalidate your canvas ever X milliseconds (using a loop and Thread.sleep())
-increment your X/Y coords after every loop
-handle the new coords in onDraw() again
Example:
private int x1, x2;
private int y1, y2;
private View v;
public void start()
{
for (int i = 0; i <= 250; i++)
{
v.invalidate();
x2 += 1;
try
{
Thread.sleep(50);
}
catch (InterruptedException e)
{
}
}
}
in your existing view class, where you already have your onDraw method
protected void onDraw(Canvas canvas)
{
//draw your line here using your X and Y member
canvas.drawLine(x1, y1, x2, y2, caneta);
}
In every onDraw method call, draw a portion of the line incrementally, depending on the speed required. For example, if you want a slow drawing, increase the size by say 5 pixels in every call till the full line is reached.
private float linePercent = 0;
protected void onDraw (Canvas canvas){
float lineX = this.getWidth() * linePercent;
canvas.drawLine(0, ys * 1/2, lineX, ys * 1/2, caneta);
linePercent += 0.05f;
if(linePercent >= 1){
linePercent = 0;
}
}
And in a background thread, schedule an invalidate on your view.
Related
I am trying to make a program where there are lines in a grid pointing towards the mouse like magnets. I am a beginner in Processing, can someone point me towards a tutorial on how to do that or give me some code and explain what it does?
int x1 = 0;
int x2 = 0;
int y1 = 0;
int y2 = 0;
void setup() {
size(200, 200);
}
void draw() {
background(255, 255, 0);
x1 = (mouseX + 100) / 2;
y1 = (mouseY + 100) / 2;
x2 = -1 * x1 + 200;
y2 = -1 * y1 + 200;
line(x1, y1, x2, y2);
}
There's plenty of solutions for this project. One of the easiest is to use Processing's PVector class.
The PVector class can be used for two or three dimensional vectors. A vector is an entity that has both magnitude and direction. The PVector class, however, stores the components of the vector (x,y for 2D, and x,y,z for 3D). The magnitude and direction are calculated from the components and can be accessed via the methods mag() and heading().
A two dimensional vector in Processing is defined through x and y components:
PVector v = new PVector(xComponent, yComponent);
With some mathematical formulae, you can determine magnitude and direction using the x- and y-components. But we don't need to determine these.
Below, I've attached completed solution code. Most of it should make sense to you. But it's worth understanding what is going on with PVector.
A nested for loop within void draw() contains x and y variables that represent the coordinates of each grid vertex.
We first define PVector v as a vector given by an x-component of mouseX - x, or the difference between the x-positions of the mouse and each grid point. Similarly, the y-component given by mouseY - y has the same difference.
Creating a variable PVector u initialized from v.setMag(15) holds a PVector that has the same direction as v, but with a length of just 15.
Now to draw the lines. Vectors represent an offset, not a position (in this case), so drawing a line from a grid point to an offset of a grid point is key.
Hence line(x, y, x + u.x, y + u.y), where u.x and u.y are the x- and y-components of the vector u.
void setup() {
size(600, 600); // Set the size of the canvas to 600x600.
}
void draw() {
background(255);
stroke(200); // Set the stroke color to black
int distVertLine = width / 10; // This variable defines the distance between each subsequent vertical line.
for(int i = 0; i < width; i += distVertLine) {
line(i, 0, i, height); // Draw a line at x=i starting at the top of the canvas (y=0) and going to the bottom (y=height)
}
int distHorizLine = height / 10; // This variable defines the distance between each subsequent vertical line.
for(int i = 0; i < width; i += distHorizLine) {
line(0, i, width, i); // Draw a line at y=i starting at the left of the canvas (x=0) and going to the right (x=width)
}
stroke(0); // Set the stroke to black.
// Use a nested for loop to iterate through all grid vertices.
for(int x = 0; x <= width; x += width/10) {
for(int y = 0; y <= height; y += height/10) {
PVector v = new PVector(mouseX - x, mouseY - y); // Define a vector that points in the direction of the mouse from each grid point.
PVector u = v.setMag(15); // Make the vector have a length of 15 units.
line(x, y, x + u.x, y + u.y); // Draw a line from the grid vertex to the terminal point given by the vector.
}
}
}
The answer already given by Ben Myers is excellent! The code below has a few small modifications:
the two for loops for the grid lines have been combined (since width and height are equal);
the construction of the vector is combined with setting the magnitude;
some minor changes to colors and comments.
Modified code:
void setup() {
// Set the size of the canvas to 600x600 pixels.
size(600, 600);
}
void draw() {
// There are 10x10 grid cells that each have a size of 60x60 pixels.
int gridSize = width / 10;
// Set the background color to anthracite and the stroke color to orange.
background(56, 62, 66);
stroke(235, 113, 52);
// Draw vertical and horizontal grid lines.
for (int lineIndex = 0; lineIndex < gridSize; lineIndex++) {
line(lineIndex * gridSize, 0, lineIndex * gridSize, height);
line(0, lineIndex * gridSize, width, lineIndex * gridSize);
}
// Set the stroke color to blue.
stroke(0, 139, 225);
// Use a nested for loop to iterate through all grid cells.
for (int x = 0; x <= width; x += gridSize) {
for (int y = 0; y <= height; y += gridSize) {
// Define a vector that points in the direction of the mouse from
// each grid point and set the vector length to 15 units.
PVector vector = new PVector(mouseX - x, mouseY - y).setMag(15);
// Draw a line from the grid point to the end point using the vector.
line(x, y, x + vector.x, y + vector.y);
}
}
}
I have written a program in which a UFO (in essence, a gray ellipse) appears from the center of the screen and flies to the edge. There is a laser that appears when the mouse is pressed, and disappears when the mouse is released. I want to make it so that the UFO disappears when the mouse clicks on it/the laser touches it.
I've made it as far as to make the UFO class and create variables that determine its movements and speed, and I was able to get the laser to appear directly on the cursor. I thought of making an 'if' statement to check if the cursor is within the radius (or diameter) of the UFO, and placing it inside of the for loop I created for the UFOs. However, I am not sure how to achieve the proper syntax to make it happen.
Note: You may need to wait a few seconds for the first circle to appear after you play the sketch.
UFO[] ufos = new UFO[3];
void setup() {
size(700, 700);
for (int j = 0; j < ufos.length; j++) {
ufos[j] = new UFO();
}
}
//UFO class
//Class setup ends on line 61
class UFO {
float a;
float b;
float c;
float sa;
float sb;
float d;
UFO() {
//declare float a/b/c value
a = random(-width/2, width/2);
b = random(-height/2, width/2);
c = random(width);
}
//UFO movement
void update() {
//float c will serve as a speed determinant of UFOs
c = c - 1;
if (c < 5) {
c = width;
}
}
//UFO setup
void show() {
//moving x/y coordinates of UFO
float sa = map(a / c, 0, 1, 0, width);
float sb = map(b / c, 0, 1, 0, height);
float d = map(c, 0, width, 50, 0);
//UFO drawing shapes
//ellipse is always sa (or sb) / c to allow UFO to appear
//as if it is moving through space
fill(200);
ellipse((sa / c), (sb / c), d + 5, d+5);
//Hides UFO way off the screen
//and replaces it with a black-filled ellipse until
//it forms into a full circle
//When radius d becomes 50, the UFO flies from the
//center of the screen to off of the screen
if (d < 50) {
fill(0);
ellipse(-5, -10, 90, 90);
sa = 10000;
sb = 10000;
}
}
}
void draw() {
//Background
background(0);
//Translated the screen so that the UFOs appear to fly from
//the center of the screen
translate(width/2, height/2);
//UFO draw loop, make UFO visible on screen
for (int j = 0; j < ufos.length; j++) {
ufos[j].update();
ufos[j].show();
//mouse-click laser
if (mousePressed == true) {
fill(200,0,0);
ellipse(mouseX - 352,mouseY - 347,50,50);
}
}
}
Like I said on the Happy Coding forum:
Basically, if your UFO is a series of circles, then you just need to use the dist() function to check whether the distance from the mouse to the center of the circle is less than the radius of the circle. If it is, then the mouse is inside the circle. Here's a small example:
float circleX = 50;
float circleY = 50;
float circleDiameter = 20;
boolean showCircle = true;
void draw(){
background(0);
if(showCircle){
ellipse(circleX, circleY, circleDiameter, circleDiameter);
}
}
void mousePressed(){
if(dist(mouseX, mouseY, circleX, circleY) < circleDiameter/2){
showCircle = false;
}
}
If your UFO is multiple circles, then you need to apply this logic to each circle. Please try something and post a small example like this one (not your whole sketch) if you get stuck. Good luck.
I'm working on a simple game and i need these squareBumpers which simply stands idle and when got hit, collides and reflects the ball. But currently the ball just flies through my squareBumpers. I can only use java awt and swing libraries. Here's the code:
class squareBumper {
private int x = 300;
private int y = 300;
private Color color = new Color(66,139,139);
public void paint(Graphics g) {
Rectangle clipRect = g.getClipBounds();
g.setColor(color);
g.fillRect(x, y, 31, 31);
}
}
class BouncingBall {
// Overview: A BouncingBall is a mutable data type. It simulates a
// rubber ball bouncing inside a two dimensional box. It also
// provides methods that are useful for creating animations of the
// ball as it moves.
private int x = 320;
private int y = 598;
public static double vx;
public static double vy;
private int radius = 6;
private Color color = new Color(0, 0, 0);
public void move() {
// modifies: this
// effects: Move the ball according to its velocity. Reflections off
// walls cause the ball to change direction.
x += vx;
if (x <= radius) { x = radius; vx = -vx; }
if (x >= 610-radius) { x = 610-radius; vx = -vx; }
y += vy;
if (y <= radius) { y = radius; vy = -vy; }
if (y >= 605-radius) { y = 605-radius; vy = -vy; }
}
public void randomBump() {
// modifies: this
// effects: Changes the velocity of the ball by a random amount
vx += (int)((Math.random() * 10.0) - 5.0);
vx = -vx;
vy += (int)((Math.random() * 10.0) - 5.0);
vy = -vy;
}
public void paint(Graphics g) {
// modifies: the Graphics object <g>.
// effects: paints a circle on <g> reflecting the current position
// of the ball.
// the "clip rectangle" is the area of the screen that needs to be
// modified
Rectangle clipRect = g.getClipBounds();
// For this tiny program, testing whether we need to redraw is
// kind of silly. But when there are lots of objects all over the
// screen this is a very important performance optimization
if (clipRect.intersects(this.boundingBox())) {
g.setColor(color);
g.fillOval(x-radius, y-radius, radius+radius, radius+radius);
}
}
public Rectangle boundingBox() {
// effect: Returns the smallest rectangle that completely covers the
// current position of the ball.
// a Rectangle is the x,y for the upper left corner and then the
// width and height
return new Rectangle(x-radius, y-radius, radius+radius+1, radius+radius+1);
}
}
Take a look at the classes that implement the Shape interface. There are ellipses and other shapes, and they all implement a intersects(Rectangle2D) method. It might help you if you don't want to perform intersection yourself.
As for dealing with the collision, well, it depends on the level of accuracy you want. Simply deflecting the ball of edges is quite easy. Just determine whether the collided side of the rectangle is vertical or horizontal, and negate the corresponding velocity component accordingly. If you want to handle the corners, well that is a bit more complicated.
You need to detect when the ball has collided with the bumper. You have the boundingBox() method of BouncingBall, this will get you a rectangle that contains your ball. So you need to check if this rectangle intersects your square bumper (which implies a collision), and then do something with that.
Im using de DDA (Digital Diferential Analizer) to make a line, and thought I know maybe using de DrawLine the way I am, just run along with it. Im trying to make different types of lines like dashed or dotted, etc. Im thinking in makeing the for from below jump some numbers to make a dotted line. But I cant still find a way todo it. This is what I have so far:
public void paint(Graphics g) {
super.paint(g);
int dot=0;
int x1 = pointStart.x;
int x2 = pointEnd.x;
int y1 = pointStart.y;
int y2 = pointEnd.y;
float dx, dy, m, y, x;
if (x1>x2){
int ax = x2;
int ay = y2;
x2 = x1;
x1 = ax;
y2 = y1;
y1 = ay;
}
dx = x2 - x1;
dy = y2 - y1;
m = dy/dx;
if (m>=-1&&m<=1){
dot = (int)dx/4;
y = y1;
System.out.println(m);
for (x = x1 ; x <= x2;x++){
//if (x>=dot&&x<=dot+10||x>=dot*2&&x<=dot*2+10||x>=dot*3&&x<=dot*3+10){
g.drawLine((int)x, (int)Math.round(y), (int)x, (int)Math.round(y));
y+=m;
//}
}
}
else{
x = x1;
System.out.println(m);
for (y = y1 ; y <= y2;y++){
g.drawLine((int)Math.round(x), (int)y, (int)Math.round(x), (int)y);
x+=1/m;
}
}
/*if (pointStart != null) {
if (x1>)
g.setColor(Color.RED);
//g.drawLine(pointStart.x, pointStart.y, pointEnd.x, pointEnd.y);
g.drawLine(x1, y1, x1, y1);
}*/
}
Any ideas?
you need draw line function (in your case g.drawline(x0,y0,x1,y1); ).
dont care about color (you can play with it later)
you need definition of your pattern (size of lines and spaces in pixels)
for example int pattern[]={10,-5,0} (10px line, then 5px space, 0 means repeat from beginning) ... -values are spaces + values are lines
you need 'global' state (actual index in pattern and actual pixel length drawed) also you can have global pattern pointer or encapsulate all in class/struct.
ok so the basic idea is to segmentate any line to selected pattern for example like this:
//---------------------------------------------------------------------------
// pattern draw state
int _pattern_ix=0; // actual index in pattern need to reset it to zero before any pattern change
double _pattern_l=0; // already drawed or skipped pixels from actual pattern[_pattern_ix]
// predefined patterns
int _pattern_dash_dash[]={ 10,-10, 0 };
int _pattern_dash_dot[] ={ 10,- 5, 1,- 5,0 };
int _pattern_dot_dot[] ={ 1,- 5, 0 };
//---------------------------------------------------------------------------
// draw line function
void drawline(int x0,int y0,int x1,int y1)
{
// this is just borland GDI access to draw line function
Form1->Canvas->MoveTo(x0,y0);
Form1->Canvas->LineTo(x1,y1);
}
//---------------------------------------------------------------------------
void pattern_line(int x0,int y0,int x1,int y1,int *pattern)
{
int p;
double x,y,xx,yy,dx,dy,dl,t,dt;
dx=x1-x0;
dy=y1-y0;
dl=sqrt((dx*dx)+(dy*dy));
dx/=dl; dy/=dl;
for (t=0.0,dt=0.0;dl>=0.5;)
{
p=pattern[_pattern_ix];
if (p<0) // skip
{
dt=-p-_pattern_l; // t=space to skip [px]
if (dt>dl) { _pattern_l+=dl; return; } // space is bigger then rest of line
dl-=dt; t+=dt; _pattern_l=0.0; // update line params and continue to next pattern entry
}
else // draw
{
dt=+p-_pattern_l; // t=space to draw [px]
x=x0+double(t*dx); // actual point pos
y=y0+double(t*dy); // space is bigger then rest of line
if (dt>dl) { _pattern_l+=dl; drawline(x,y,x1,y1); return; }
dl-=dt; t+=dt; _pattern_l=0.0; // update line params
xx=x0+double(t*dx); // actual point pos
yy=y0+double(t*dy);
drawline(x,y,xx,yy); // draw line and continue to next pattern entry
}
_pattern_ix++;
if (!pattern[_pattern_ix]) _pattern_ix=0;
}
}
//---------------------------------------------------------------------------
void main()
{
// borland GDI clear screen and color settings
Canvas->Brush->Color=clBlack;
Canvas->Pen->Color=clWhite;
Canvas->FillRect(ClientRect);
// draw dash-dot-ed rectangle
int x0,x1,y0,y1;
x0=30; x1=200;
y0=30; y1=100;
pattern_line(x0,y0,x1,y0,_pattern_dash_dot);
pattern_line(x1,y0,x1,y1,_pattern_dash_dot);
pattern_line(x1,y1,x0,y1,_pattern_dash_dot);
pattern_line(x0,y1,x0,y0,_pattern_dash_dot);
}
//---------------------------------------------------------------------------
and do not forget to reset pattern ix,l to zero before any pattern style change.
Code is not optimized so its pretty slow but simple enough to understand i hope.
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 200x300). 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.
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);
}
}
You want to look into the bresenham algorithm. A bresenham algorithm is a method to draw or rasterize a line. It's a bit different from the subdivision of a grid in a certain angle for example a morton-curve. It's a bit like compute the scalar product for every angle like recall here Traversing a 2D array in an angle.