Graphic/Array question

coordinates for drawing a line using an array

Page 1 of 1

3 Replies - 815 Views - Last Post: 24 June 2009 - 06:04 AM Rate Topic: -----

#1 mobius4139  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 118
  • Joined: 24-March 09

Graphic/Array question

Posted 21 June 2009 - 11:42 AM

So I am attempting to create a 'animated' graphic in java and I was wondering if you can use an array to set the coordinates for the g.drawLine command.

here is my rough thoughts but since I am at work I am restricted from compiling and trying to run this or from using an IDE to clear up the bugs. I am attempting to create a simple 'Clock' with continuously rotating hands as part of a bigger project. Just trying to get this running before I go deeper.

Any help would be appreciated.
import java.awt.Color;
import java.awt.Graphics;
import Javax.swing.JPanel;

public class BusinessLogo extends JPanel
{

	public void paintComponent (Graphics g)
	{
		// --- call superclass's paintComponent ---
		super.paintComponent(g);
		

		// --- set array for minute hand 'x' and 'y' coordinates ---
		int minuteHand [][]= {20,21,21,22,22,23,23,24,24,24,23,23,22,22,21,21,20,19,19,18,18,17,17,16,16,16,17,17,18,18,19,19},
						  {16,16,17,17,18,18,19,19,20,21,21,22,22,23,23,24,24,24,23,23,22,22,21,21,20,19,19,18,18,17,17,16};

		// --- set array for hour hand x' and 'y' vertices ---
		int hourHand [][]= {20,21,21,22,22,23,23,24,24,24,23,23,22,22,21,21,20,19,19,18,18,17,17,16,16,16,17,17,18,18,19,19},
					  {16,16,17,17,18,18,19,19,20,21,21,22,22,23,23,24,24,24,23,23,22,22,21,21,20,19,19,18,18,17,17,16};

		// --- for loop to animate the clock ---
		// --- first loop for the hour hand ---		
		for (int hour = 0; hour <999999; hour++)
		{
			for (int minute = 0; minute < hourHand.length; minute++)
			{
				g.getColor(Color.BLACK);  // *here is the code in question
				g.drawLine(20,20,hourHand[hour][hour]); // *can I use the array minutehand 
				g.getColor(Color.BLACK); // * or hourHand to set the second
				g.drawLine(20,20,minuteHand[minute][[minute]); // *  coordinates for my Line
			}
		}

	
	}// --- end method paintComponent
}// --- end class BusinessLogo ---



Is This A Good Question/Topic? 0
  • +

Replies To: Graphic/Array question

#2 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Graphic/Array question

Posted 21 June 2009 - 12:29 PM

My advice for this problem. A 2D array is going to cause you grief. YOu will be better off taking a different approach. Even the way you have it, your 2d arrays are not set up properly. You would need to change them to somthing more like
		
		// --- set array for minute hand 'x' and 'y' coordinates ---
		int minuteHand [][]=  {{20,21,21,22},{22,23,23,24},{24,24,23,23},{22,22,21,21},{20,19,19,18},{18,17,17,16},{16,16,17,17},{18,18,19,19},{16,16,17,17},{18,18,19,19},{20,21,21,22},{22,23,23,24},{24,24,23,23},{22,22,21,21},{20,19,19,18},{18,17,17,16}};

		// --- set array for hour hand x' and 'y' vertices ---
		int hourHand [][]=  {{20,21,21,22},{22,23,23,24},{24,24,23,23},{22,22,21,21},{20,19,19,18},{18,17,17,16},{16,16,17,17},{18,18,19,19},{16,16,17,17},{18,18,19,19},{20,21,21,22},{22,23,23,24},{24,24,23,23},{22,22,21,21},{20,19,19,18},{18,17,17,16}};



But then this still returns the compile error drawLine(int, int, int, int) in java.awt.Graphics cannot be applied to drawLine(int, int, int), so for some reason its only ever reading in 3 ints.

p.s. that &nwsp is adding itself if you see it

This post has been edited by nick2price: 21 June 2009 - 12:32 PM

Was This Post Helpful? 1
  • +
  • -

#3 mobius4139  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 118
  • Joined: 24-March 09

Re: Graphic/Array question

Posted 22 June 2009 - 11:48 AM

okay so here is my idea to get this to work. Assuming that all the array variables are in place does anyone have ant thoughts? I am at work and unable to test it out so any help would be appreciated.

~Bruce

Here's the code:
int hX []= {##,#,##,#,##,#,##}; // numbers to be filled in later
int hY []= {##,#,##,#,##,#,##}; // numbers to be filled in later
int mX []= {##,#,##,#,##,#,##}; // numbers to be filled in later
int mY []= {##,#,##,#,##,#,##}; // numbers to be filled in later

for (int clock = 1; clock < 999999; clock++) // sets an almost infinite loop to repeat the drawing
{
	for (int h = 0; h < 12; h++)
	{
		for (int m = 0; m <mX.length; m++)
		{
		g.getColor(Color.BLACK);  
				g.drawLine(60,60,hX[h],hY[h]);
			g.getColor(Color.BLACK);  
				g.drawLine(60,60,mX[m],mY[m]);
		
		for (int delay = 0; delay < 999999; delay++);
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#4 mobius4139  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 118
  • Joined: 24-March 09

Re: Graphic/Array question

Posted 24 June 2009 - 06:04 AM

Okay, So I am lost and need some help. I am trying to develop a graphic in Java for practice. My idea was to create a clock (not an actual time telling clock) that would appear animated. The problem is when I run the program the existing Hour and Minute hands don't go away and the miute hands create a star with no motion. I know that I get the already drawn lines to disappear using redraw(); Can anyone take a look ath this and tell me where I am going wrong? Thanks!

~Bruce


here is the Logo class:
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JPanel;
import java.util.Random;

public class Logo extends JPanel
{


	@Override
	@SuppressWarnings("empty-statement")
	public void paintComponent (Graphics g)
	{
		// --- call superclass's paintComponent ---
		super.paintComponent(g);

		Random random = new Random();
		this.setBackground( Color.BLACK );


		int green [] = {0, 200,255};

		// --- set array for hand hand 'x' and 'y' coordinates ---
		int hX []= {60,67,74,79,80,79,74,67,60,53,46,41,40,41,46,53};
		int hY []= {40,41,46,53,60,67,74,79,80,79,74,67,60,53,46,41};

		// --- set array for hour hand x' and 'y' vertices ---
		int mX []= {60,65,70,75,81,85,88,89,90,89,88,85,81,75,70,65,60,54,50,44,39,35,32,31,30,31,32,35,39,44,50,55};
		int mY []= {30,31,32,35,39,44,50,55,60,65,70,75,81,85,88,89,90,89,88,85,81,75,70,65,60,54,50,44,39,35,32,31}; // numbers to be filled in later
		
		// set the font and color for the logo
		Font logoFont = new Font("Broadway", Font.ITALIC, 40);
		
		//Color lColor = new Color(255,green[random.nextInt(3)],0);
		Color lColor = new Color(random.nextInt(256), random.nextInt(256),random.nextInt(256));

		// --- Draws the clock circle ---
		g.setColor(lColor);
		g.drawArc(20,20,80,80,0,360);

		// --- draw the Company Name ---
		g.setFont(logoFont);
		g.setColor(Color.RED);
		g.drawString("Mobius Studios",110, 75);
		Color logoColor = lColor;

		// --- for loop to animate the clock ---
		// --- first loop for the hour hand ---
		// --- second loop for the minute hand --- 
		
			for (int h = 0; h < 12; h++)
			{
				for (int m = 0; m <mX.length; m++)
				{
					
					for (int delay = 0; delay < 99; delay++)
					{
						// --- Draw the clock moving the min and hour hands ---
						g.setColor(logoColor);
						g.drawLine(60,60,hX[h],hY[h]);
						g.setColor(logoColor);
						g.drawLine(60,60,mX[m],mY[m]);

						for (int t = 0; t < 9999; t++);
						repaint();
						
					}
				}
			}
			repaint();
	}// --- end method paintComponent
}// --- end class BusinessLogo ---




here is the LogoTest class:

import java.awt.Color;
import javax.swing.JFrame;


public class LogoTest
{
	public static void main(String args[])
	{
		// --- The JFrame and JPanel Attributes ---
		JFrame frame = new JFrame("Mobius, Inc.");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		Logo mobiusJPanel = new Logo();
		frame.add(mobiusJPanel);
		frame.setBackground(Color.BLACK);
		frame.setSize(430,150);
		frame.setVisible(true);
	}
}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1