question of drawing star using JAVA

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 44049 Views - Last Post: 08 April 2009 - 08:57 PM Rate Topic: -----

#1 suckdog  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-October 08

question of drawing star using JAVA

Posted 16 October 2008 - 06:47 PM

create a class called star that represents a five-pointed star of a particular size and color. Then write a program that creates and draws four stars of various sizes at different locations.

im a starter and i dont know too much about java, so someone plz help me out, tyvm
Is This A Good Question/Topic? 0
  • +

Replies To: question of drawing star using JAVA

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: question of drawing star using JAVA

Posted 16 October 2008 - 06:51 PM

Welcome at DIC.
This is your first post as I can see.
May be wrong forum: we won't do your homework

[rules][/rules]

This post has been edited by pbl: 16 October 2008 - 06:52 PM

Was This Post Helpful? 0
  • +
  • -

#3 suckdog  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-October 08

Re: question of drawing star using JAVA

Posted 16 October 2008 - 07:00 PM

[code]import java.awt.*;
import javax.swing.*;

public class Star extends JApplet
{
public void paint (Graphics g)
{
super.paint( g );

g.setColor( new Color (0,0,0));
g.drawStar(50,75);// this is the step where im having trouble with, so someone plz help me on this
}
}

This post has been edited by suckdog: 16 October 2008 - 07:01 PM

Was This Post Helpful? 0
  • +
  • -

#4 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: question of drawing star using JAVA

Posted 16 October 2008 - 07:16 PM

My guess is that you're supposed to implement a method called drawStar, since there's no such method in the graphics class.

The method should take some parameters, size and color and maybe others like position.

The star can be drawn using Graphics.drawLine method (10 lines). Each line has a source point and a destination point. Take a while to think of how the star looks, what angles are involved. Think about how you can solve this problem using simple trigonometry.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: question of drawing star using JAVA

Posted 16 October 2008 - 07:37 PM

View PostGloin, on 16 Oct, 2008 - 07:16 PM, said:

My guess is that you're supposed to implement a method called drawStar, since there's no such method in the graphics class.

The method should take some parameters, size and color and maybe others like position.

The star can be drawn using Graphics.drawLine method (10 lines). Each line has a source point and a destination point. Take a while to think of how the star looks, what angles are involved. Think about how you can solve this problem using simple trigonometry.


Those sweet Sweeds are always complicating their life for nothing
A star is also a polygone
The Graphics class has a method drawPlolyline() that receives as parameter an array of X points and an array of Y points

This post has been edited by pbl: 16 October 2008 - 07:40 PM

Was This Post Helpful? 0
  • +
  • -

#6 suckdog  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-October 08

Re: question of drawing star using JAVA

Posted 16 October 2008 - 08:07 PM

i have found those points now, can anyone help me modify my program, coz it's not working rite now

import java.awt.*;
import javax.swing.*;

public class Star
{
private int x,y;
private Color color;

public Star(int x1, int y1, Color starColor)
{
x=x1;
y=y1;
color=starColor;
}
public void drawFilled(Graphics gc)
{
g.setColor(green);
g.fillStar(50,75);
}
}
int xPoints[] = { 55, 67, 109, 73, 83, 55, 27, 37, 1, 43 };
int yPoints[] = { 0, 36, 36, 54, 96, 72, 96, 54, 36, 36 };
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: question of drawing star using JAVA

Posted 16 October 2008 - 08:42 PM

Gheez you made a lot of progress... good show
However your code is not complete
And what is the Color paramter that you pass by if you always draw your star in Green ? Anyhow, minor problem.

We do not even see if it is an application or an applet
Please post all your code
and please

:code:
Was This Post Helpful? 0
  • +
  • -

#8 suckdog  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-October 08

Re: question of drawing star using JAVA

Posted 16 October 2008 - 09:25 PM

this is a source sode, can anyone help me to modify it to 4 stars??
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.geom.*;

public class Shapes2 extends JFrame 
	{
	 public Shapes2()
		 {
		 super( "Drawing 2D Shapes" );
		 setBackground( Color.yellow );
		 setSize( 400, 400 );
		 show();
	 }
	
	 public void paint( Graphics g )
		 {
		 int xPoints[] = { 55, 67, 109, 73, 83, 55, 27, 37, 1, 43 };
		 int yPoints[] = { 0, 36, 36, 54, 96, 72, 96, 54, 36, 36 };
		
		 Graphics2D g2d = ( Graphics2D ) g;
		
		 // create a star from a series of points
		 GeneralPath star = new GeneralPath();
		
		 // set the initial coordinate of the General Path
		 star.moveTo( xPoints[ 0 ], yPoints[ 0 ] );
		
		 // create the star--this does not draw the star
		 for ( int k = 1; k < xPoints.length; k++ )
		 star.lineTo( xPoints[ k ], yPoints[ k ] );
		
		 // close the shape
		 star.closePath();
		
		 // translate the origin to (200, 200)
		 g2d.translate( 200, 200 );
		
		 // rotate around origin and draw stars in random colors
		 for ( int j = 1; j <= 20; j++ ) 
			 {
			 g2d.rotate( Math.PI / 10.0 );
			 g2d.setColor(new Color( ( int ) ( Math.random() * 256 ),( int ) ( Math.random() * 256 ), 
			 ( int ) ( Math.random() * 256 ) ) );
			 g2d.fill( star ); // draw a filled star
		 }
	 }
	
	 public static void main( String args[] )
		 {
		 Shapes2 app = new Shapes2();
		
		 app.addWindowListener(new WindowAdapter() 
			 {
			 public void windowClosing( WindowEvent e )
				 {
				 System.exit( 0 );
			 }
		 });
	 }
}


This post has been edited by suckdog: 16 October 2008 - 09:26 PM

Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: question of drawing star using JAVA

Posted 16 October 2008 - 09:35 PM

Cute nice application
Works for me...
What else do you want ?

Only four stars ? You are the one who wrote the code don't you ? Don't tell me you cannot make only 4 stars to appears.... Ho you stealed the code from the Internet and your teacher (who probably monitor this site) wants only 4 stars ?
Was This Post Helpful? 0
  • +
  • -

#10 suckdog  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-October 08

Re: question of drawing star using JAVA

Posted 16 October 2008 - 09:40 PM

as i said, it's other's code, and i wanna modify it to 4 stars
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: question of drawing star using JAVA

Posted 16 October 2008 - 09:48 PM

As you are a looser and a cheater here is a quick and dirty solution to your problem

	// rotate around origin and draw stars in random colors
	for ( int j = 1; j <= 20; j++ ) 
	{
		g2d.rotate( Math.PI / 10.0 );
		if(j % 5 != 0) continue;   // <---- pbl's code
		g2d.setColor(new Color( ( int ) ( Math.random() * 256 ),( int ) ( Math.random() * 256 ), 
					( int ) ( Math.random() * 256 ) ) );
		g2d.fill( star ); // draw a filled star
	}



And do not expect me to answer to any of your future post
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: question of drawing star using JAVA

Posted 16 October 2008 - 09:54 PM

View Postsuckdog, on 16 Oct, 2008 - 09:40 PM, said:

as i said, it's other's code, and i wanna modify it to 4 stars

As you said in which post ?
Was This Post Helpful? 0
  • +
  • -

#13 suckdog  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-October 08

Re: question of drawing star using JAVA

Posted 16 October 2008 - 10:18 PM

im not a looser, and i figured out by my own, here is how i did
for ( int j = 1; j <= 5; j++ ) 
	{
		g2d.rotate( Math.PI / 10.0 );
		if(j % 5 != 0) continue;   // <---- pbl's code
		g2d.setColor(new Color( ( int ) ( Math.random() * 256 ),( int ) ( Math.random() * 256 ), 
					( int ) ( Math.random() * 256 ) ) );
		g2d.fill( star ); // draw a filled star
	}


Was This Post Helpful? 0
  • +
  • -

#14 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: question of drawing star using JAVA

Posted 17 October 2008 - 05:11 AM

Is this PBL writing all the posts for our amusement or is this guy serious?
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: question of drawing star using JAVA

Posted 17 October 2008 - 03:17 PM

View PostGloin, on 17 Oct, 2008 - 05:11 AM, said:

Is this PBL writing all the posts for our amusement or is this guy serious?

If he claims he wrote it better to remove the comment with my name in it :D
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2