Solved

How to draw a hexagon in java

Posted on 2004-09-27
3
3,833 Views
Last Modified: 2012-05-05
This should be easy, but I'm having a difficult time finding the answer. I'm trying to draw a regular hexagon.

How do I draw a line 100 pixels long, then draw another line that is 100 pixels long at a 120 degree angle from where the first line ended?

If it's not much harder, what is the best way to draw an entire hexagon like this given the absolute center?
0
Comment
Question by:darkpegasus5
  • 2
3 Comments
 
LVL 92

Accepted Solution

by:
objects earned 125 total points
ID: 12165619

try the following:

    Polygon getHexagon(int x, int y, int h)
    {
        Polygon hexagon = new Polygon();

        double a;
        for (int i=0; i < 7; i++)
        {
           double a = Math.PI / 3.0 * i;
           hexagon.addPoint((int)(Math.round(x + Math.sin(a) * h)), (int)(Math.round(y + Math.cos(a) * h)));
        }
        return hexagon;
    }
0
 
LVL 1

Author Comment

by:darkpegasus5
ID: 12166433
That worked perfectly, thanks.
0
 
LVL 92

Expert Comment

by:objects
ID: 12166445
(:
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now