Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

graphing a polygon with double values

Posted on 2006-11-20
3
Medium Priority
?
270 Views
Last Modified: 2008-02-01
I'm looking to generate plotted Point graph output using a graphic library that creates a 2D set of cartesian axes, plots points[i] for i = 0 to points.length-1. Also draws lines between each of points[n] and points[n+1] for n = 0 to points.length-2. Also plot a line from points[points.length-1] to points[0].

Here is my code but it doesn't work....please help!


import java.lang.Math.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.Polygon;
import java.awt.Graphics;

/* more code above but not necessary to show i don't think */

public void draw ( Point[] points, String[] text )
            {
                  x = new long[points.length];
                  y = new long[points.length];
                  tempPoint = new Point[points.length];
                  Graphics g1 = null;


                  for (int i = 0; i < points.length; i++)
                  {
                        x[i] = points[i].x();
                        y[i] = points[i].y();
                        tempPoint[i] = new Point(x[i], y[i]);
                  }

                  LongPolygon g = new LongPolygon(tempPoint);



                  g1.drawPolygon(g);
                  System.out.println(text);
            }      

private class LongPolygon extends Polygon
      {
            private Point[] tempPoints;

            public LongPolygon(Point[] points)
            {
                  tempPoints =  points;
            }
      }            
0
Comment
Question by:shpark82
[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
3 Comments
 
LVL 10

Accepted Solution

by:
ADSLMark earned 2000 total points
ID: 17987233
I have no idea what you want to do, but here is some example code.

--PolygonExample.java
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

class PolygonExample extends JFrame
{
    final static int NUM_POINTS = 10;

    //Constructor
    public PolygonExample()
    {
        //Set frame properties
        this.setSize(800, 600);
        this.setTitle("Polygon Example");
        this.setLayout(null);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        //Fille list with some random points
        Point2D.Double[] points = new Point2D.Double[NUM_POINTS];
        for(int i=0;i<NUM_POINTS;i++)
        {
            double r1 = (Math.random()*20) - 10;
            double r2 = (Math.random()*20) - 10;
            points[i] = new Point2D.Double(r1, r2);
        }

        PolygonGraph pg = new PolygonGraph(points);
        pg.setBounds(200, 100, 400, 400);
        this.add(pg);
    }

    //Program entry
    public static void main(String[] args)
    {
        PolygonExample pe = new PolygonExample();
        pe.setVisible(true);
    }
}

--PolygonGraph.java
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

class PolygonGraph extends JPanel
{
    private Point2D.Double[] points;

    //Screen points/sizes
    final static int GRAPH_WIDTH = 400-1;
    final static int GRAPH_HEIGHT = 400-1;

    final static int GRAPH_ORIGIN_X = GRAPH_WIDTH/2;
    final static int GRAPH_ORIGIN_Y = GRAPH_HEIGHT/2;

    //Real points/sizes
    final static double MIN_X = -10.0;
    final static double MAX_X = 10.0;

    final static double MIN_Y = -10.0;
    final static double MAX_Y = 10.0;

    final static double STEP_SIZE_X = 1.0;
    final static double STEP_SIZE_Y = 1.0;

    //Constructor
    public PolygonGraph(Point2D.Double[] points)
    {
        this.setSize(GRAPH_WIDTH, GRAPH_HEIGHT);

        this.points = points;
    }

    //Override paintcomponent
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        this.draw((Graphics2D) g);
    }

    //Draw graph
    public void draw(Graphics2D g2)
    {
        this.drawGrid(g2);
        this.drawPolygon(g2);
    }

    //Draw polygon
    public void drawPolygon(Graphics2D g2)
    {
        //Set graphics properties
        g2.setColor(Color.BLUE);

        //Split points
        int[] xPoints = new int[points.length];
        int[] yPoints = new int[points.length];

        //Create point lists
        for(int i=0;i<points.length;i++)
        {
            Point p = realToScreen(points[i]);
            xPoints[i] = (int) p.getX();
            yPoints[i] = (int) p.getY();
        }

        //Draw polygon
        g2.drawPolygon(xPoints, yPoints, points.length);
    }

    //Draw grid
    public void drawGrid(Graphics2D g2)
    {
        Point p1; Point p2;
        double gridX; double gridY;

        //Draw vertical lines

        //To the right
        g2.setColor(Color.LIGHT_GRAY);
        g2.setStroke(new BasicStroke(1));
        gridX = 0;
        while(gridX<MAX_X)
        {
            gridX += STEP_SIZE_X;

            p1 = realToScreen(new Point2D.Double(gridX, MAX_Y));
            p2 = realToScreen(new Point2D.Double(gridX, MIN_Y));
            g2.drawLine((int) p1.getX(), (int) p1.getY(), (int) p2.getX(), (int) p2.getY());
        }

        //Center line
        g2.setColor(Color.BLACK);
        g2.setStroke(new BasicStroke(2));
        p1 = realToScreen(new Point2D.Double(0, MAX_Y));
        p2 = realToScreen(new Point2D.Double(0, MIN_Y));
        g2.drawLine((int) p1.getX(), (int) p1.getY(), (int) p2.getX(), (int) p2.getY());

        //To the left
        g2.setColor(Color.LIGHT_GRAY);
        g2.setStroke(new BasicStroke(1));
        gridX = 0;
        while(gridX>MIN_X)
        {
            gridX -= STEP_SIZE_X;

            p1 = realToScreen(new Point2D.Double(gridX, MAX_Y));
            p2 = realToScreen(new Point2D.Double(gridX, MIN_Y));
            g2.drawLine((int) p1.getX(), (int) p1.getY(), (int) p2.getX(), (int) p2.getY());
        }

        //Draw horizontal lines

        //To the top
        g2.setColor(Color.LIGHT_GRAY);
        g2.setStroke(new BasicStroke(1));
        gridY = 0;
        while(gridY<MAX_Y)
        {
            gridY += STEP_SIZE_Y;

            p1 = realToScreen(new Point2D.Double(MAX_X, gridY));
            p2 = realToScreen(new Point2D.Double(MIN_X, gridY));
            g2.drawLine((int) p1.getX(), (int) p1.getY(), (int) p2.getX(), (int) p2.getY());
        }

        //Center line
        g2.setColor(Color.BLACK);
        g2.setStroke(new BasicStroke(2));
        p1 = realToScreen(new Point2D.Double(MAX_X, 0));
        p2 = realToScreen(new Point2D.Double(MIN_X, 0));
        g2.drawLine((int) p1.getX(), (int) p1.getY(), (int) p2.getX(), (int) p2.getY());

        //To the bottom
        g2.setColor(Color.LIGHT_GRAY);
        g2.setStroke(new BasicStroke(1));
        gridY = 0;
        while(gridY>MIN_Y)
        {
            gridY -= STEP_SIZE_Y;

            p1 = realToScreen(new Point2D.Double(MAX_X, gridY));
            p2 = realToScreen(new Point2D.Double(MIN_X, gridY));
            g2.drawLine((int) p1.getX(), (int) p1.getY(), (int) p2.getX(), (int) p2.getY());
        }
    }

    //Convert point from real to screen
    public static Point realToScreen(Point2D.Double p)
    {
        double scaleXFactor = GRAPH_WIDTH  / (MAX_X-MIN_X);
        double scaleYFactor = GRAPH_HEIGHT / (MAX_Y-MIN_Y);

        double translateXFactor = -MIN_X;
        double translateYFactor = -MIN_Y;

        return new Point((int) Math.floor((p.getX()+translateXFactor)*scaleXFactor)
                        ,(int) Math.floor((p.getY()+translateYFactor)*scaleYFactor));
    }
}

I hope it works, I didn't test it thorougly. You should see it as a step in the right direction, you probably have to adjust quite a lot to fit your needs, but the design is flexible.

Good luck.
Mark
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Suggested Courses

661 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