Polygon Line (Stroke Size)

Hello,

A friend in this forum once helped me to sort out a problem in the code below. The problem then was that I wanted to increase the stroke size of polygon lines. The polygon points represent cities. The example then was based on 2 cities and the code worked. Now I increased it to 3 cities but the result is wrong.

package graphic;

import java.awt.Color;

import java.awt.Graphics2D;

import java.awt.Polygon;

import java.awt.image.BufferedImage;

import java.util.*;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JLabel;

import java.awt.geom.*;

import java.awt.BasicStroke;

import java.awt.RenderingHints;

publicclass DrawCliqueTest{

publicstaticvoid main(String[] args){

try{

JFrame frame =new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

BufferedImage image =new BufferedImage(400, 300,

BufferedImage.TYPE_INT_RGB);

Graphics2D g = image.createGraphics();

g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

g.setColor(Color.WHITE);

g.fillRect(0, 0, image.getWidth(), image.getHeight());

g.setColor(Color.BLACK);

List<String[]> StringElements =new ArrayList<String[]>();

String[] Elem1 ={"Wahington","Chicago","London"};

String[] Elem2 ={"Washington","Tokyo","London","Chicago"};

StringElements.add(Elem1);

StringElements.add(Elem2);

List<int[]> Xlist =new ArrayList<int[]>();

int[] xPoint1 ={150,278,250};

int[] xPoint2 ={150,50,250,278};

Xlist.add(xPoint1);

Xlist.add(xPoint2);

List<int[]>Ylist =new ArrayList<int[]>();

int[] yPoint1 ={50,80,203};

int[] yPoint2 ={50,200,203,80};

Ylist.add(yPoint1);

Ylist.add(yPoint2);

ArrayList<Polygon> list =new ArrayList<Polygon>();

list.add(new Polygon(newint[]{150, 278,250},newint[]{50, 80,203}, 3));

list.add(new Polygon(newint[]{150, 50, 250, 278},

newint[]{50, 200, 203, 80}, 4));

//Draw the Strings

for(int i = 0; i < StringElements.size(); i++){

for(int j = 0; j < StringElements.get(i).length; j++){

g.setColor(Color.RED);

g.drawString(StringElements.get(i)[j], Xlist.get(i)[j],

Ylist.get(i)[j]);

}

}

// Draw a line between every pair of points

for (Polygon poly : list){

for (int i = 0; i < poly.npoints-1; i++){

for (int j = i+1; j < poly.npoints; j++){

g.setColor(Color.BLACK);

//if((i == 0 && j == 3) || (i == 1 && j == 0))

if(((i==0 && j==3) ||(i==0 && j==2))||((i==1 && j==0)||(i==1 && j==2))||((i==2 && j==0 ||(i==2 && j==3))))

g.setStroke(new BasicStroke(2f));

else

g.setStroke(new BasicStroke(1f));

g.drawLine(poly.xpoints[i], poly.ypoints[i],poly.xpoints[j], poly.ypoints[j]);

}

}

}

g.dispose();

frame.add(new JLabel(new ImageIcon(image)));

frame.pack();

frame.setVisible(true);

}catch(Exception e){ e.printStackTrace();}

}

}

The program is meant to check the elements in each array of the ArrayList "StringElements" and get the strongly connected cities (the same cities that are in both arrays; Elem1 and Elem2. .

If you run the code, it draws the connections between London->Washington, London->Chicago and London->Tokyo with a stroke size 2 and the rest with stroke size 1. But, London->Tokyo is not suppose to be drawn with stroke size 2 because Tokyo is not in Elem1.

My plans is to implement this line of the code which is my problem point: if(((i==0 && j==3) ||(i==0 && j==2))||((i==1 && j==0)||(i==1 && j==2))||((i==2 && j==0 ||(i==2 && j==3))))

dynamically but the static version is not even working.

Any solution or idea on how to overcome this problem?

Thanks,

Jona_T

[7353 byte] By [Jona_Ta] at [2007-11-27 10:12:58]
# 1

Hello Friends,

I went through the line of code where my problem is and was able to detect my mistake. The line code should read; if((i==0 && (j==3 || j==2))||(i==2 && (j==3)))

. With this, I can get the correct result.

But my problem is still how to implement it dynamically so that it can take various numbers of cities that appear both in Elem1 and Elem2 respectively.

Thanks,

Jona_T

Jona_Ta at 2007-7-28 15:23:51 > top of Java-index,Security,Cryptography...
# 2

import java.awt.Color;

import java.awt.Graphics2D;

import java.awt.Polygon;

import java.awt.image.BufferedImage;

import java.util.*;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JLabel;

import java.awt.geom.*;

import java.awt.BasicStroke;

import java.awt.RenderingHints;

public class DCT {

public static void main(String[] args) {

JFrame frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

BufferedImage image = new BufferedImage(400, 300,

BufferedImage.TYPE_INT_RGB);

Graphics2D g = image.createGraphics();

g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

g.setColor(Color.WHITE);

g.fillRect(0, 0, image.getWidth(), image.getHeight());

g.setColor(Color.BLACK);

List<String[]> StringElements = new ArrayList<String[]>();

String[] Elem1 = {"Washington","Chicago","London"};

String[] Elem2 = {"Washington","Tokyo","London","Chicago"};

StringElements.add(Elem1);

StringElements.add(Elem2);

List<int[]> Xlist = new ArrayList<int[]>();

int[] xPoint1 = {150,278,250};

int[] xPoint2 = {150,50,250,278};

Xlist.add(xPoint1);

Xlist.add(xPoint2);

List<int[]>Ylist = new ArrayList<int[]>();

int[] yPoint1 = {50,80,203};

int[] yPoint2 = {50,200,203,80};

Ylist.add(yPoint1);

Ylist.add(yPoint2);

ArrayList<Polygon> list = new ArrayList<Polygon>();

list.add(new Polygon(new int[] {150, 278, 250},

new int[] { 50, 80, 203}, 3));

list.add(new Polygon(new int[] {150, 50, 250, 278},

new int[] { 50, 200, 203, 80}, 4));

//Draw the Strings

for(int i = 0; i < StringElements.size(); i++) {

for(int j = 0; j < StringElements.get(i).length; j++) {

g.setColor(Color.RED);

g.drawString(StringElements.get(i)[j], Xlist.get(i)[j],

Ylist.get(i)[j]);

}

}

// Draw a line between every pair of points from one polygon to the next.

for(int k = 0; k < list.size()-1; k++) {

Polygon orig = list.get(k);

Polygon dest = list.get(k+1);

for (int i = 0; i < orig.npoints; i++) {

for (int j = 0; j < dest.npoints; j++) {

// Don't draw a line if orig and dest are the same.

if(StringElements.get(k)[i].equals(StringElements.get(k+1)[j])) {

//System.out.printf("equality for %s and %s%n",

//StringElements.get(k)[i],

//StringElements.get(k+1)[j]);

continue;

}

//System.out.printf("xpoints[%d] = %d ypoints[%d] = %d " +

//"xpoints[%d] = %d ypoints[%d] = %d%n",

//i, orig.xpoints[i], i, orig.ypoints[i],

//j, dest.xpoints[j], j, dest.ypoints[j]);

g.setColor(Color.BLACK);

// Use heavy Stroke if route is duplicated.

if(isDuplicated(i, j, k, k+1, StringElements))

g.setStroke(new BasicStroke(2f));

else

g.setStroke(new BasicStroke(1f));

g.drawLine(orig.xpoints[i], orig.ypoints[i],

dest.xpoints[j], dest.ypoints[j]);

}

}

}

g.dispose();

frame.add(new JLabel(new ImageIcon(image)));

frame.pack();

frame.setVisible(true);

}

private static boolean isDuplicated(int i, int j, int origIndex, int destIndex,

List<String[]> StringElements) {

//System.out.printf("i = %d j = %d%n", i, j);

String[] origCities = StringElements.get(origIndex);

String orig = origCities[i];

//System.out.println("orig = " + orig);

String[] destCities = StringElements.get(destIndex);

String dest = destCities[j];

//System.out.println("dest = " + dest);

// If we find the dest city in the origin array and

// the origin city in the dest array return true.

return contains(origCities, dest) && contains(destCities, orig);

}

private static boolean contains(String[] circuit, String city) {

for(int j = 0; j < circuit.length; j++) {

if(city.equals(circuit[j]))

return true;

}

return false;

}

}

crwooda at 2007-7-28 15:23:51 > top of Java-index,Security,Cryptography...
# 3

Crwood,

Thanks for the answer and respect for your java2D know-how.

Jona_T

Jona_Ta at 2007-7-28 15:23:51 > top of Java-index,Security,Cryptography...
# 4

Hello Crwood,

I have a little problem trying to adapt and extend your code. I increased the number of Arrays in the StringArray "StringElements" to 10 and also implemented a method that checks and assigns each city that appears in different arrays (Elem1 to Elem10) the same xpoint and ypoint, so, New York will have the same xpoints and ypoints. I then tried to modify how lines are draw between the cities but had problem. These portions of the code; // Draw a line between every pair of points from one polygon to the next.

for(int k = 0; k < list.size()-1; k++) {

Polygon orig = list.get(k);

Polygon dest = list.get(k+1);

for (int i = 0; i < orig.npoints; i++) {

for (int j = 0; j < dest.npoints; j++) {

and

g.drawLine(orig.xpoints[i], orig.ypoints[i],

dest.xpoints[j], dest.ypoints[j]);

takes the elements in two different arrays of the ArrayList and draw lines among them. This is wrong. It should only draw lines among elements in the same array (Elem1 or Elem2) and not between (Elem1 and Elem2), e.g, between (Washington, Chicago and London). So the idea is, firstly, to draw lines among the cities in Elem1 and thereafter go to Elem 2, check if it has more than 1 duplicates when compared with Elem1, draw lines among the cities but the duplicates with a bigger stroke size.

Based on the information I gave earlier, the last implementation was OK only that it draws lines among cities in different arrays instead of the same array. Is it possible correct it?

Thanks,

Jona_T

Jona_Ta at 2007-7-28 15:23:51 > top of Java-index,Security,Cryptography...
# 5

import java.awt.Color;

import java.awt.Graphics2D;

import java.awt.Polygon;

import java.awt.image.BufferedImage;

import java.util.*;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JLabel;

import java.awt.geom.*;

import java.awt.BasicStroke;

import java.awt.RenderingHints;

public class DCT {

public static void main(String[] args) {

JFrame frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

BufferedImage image = new BufferedImage(400, 300,

BufferedImage.TYPE_INT_RGB);

Graphics2D g = image.createGraphics();

g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

g.setColor(Color.WHITE);

g.fillRect(0, 0, image.getWidth(), image.getHeight());

g.setColor(Color.BLACK);

List<String[]> StringElements = new ArrayList<String[]>();

String[] Elem1 = {"Washington","Chicago","London"};

String[] Elem2 = {"Washington","Tokyo","London","Chicago"};

StringElements.add(Elem1);

StringElements.add(Elem2);

List<int[]> Xlist = new ArrayList<int[]>();

int[] xPoint1 = {150,278,250};

int[] xPoint2 = {150,50,250,278};

Xlist.add(xPoint1);

Xlist.add(xPoint2);

List<int[]>Ylist = new ArrayList<int[]>();

int[] yPoint1 = {50,80,203};

int[] yPoint2 = {50,200,203,80};

Ylist.add(yPoint1);

Ylist.add(yPoint2);

ArrayList<Polygon> list = new ArrayList<Polygon>();

list.add(new Polygon(new int[] {150, 278, 250},

new int[] { 50, 80, 203}, 3));

list.add(new Polygon(new int[] {150, 50, 250, 278},

new int[] { 50, 200, 203, 80}, 4));

//Draw the Strings

for(int i = 0; i < StringElements.size(); i++) {

for(int j = 0; j < StringElements.get(i).length; j++) {

g.setColor(Color.RED);

g.drawString(StringElements.get(i)[j], Xlist.get(i)[j],

Ylist.get(i)[j]);

}

}

// Draw a line between every pair of points in each polygon.

for(int i = 0; i < list.size(); i++) {

Polygon poly = (Polygon)list.get(i);

for (int j = 0; j < poly.npoints; j++) {

for (int k = j+1; k < poly.npoints; k++) {

g.setColor(Color.BLACK);

// Use heavy Stroke if route is duplicated.

if(isDuplicated(i, j, k, StringElements))

g.setStroke(new BasicStroke(2f));

else

g.setStroke(new BasicStroke(1f));

g.drawLine(poly.xpoints[j], poly.ypoints[j],

poly.xpoints[k], poly.ypoints[k]);

}

}

}

g.dispose();

frame.add(new JLabel(new ImageIcon(image)));

frame.pack();

frame.setVisible(true);

}

private static boolean isDuplicated(int elementIndex, int origIndex,

int destIndex, List<String[]> StringElements) {

// Look for cities in StringElements.get(elementIndex) located

// at [origIndex] and [destIndex] in the other StringElement arrays.

String[] elementCities = StringElements.get(elementIndex);

String orig = elementCities[origIndex];

//System.out.println("orig = " + orig);

String dest = elementCities[destIndex];

//System.out.println("dest = " + dest);

// If we find both the orig and dest cities in any other

// element of StringElements return true.

for(int j = 0; j < StringElements.size(); j++) {

if(j == elementIndex)

continue;

String[] element = StringElements.get(j);

if(contains(element, orig) && contains(element, dest))

return true;

}

return false;

}

private static boolean contains(String[] circuit, String city) {

for(int j = 0; j < circuit.length; j++) {

if(city.equals(circuit[j]))

return true;

}

return false;

}

}

crwooda at 2007-7-28 15:23:51 > top of Java-index,Security,Cryptography...
# 6

Hello Crwood,

the problem is fixed. Thanks a lot.

Jona_T

Jona_Ta at 2007-7-28 15:23:51 > top of Java-index,Security,Cryptography...