setStroke() - Implementation Problem

Hello,

Please, I have tried to increase the stroke size of a particular edge of a polygon but was unable to implement it.

In the code below, I have bothWashington andTokyo in both String Array Elem1 and Elem2. When drawing, the link or line between both cities should be draw with a new bigger stroke size (because they appeared in both Arrays), eg. ,setStroke(new BasicStroke(2)) assume that my initial setStroke() is 1.

Note: In each array, the cities are linked to each other. You can run the program to get a better picture of my explanation.

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;

publicclass DrawCliqueTest{

/**

* @param args

*/

publicstaticvoid main(String[] args){

try{

JFrame frame =new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

BufferedImage image =new BufferedImage(300, 300, BufferedImage.TYPE_INT_RGB);

Graphics2D g = image.createGraphics();

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","Tokyo"};

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

StringElements.add(Elem1);

StringElements.add(Elem2);

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

int []xPoint1 ={150,278};

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

Xlist.add(xPoint1);

Xlist.add(xPoint2);

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

int []yPoint1 ={50,80};

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},newint[]{50, 80}, 2));

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);

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();}

}

}

Thanks,

Jona_T

[6103 byte] By [Jona_Ta] at [2007-11-27 5:38:26]
# 1

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) {

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 = {"Washington","Tokyo"};

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

StringElements.add(Elem1);

StringElements.add(Elem2);

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

int[] xPoint1 = {150,278};

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

Xlist.add(xPoint1);

Xlist.add(xPoint2);

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

int[] yPoint1 = {50,80};

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}, new int[] {50, 80}, 2));

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

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))

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(); }

}

}

crwooda at 2007-7-12 15:12:00 > top of Java-index,Security,Cryptography...
# 2

Hello Crwood,

Thanks for the prompt and correct answer. But I am yet to understand how you came about - if((i == 0 && j == 3) || (i == 1 && j == 0)).

Please can you explain in a Pseudo code or normal description, the reasoning behind it.

I am quite grateful,

Jona_T

Jona_Ta at 2007-7-12 15:12:00 > top of Java-index,Security,Cryptography...
# 3

Please can you explain...the reasoning behind it.

First I looked at your code for drawing the lines and noticed you are using two loops to

draw lines among all coordinates/points in your data structures. It looked like there was a

place to insert a change in Stroke for any line. The question was then how to identify the

lines via loop indices.

Then I looked at your data structures and saw the way.

// i loop index01

String[] Elem1 = {"Washington","Tokyo"};

// j loop index01 23

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

In the i loop you start with index zero which correlates with "Washington". Your line

to "Tokyo" is drawn when the j loop index is 3. The next time a line is drawn between

the two is when the i loop index is one correlating with "Tokyo" and the j

loop is zero correlating with "Washington".

crwooda at 2007-7-12 15:12:00 > top of Java-index,Security,Cryptography...
# 4
Crwood,That is great. Your dukes are on the way.Thanks,Jona_T
Jona_Ta at 2007-7-12 15:12:00 > top of Java-index,Security,Cryptography...