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
Solved

How can I use drawPolygon with decimal numbers?

Posted on 2003-10-29
5
299 Views
Last Modified: 2006-11-17
I am very new to this object oriented type programming, most of what I have done is in PERL, so forgive my ignorance.

I am using drawPolygon just fine with whole numbers. I need to introduce numbers with decimals in to the routine. How would I go about this? I've tried changing the int to double and float, but keep getting errors during the compile.
Thanks
-Dale
0
Comment
Question by:dalehanzelka
  • 3
5 Comments
 
LVL 15

Expert Comment

by:jimmack
ID: 9646728
You just need to cast your doubles (or floats) before passing them into the method.  Unfortunately, I don't *think* there's a way to simply cast an array of doubles to an array of ints, like this:

double[] myDoubles = {1.1, 2.2, 3.3};
int[] myInts = (int[])myDoubles;

In this case, you would need to do:

int[] myInts = new int[myDoubles.length];
for (int i = 0; i < myDoubles.length; i++)
{
    myInts[i] = (int)myDoubles[i];
}

For the last parameter, I'm guessing that your number of points is already an int ;-)
0
 
LVL 4

Accepted Solution

by:
delphi3 earned 250 total points
ID: 9651080
Hi dalehanzelka,

If this is of any help in using decimals to draw geometric shapes??

Here is an application that draws a 2 figures with some decimals in one of them that I happen
to have in my files.

And it is not pure OOP  but has some math with it.

Delphi3



package applicationpolygontest;

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

public class polygonApplic {
  public polygonApplic() {
  }

  public static void main(String[] args) {
    polygonApplic polygonApplic1 = new polygonApplic();
    PolygonFrame frame = new PolygonFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.show();
  }
}

/**
   A frame with a message panel
 */
class PolygonFrame
    extends JFrame {
  public PolygonFrame() {
    setTitle("Polygon Test");
    setSize(WIDTH, HEIGHT);
    // add panel to frame
    PolygonPanel panel = new PolygonPanel();
    Container contentPane = getContentPane();
    contentPane.add(panel);
  }

  public static final int WIDTH = 200;
  public static final int HEIGHT = 300;
}

/**
   A panel that shows  polygons
 */
class PolygonPanel
    extends JPanel {

  public void paint(Graphics g) {
    // a triangle
    int factor = 12; // an enlargement factor
    int[] TriPts = {
        1 * factor, 1 * factor, 3 * factor, 4 * factor, 2 * factor, 6 * factor};
    g.setColor(Color.red);

    g.drawLine(TriPts[0], TriPts[1], TriPts[2], TriPts[3]);
    g.drawLine(TriPts[2], TriPts[3], TriPts[4], TriPts[5]);
    g.drawLine(TriPts[4], TriPts[5], TriPts[0], TriPts[1]);

    // a pentagon
    int PentaPts = 6;
    int PentaPtsX[] = new int[PentaPts];
    // since it is a circular motion here calling an extra to close up the figure
    int PentaPtsY[] = new int[PentaPts];
    g.setColor(Color.blue);
    int rad = 50; // an enlargement factor
    int x1, y1, x2, y2;
    for (int i = 0; i < PentaPts; i++) {
      System.out.println(i + ". " + "Cos X = " +
                         rad * Math.cos(2 * Math.PI / 5 * i));
      System.out.println(i + ". " + "Sine Y = " +
                         rad * Math.sin(2 * Math.PI / 5 * i));

      x1 = (int) (rad * Math.cos(2 * Math.PI / 5 * i));
      y1 = (int) (rad * Math.sin(2 * Math.PI / 5 * i));
      PentaPtsX[i] = x1;
      PentaPtsY[i] = y1;
    }
    System.out.println(" ");
    // Translate to center at 100,150;
    for (int ic = 0; ic < PentaPts - 1; ic++) {
      if (ic == 0) {
        g.setColor(Color.red);
      }
      if (ic == 1) {
        g.setColor(Color.blue);
      }
      if (ic == 2) {
        g.setColor(Color.green);
      }
      if (ic == 3) {
        g.setColor(Color.ORANGE);
      }
      if (ic == 4) {
        g.setColor(Color.MAGENTA);
      }
      g.drawLine(100 + PentaPtsX[ic], 150 + PentaPtsY[ic],
                 100 + PentaPtsX[ic + 1], 150 + PentaPtsY[ic + 1]);
    }
    g.setColor(Color.CYAN);
    g.drawOval(50, 100, 100, 100);
  }
}
0
 

Author Comment

by:dalehanzelka
ID: 9673305
I found that using the graphics2D g2, I could accomplish what I needed, but your suggestion did work also.

Thanks,
-Dale
0
 
LVL 4

Expert Comment

by:delphi3
ID: 9673540

Glad to help.

Delphi3
0
 
LVL 4

Expert Comment

by:delphi3
ID: 9674270
dalehanzelka,

Thanks for the points and grade.

Delphi3
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
netstat -ano | find "8000" and taskkill /f /pid 2984 3 80
fibonacci ten numbers 4 45
SequenceInputStream example 3 19
restrict decimal places for double datatype 10 19
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

765 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