Solved

How to draw a hexagon in java

Posted on 2004-09-27
3
4,509 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ejb on wildfly 5 64
sql import cannot be resolved jsp 3 117
Eclipse with various Java releases 7 91
Bitwise and to sum elements 2 54
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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

752 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