Results 1 to 2 of 2
  1. #1
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    261
    Rep Power
    6

    Default [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:

    Java 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.
    Last edited by AndrewM16921; 03-25-2009 at 09:27 PM.

  2. #2
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    261
    Rep Power
    6

    Default

    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.

Similar Threads

  1. JFrame at Center Screen
    By hiranya in forum AWT / Swing
    Replies: 8
    Last Post: 02-11-2010, 04:29 PM
  2. jsf center tag needed
    By Srikala in forum JavaServer Faces (JSF)
    Replies: 5
    Last Post: 07-27-2009, 11:49 AM
  3. center a form
    By tommy in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 08:47 PM
  4. Help Center Live 2.1.3
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-06-2007, 03:43 PM
  5. Replies: 4
    Last Post: 06-08-2007, 06:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •