Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a Flag

Status
Not open for further replies.

tampa

Programmer
Nov 17, 2002
3
DE
I am very new to Java and I am currently writing a program that should display the US Flag along with the originals 49 stars. I am attaching what I have....are there any ideas so I can get this project completed....I am using a book called teach yourself Java 2

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

////////// Flag

public class Flag

{
///////main
public static void main (String [] args)
{
JFrame windo = new FlagWindow();
windo.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
windo.setVisible(true);
}//end class Flag

class FlagWindow extends JFrame {

FlagWindow() {
Container content = this.getContentPane();
content.setLayout(new BorderLayout());
MyDrawing drawing = new MyDrawing ();
content.add(drawing, BorderLayout.CENTER);
this.setTitle("The Freedom Flag");
this.pack();

}//end settings

}//endclass FlagWindow

class MyDrawing extends JPanel {
////Constructor
MyDrawing (){

public void paintComponent(Graphics g)
{

super.paintComponent(g);
setPreferredSize(new Dimension(900, 700)); //screen size
this.setBackground(Color.black);
int w = getWidth( );
int h = getHeight();
double w1 = (2/3)*w; //flag width
double h1 = (2/3)*h; // flag height
double stripesHeight = (h1/13); //dividing the screen into equal number of stripes
double y = stripesHeight;





g.fillRect(w/6. h/6. (2/3)*w. (2/3)*h);
g.setColor(Color.red);

while

(y < (5/6) *w);

{

g.fillRect(w/6. (h/6. + y). (2/3)*w. (2/3)*h);
g.setColor(Color.white);
y = y + (2 stripesHeight);
}//end constructor

}//end paintComponent
}//endclass MyDrawing

 
I got it to work....however, it seems that my stars look more like round little circles....I am again attaching the program....if anyone have any idea about creating better stars....their welcome would be greatly appreciated...in addition, I would love any ideas about making the flag wave...that would be really cool for my third programming project....

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

////////// Flag

class Flag
{
///////// main
public static void main(String[] args)
{
JFrame windo = new FlagWindow();
windo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
windo.setVisible(true);
}//end main
}//end Flag

///////////FlagWindow

class FlagWindow extends JFrame {

//////Constructor

FlagWindow() {

Container content = this.getContentPane();//get content pane
content.setLayout(new BorderLayout());//set its layout
MyDrawing drawing = new MyDrawing();//Create a drawing
content.add(drawing, BorderLayout.CENTER);//center expands

this.setTitle(&quot;The United States Flag&quot;);
this.pack();
}//end constructor
}// end class Flag

//////// class MyDrawing
//this class extends JPaneland overrides paintcomponent to
//create a component for drawing - in this case the U.S. flag

class MyDrawing extends JPanel
{
///////Constructor

MyDrawing()
{
setPreferredSize(new Dimension(800, 400));//initial screen sizes
setBackground(Color.white);//sets the background color
}
public void paintComponent(Graphics g) {
super.paintComponent(g);

int w = getWidth();
int h = getHeight();
int stripeHeigth = h*4/65;
int y = stripeHeigth;
int x;
g.setColor(Color.red);//setting red stripes across the flag
g.fillRect(w/10, h/10, w*4/5, h*4/5);
g.setColor(Color.white);//setting white stripes across the flag

while (y < h*7/10) {
g.fillRect(w/10, h/10 + y, w*4/5, h*4/65);
y = y + (stripeHeigth*2);
}
g.setColor(Color.blue);//setting a blue panel to be filled with stars
g.fillRect(w/10, h/10, w*8/25, h*28/65);
g.setColor(Color.white);///color to be used for 50 stars
x = h*85/650;
y = w/10+w*28/2325;
while (y < w*105/250) {
g.fillOval(y, x, w*4/200, w*4/200);
x += h*13/155;
g.fillOval(y, x, w*4/200, w*4/200);
x += h*13/155;
g.fillOval(y, x, w*4/200, w*4/200);
x += h*13/155;
g.fillOval(y, x, w*4/200, w*4/200);
x += h*13/155;
g.fillOval(y, x, w*4/200, w*4/200);
y+= w*128/2325;
x = h*85/650;
}
y = w/10+w*88/2325;
x = h*85/650+h*13/310;
while(y < w*95/250) {
g.fillOval(y, x, w*4/200, w*4/200);
x += h*13/155;
g.fillOval(y, x, w*4/200, w*4/200);
x += h*13/155;
g.fillOval(y, x, w*4/200, w*4/200);
x += h*13/155;
g.fillOval(y, x, w*4/200, w*4/200);
y+= w*128/2325;
x = h*85/650+h*13/310;
}


}
}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top