-
[SOLVED] Applet Center
I'm writing a program to make curves from straight lines. You'll understand what I mean if you run it. Anyway, my problem is that it is using (centerX, 0) as the center rather than (centerX, centerY). I've looked through it several times, but couldn't figure out why. Does anyone know?
Here's my code thus far:
Code:
import java.awt.*;
import java.applet.*;
import java.util.Random;
public class Curves extends Applet implements Runnable
{
public Image backbuffer;
public Graphics backg;
public Thread thread;
public Random gen = new Random();
public boolean start = true;
public int n = 600, inc = 10;
public int WIDTH = (int)(n*1.2), LENGTH = (int)(n*1.2);
public int centerX = WIDTH/2, centerY = HEIGHT/2;
public int Q1X, Q2X, Q3X, Q4X, Q1Y, Q2Y, Q3Y, Q4Y;
public void init()
{
requestFocus();
centerX = WIDTH/2;
centerY = HEIGHT/2;
Q1X = centerX;
Q2X = centerX-n/2;
Q3X = centerX;
Q4X = centerX+n/2;
Q1Y = centerY-n/2;
Q2Y = centerY;
Q3Y = centerY+n/2;
Q4Y = centerY;
setSize(WIDTH, LENGTH);
setBackground(Color.black);
backbuffer = createImage(WIDTH, LENGTH);
backg = backbuffer.getGraphics();
repaint();
}
public void start()
{
thread = new Thread(this);
thread.start();
}
public void run()
{
Thread current = Thread.currentThread();
while(current == thread)
{
try
{
Thread.sleep(100);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
repaint();
}
}
public void stop()
{
thread = null;
}
public void update(Graphics g)
{
//backg.setColor(new Color(gen.nextInt(256), gen.nextInt(256), gen.nextInt(256)));
backg.setColor(Color.red);
if(Q1X < centerX + n/2 && Q1Y < centerY)
{
backg.drawLine(centerX, Q1Y, Q1X, centerY);
Q1Y-=inc;
Q1X+=inc;
}
if(Q2X < centerX && Q2Y > centerY - n/2)
{
backg.drawLine(Q2X, centerY, centerX, Q2Y);
Q2Y-=inc;
Q2X+=inc;
}
if(Q3X > centerX - n/2 && Q3Y > centerY)
{
backg.drawLine(centerX, Q3Y, Q3X, centerY);
Q3Y-=inc;
Q3X-=inc;
}
if(Q4X > centerX && Q4Y < centerY - n/2)
{
backg.drawLine(Q4X, centerY, centerX, Q4Y);
Q4Y+=inc;
Q4X-=inc;
}
requestFocus();
paint(g);
}
public void paint(Graphics g)
{
g.drawImage(backbuffer, 0, 0, this);
}
}
EDIT: Added if statements for the drawLine methods.
EDIT: Fixed some logic errors with the inc variable.
-
OMG I figured it out! Lol, i was using HEIGHT and LENGTH and yeah... anyway silly mistake... now for me to fix the other issues. Thanks anyway.