I have a code inside that needs tweaking JAVA
For the most part this does what i want but i would like it to do the following:
back increment the array so that i dont have to use index at 0 to get the app to start at top. I'm trying to create a YoYo and I would like the line to go backwards as well as forward without having to index back to 0 at the end of the forward run. I would also like the circle to be a different color then the line.
Code:
import java.awt.*;
import java.applet.*;
public class YoYo2 extends Applet //make the class
{
private int ind = 0;
private int x = 20;
private int y = 20;
int[] hor = { 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60};
int[] vert = { 40,90, 100, 110, 120, 130, 140, 150, 160, 170, 180};
int[] cHor = { 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50};
int[] cVert = { 40,90, 100, 110, 120, 130, 140, 150, 160, 170, 180};
private int sleep = 150;
public void start()
{
ind = 0;
}
public void paint(Graphics gr)
{
gr.setColor(Color.WHITE);
gr.drawLine(60, 40, hor[ind], vert[ind]);
gr.fillOval(cHor[ind], cVert[ind],x, y);
++ind;
if(ind == hor.length)
ind = 0;
gr.setColor(Color.BLACK);
gr.drawLine(60, 40, hor[ind], vert[ind]);
gr.fillOval(cHor[ind], cVert[ind],x, y);
try
{
Thread.sleep(sleep);
}
catch(InterruptedException e)
{
}
repaint();
}
}
Re: I have a code inside that needs tweaking JAVA
Hi I am learning Java.
I don't have any idea about array but for Oval color why don't you initializes a color variable?
Quote:
// The colors you will use
Color redColor;
// Standard colors can be named like this
redColor = Color.red;
// a circle (int x, int y, int width, int height,int startAngle, int arcAngle);
// ovals are also possible this way.
g.fillArc(120,120,60,60,0,360);
g.setColor(Color.red);
Quote:
Graphics.setColor() :
This is the setColor() method which is the Graphics class method imported by the java.awt.*; package. This method sets the color for the object by specified color. Here is the syntax of the setColor() method :
g.setColor(Color.color_name);
Graphics.fillOval() :
This is the fillOval() method used to fill the color inside the oval by specified color. Here is the syntax of the fillColor() method :
g.fillColor(Color.color_name);
Before drawing the Oval type this.
g.setColor(Color.color_name);
I hope this Helps.