Results 1 to 2 of 2
Thread: [SOLVED] Applet Center
- 03-25-2009, 06:05 PM #1
Senior Member
- Join Date
- Jan 2009
- Location
- NJ, USA
- Posts
- 183
- Rep Power
- 5
[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:
EDIT: Added if statements for the drawLine methods.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: Fixed some logic errors with the inc variable.Last edited by AndrewM16921; 03-25-2009 at 09:27 PM.
- 03-26-2009, 12:38 AM #2
Senior Member
- Join Date
- Jan 2009
- Location
- NJ, USA
- Posts
- 183
- Rep Power
- 5
Similar Threads
-
JFrame at Center Screen
By hiranya in forum AWT / SwingReplies: 8Last Post: 02-11-2010, 04:29 PM -
jsf center tag needed
By Srikala in forum JavaServer Faces (JSF)Replies: 5Last Post: 07-27-2009, 11:49 AM -
center a form
By tommy in forum New To JavaReplies: 2Last Post: 08-06-2007, 08:47 PM -
Help Center Live 2.1.3
By JavaBean in forum Java SoftwareReplies: 0Last Post: 07-06-2007, 03:43 PM -
Applet, To center text and To open I engage in a dialog in an Applet
By Marcus in forum Java AppletsReplies: 4Last Post: 06-08-2007, 06:15 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks