Draw Texas Flag

I need some help implementing the drawPolygon function to connect the star points.

This is what I have gotten so far:

<code>

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

public class Texas{
  public static void main(String[] args){

    JFrame frame = new JFrame();
    frame.getContentPane().add(new txflag());
    frame.setSize(500,500);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}
class txflag extends JPanel{
       public void paintComponent(Graphics g){
               super.paintComponent(g);
              int[] x = new int[5];
              int[] y = new int[5];
            
              g.setColor(Color.BLUE);
              g.drawRect(0, 0, 200, 400);
              g.fillRect(0, 0, 200, 400);
            
              g.setColor(Color.WHITE);
              g.drawRect(0, 200, 400, 200);
              g.fillRect(0, 200, 200, 400);
            
              g.setColor(Color.RED);
              g.drawRect(200, 0, 200, 400);
              g.fillRect(200, 0, 200, 100);
            
                 g.setColor(Color.WHITE);
              for (int i = 0; i < 5; i++) {
      
                   x[i] = 100 * Math.cos(Math.PI / 2 + i * 2 * Math.PI / 5) + 100;
                   y[i] = 100 * Math.sin(Math.PI / 2 + i * 2 * Math.PI / 5) + 100;
                  
                  
            }
      }
}

</code>

Thanks
Coconut77840Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
g.drawPolygon(x, y, 5);
0
Coconut77840Author Commented:
I am getting the following error:

java.lang.ClassCastException: Texas
      at sun.applet.AppletPanel.createApplet(Unknown Source)
      at sun.applet.AppletPanel.runLoader(Unknown Source)
      at sun.applet.AppletPanel.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)

and the applet is not initializing.

Thanks
0
objectsCommented:
Its not an applet, its an application.
you run it from the command line using:

java Texas
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Coconut77840Author Commented:
The applet is not loading.

txflag.paintComponent(Graphics) line: 48
txflag(JComponent).paint(Graphics) line: not available
JPanel(JComponent).paintChildren(Graphics) line: not available
JPanel(JComponent).paint(Graphics) line: not available
JLayeredPane(JComponent).paintChildren(Graphics) line: not available
JLayeredPane(JComponent).paint(Graphics) line: not available
JLayeredPane.paint(Graphics) line: not available
JRootPane(JComponent).paintChildren(Graphics) line: not available
JRootPane(JComponent).paintWithOffscreenBuffer(JComponent, Graphics, int, int, int, int, Image) line: not available
JRootPane(JComponent).paintDoubleBuffered(JComponent, Component, Graphics, int, int, int, int) line: not available
JRootPane(JComponent).paint(Graphics) line: not available
GraphicsCallback$PaintCallback.run(Component, Graphics) line: not available
GraphicsCallback$PaintCallback(SunGraphicsCallback).runOneComponent(Component, Rectangle, Graphics, Shape, int) line: not available
GraphicsCallback$PaintCallback(SunGraphicsCallback).runComponents(Component[], Graphics, int) line: not available
JFrame(Container).paint(Graphics) line: not available
RepaintArea.paintComponent(Component, Graphics) line: not available
RepaintArea.paint(Object, boolean) line: not available
WFramePeer(WComponentPeer).handleEvent(AWTEvent) line: not available
JFrame(Component).dispatchEventImpl(AWTEvent) line: not available
JFrame(Container).dispatchEventImpl(AWTEvent) line: not available
JFrame(Window).dispatchEventImpl(AWTEvent) line: not available
JFrame(Component).dispatchEvent(AWTEvent) line: not available
EventQueue.dispatchEvent(AWTEvent) line: not available
EventDispatchThread.pumpOneEventForHierarchy(int, Component) line: not available
EventDispatchThread.pumpEventsForHierarchy(int, Conditional, Component) line: not available
EventDispatchThread.pumpEvents(int, Conditional) line: not available
EventDispatchThread.pumpEvents(Conditional) line: not available
EventDispatchThread.run() line: not available
0
Coconut77840Author Commented:
The application I should have said
0
objectsCommented:
what the first line of the stack trace, ie. what exception is being thrown
and what is at line 48
0
Coconut77840Author Commented:
Line 48: x[i] = 100 * Math.cos(Math.PI / 2 + i * 2 * Math.PI / 5) + 100;

There is exception being thrown.
0
Coconut77840Author Commented:
This is what the program reads.

<quote>

Write a Java program which displays a
correctly-proportioned Texas flag, namely, one with the blue field
twice as
high as it is wide and the red and white fields exactly the same size
as the
blue field.  Hint: For the star, something like

      x[i] = 100 * Math.cos(Math.PI / 2 + i * 2 * Math.PI / 5) + 100;
      y[i] = 100 * Math.sin(Math.PI / 2 + i * 2 * Math.PI / 5) + 100;

will generate the 5 points of the star for i=0 to 4, and then you can
use
drawLine or drawPolygon.  You may need to tweak the 100's to position
the star
correctly.  If you have problems filling the star, you might prefer to
make the
outline of the star a 10-point polygon by changing these formulas.

</quote>

Thank you
0
Coconut77840Author Commented:
I figured that:

     x[i] = 100 * Math.cos(Math.PI / 2 + i * 2 * Math.PI / 5) + 100;
     y[i] = 100 * Math.sin(Math.PI / 2 + i * 2 * Math.PI / 5) + 100;

should be:


    x[i]=(int)(100*Math.cos(Math.PI/2+i*2*Math.PI/5+Math.PI)+125);
    y[i]=(int)(100*Math.sin(Math.PI/2+i*2*Math.PI/5+Math.PI)+250);

I am on my computer and thus I cannot compile it to see whether it does the trick.
Do you think that this would fix the error?

Thanks
0
objectsCommented:
yes that should fix it
(i wouldn't have expected it to compile without it actually)
0
Coconut77840Author Commented:
It's drawing an odd polygon, how can I fix it to draw a star?

<code modified>

              g.setColor(Color.BLUE);
              g.drawRect(0, 0, 200, 400);
              g.fillRect(0, 0, 200, 400);
            
              g.setColor(Color.WHITE);
              g.drawRect(200, 0, 400, 200);
              g.fillRect(200, 0, 400, 200);
            
              g.setColor(Color.RED);
              g.drawRect(200, 200, 400, 200);
              g.fillRect(200, 200, 400, 200);
            
</code modified>

Thanks
0
objectsCommented:
isn't that what your polygon is for?
0
Coconut77840Author Commented:
It is not even drawing a star. I've been told that the drawPolygon function can be used in a way to draw a star.
0
objectsCommented:
see my first comment :)
0
Coconut77840Author Commented:
Your first comment says:

 g.drawPolygon(x, y, 5);

That's exactly what I have, yet it's not drawing the expected star shape.
0
objectsCommented:
sorry, the code u posted above didn't include it so it wasn't clear.
0
Coconut77840Author Commented:
<updated code>

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

public class Texas{
  public static void main(String[] args){

    JFrame frame = new JFrame();
    frame.getContentPane().add(new txflag());
    frame.setSize(700,500);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}
class txflag extends JPanel{
       public void paintComponent(Graphics g){
               super.paintComponent(g);
              int[] x = new int[5];
              int[] y = new int[5];
            
              g.setColor(Color.BLUE);
              g.drawRect(0, 0, 200, 400);
              g.fillRect(0, 0, 200, 400);
            
              g.setColor(Color.WHITE);
              g.drawRect(200, 0, 400, 200);
              g.fillRect(200, 0, 400, 200);
            
              g.setColor(Color.RED);
              g.drawRect(200, 200, 400, 200);
              g.fillRect(200, 200, 400, 200);
            
                 g.setColor(Color.WHITE);
              for (int i = 0; i < 5; i++) {
      
                  x[i] = (int)(100 * Math.cos(Math.PI / 2 + i * 2 * Math.PI / 5) + 100);
                  y[i] = (int)(100 * Math.sin(Math.PI / 2 + i * 2 * Math.PI / 5) + 100);
                  
                   g.drawPolygon(x, y, x.length);
                   g.fillPolygon(x, y, x.length);
            }
      }
}


</code>

Thanks
0
objectsCommented:
what *should* the star look like?
0
objectsCommented:
you should be drawing the poly *outside* your loop

           for (int i = 0; i < 5; i++) {
     
               x[i] = (int)(100 * Math.cos(Math.PI / 2 + i * 2 * Math.PI / 5) + 100);
               y[i] = (int)(100 * Math.sin(Math.PI / 2 + i * 2 * Math.PI / 5) + 100);              
          }
          g.drawPolygon(x, y, x.length);
          g.fillPolygon(x, y, x.length);
0
Coconut77840Author Commented:
This is the Texas star. http://www.texaswallpaper.com/0036-1024.jpg

The one I have looks exactly like a polygon.

Thanks
0
objectsCommented:
looks like u have the points right, they are just in the wroong order.
you need something more like:

          for (int i = 0; i < 5; i++) {
     
               x[(i*2)%5] = (int)(100 * Math.cos(Math.PI / 2 + i * 2 * Math.PI / 5) + 100);
               y[(i*2)%5] = (int)(100 * Math.sin(Math.PI / 2 + i * 2 * Math.PI / 5) + 100);              
          }
          g.drawPolygon(x, y, x.length);
          g.fillPolygon(x, y, x.length);
0
Coconut77840Author Commented:
How do I fill the inner polygon with white color?
0
Coconut77840Author Commented:
Is it possible to rotate the star 180 degree?
0
objectsCommented:
What you really need is the 4 points for the inside of the star.
The hint you were given for drawing the star seems a bit misleading (unless i'm missing something), can you ask your teacher for some clarification on it
0
Coconut77840Author Commented:
How can I re-work this problem using GeneralPath?

I've raised the points to 250 as I figured this could be a new question.
0
Coconut77840Author Commented:

<code>
 Graphics2D g2d = ( Graphics2D ) g;
 GeneralPath star = new GeneralPath();
 star.moveTo( x[ 0 ], y[ 0 ] );
 for ( int k = 1; k < x.length; k++ )
 star.lineTo( x[ k ], y[ k ] );
</code>

am I on the right track here?
0
objectsCommented:
             GeneralPath path = new GeneralPath(new Polygon(x, y, 5));
              path.setWindingRule(GeneralPath.WIND_NON_ZERO );
              ((Graphics2D)g).fill(path);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Coconut77840Author Commented:
Back to the first problem,

I substituted

          g.drawPolygon(x, y, x.length);
          g.fillPolygon(x, y, x.length);

with:

              g.drawLine(x[1],y[1],x[3],y[3]);
              g.drawLine(x[3],y[3],x[0],y[0]);
              g.drawLine(x[0],y[0],x[2],y[2]);
              g.drawLine(x[2],y[2],x[4],y[4]);
              g.drawLine(x[4],y[4],x[1],y[1]);

Now, how do I fill it with white?
0
objectsCommented:
you can't.

did u try the generalpath code i posted above?
0
Coconut77840Author Commented:
Yes, it draws just the outlines, it does not fill the inner polygon.
0
Coconut77840Author Commented:
How can I push that star downward few pixels using GeneralPath?
0
objectsCommented:
>                y[(i*2)%5] = (int)(100 * Math.sin(Math.PI / 2 + i * 2 * Math.PI / 5) + 100);          

increase the value added to y (currently 100)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.