[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2515
  • Last Modified:

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
0
Coconut77840
Asked:
Coconut77840
  • 18
  • 14
4 Solutions
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 18
  • 14
Tackle projects and never again get stuck behind a technical roadblock.
Join Now