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;
public class DrawCliqueTest {
     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 = {"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(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
               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, 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

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

Similar Messages

  • HPLJ 4250 printing black line inch size across top of page.

    HPLJ 4250 printing black line inch size across top of page.  The Maintenance Kit and the toner have been changed, the option in the menu to clean fuser has also been done.    The black line is intermittent so it hard to work out what to do next.

    Thanks for the tip Mikey - however the same thing happens with the thin black lines when I print on 'normal' A3 paper too. On normal A4 paper it's fine, no lines, but when I print to A3 I get the lines - on both kinds of paper.
    I've attached a couple of (awful-looking) scans to help illustrate my point.

  • Filling Flex chart bars with line strokes

    HI all,
    I have requirement to fill the columns of column chart with line strokes any suggestions ??..plz put here..

    Try BitmapFill or a custom fill.

  • How to I add lines (strokes) to the pieces of a 3-D pie chart

    When I select the individual pieces, the stroke option will not let me put a line outline, only choose a color. new to keynote sorry if this is really basic. thanks

    Unfortunately, that's one of the things Keynote can't do. If you don't have too many charts, you could try adding strokes manually using the pen in the shapes tool. Sorry for not being able to help.

  • I have a line (stroke, no fill) and an eps graphic. I'm trying to make a clipping mask of the single line art but all it does is cut the eps graphic instead of making a mask?

    I have an eps graphic and a single line drawing 2 pt wt. I'm trying to make a clipping mask of the line drawing. When I tried it all that happened was the eps graphic was cut in half. I did the exact steps from AdobeTV except the line drawing [pink camera.ai*@1200% (CMYK/Outline)] is an ai pen line drawing instead of a vector graphic and the other is [vector flower lillies.eps @ 100% (RGB/Preview)]. If you need more info on the graphics please tell me where to find it and I will send it.
    This is all because the eps graphic has a white background. I have tried everything I know including opening it in Photoshop and deleting the background and it always pastes with a white background. My thought was if I can't change the background then maybe I can make a clipping mask of the pink camera and just paste it in front.
    Please help this has been a thorn in my side for too long...

    please post a screen shot of the graphic(s) as it's hard to understand exactly what you are asking
    IF these are 'vector' graphics there should be no need for a clipping mask--also--if the .eps files are small (MB or KB) try posting one of those

  • How to view browser history off-line, full size

    hello - i am able to search for and locate web pages i visited many months ago - however i don't know how to view them as they were then.  for instance, one was a blog with links i tried to revisit, so i found it, but the blog no longer exists.  i figure that since the text and image were saved (obviously i was able to search for the text and view a thumbnail) they are probably available somehow "offline" - how can i view pages visted off-line rather than revisiting them?  thanks! 

    Assuming you are using Safari as your browser, in it's general preferences, there's a "Remove history items" menu popup which gives you choices of when to delete history.  Included in that list is "manually" if you so choose.  Other browsers have similar options (e.g., firefox).
    As for what's kept, it's url's, not whole web pages.  So if a url is old enough there is a probability that the page it is pointing to changed or no longer exists. 

  • How to get inner or outer stroke for an unclosed line?

    Hi,
    I've been struggling to use stroke alignment for unclosed lines in Illustrator CC 2014. It is not possible as I can see, only centered is active.
    First of all, is there any normal miracle way to do that?
    Secondly, I've seen some scripts that can change Illustrators coordinate system(Instead using Top-Bottom coordinate system, it can allow to use Top-Right) So, maybe it is possible to change stroke alignment by coding? is there anyone who know something about that?
    (My trouble is, I have 120 shapes drawn by me before not knowing the alignment restriction. Currently, I am in a deep trouble to use those icons with different stroke sizes because, icon's size is been changed)
    thank you by now,
    regards.

    @rcraighead Monika Gause thank you for your help!

  • Does the Bone tool work with strokes created using the pencil or line tools?

    I have a working bone rig in a document(CS4) that I created using a SINGLE pencil stroke, and applying the armature to it.  Object drawing was off and it was a single piece with no part being a  symbol.  This was accomplished TWICE for two stick figure arms.
    However, when I tried the same thing in a new document, I couldn't apply bone to the pencil stroke or the line stroke.  After going back into the earlier document(where it was still functional) I tried creating a new armature using the exact same technique.  But this time Flash wouldn't allow it.
    Having failed every additional attempt, I'm wondering if Flash may have glitched somehow the first time, allowing this to work when, typically, it doesn't.  Or could I have possibly missed a step somewhere that caused it to fail each attempt at a recreation.

    I agree, it is sometimes difficult to click the sweet spot. Here are 3 suggestions:
    Pay close attention to the cursor icon. It will change when you're over an anchor or direction point.
    Try turning on "Smart Guides". They can be a nuisance, but sometimes they're helpful.
    Use the "Convert Buttons" on the Options bar. They may take a bit longer but they're full-proof, plus, you can convert or delete multiple selected points simultaneously using this method.

  • Creating die lines in illustrator

    I have been doing some research on creating die lines in illy but I have some unanswered questions: (using a standard box as an example)
    1. If I want to indicate a fold (dotted line) at the base of a tab, do I create a dotted stroke across the fold, leaving an open path with the end anchor points overlapping the shape of the tab at the base?
    2. Is the die line for a basic box one shape (closed path) with fold line strokes (open paths) overlapping the shape? Or are packages made from a bunch of different shapes (individual closed paths) all touching each other (overlapping sides) where each shape would be a section of the box?
    4. Do the fold lines go on a separate layer from the cut lines?
    I haven't been able to find a tutorial to create the die line from scratch by just going off measurements, so my questions are in regards to using the pen tool and shapes to get to the final image of the die.
    Thanks!

    1. Yes
    2. Yes one shape is preferred. Often dies from Autocad do not have the paths conneceted and we have to connect them.
    4. No the fold lines do not have to go on a seperate layer, use a global color for folds (also bleed &  trim). If someone wants the folds on a separate layer , they can select the strokes fo similar color and easily do that.
    Make sure use preview bounds is off, or else your stroke thickness will be calculated into your sizes in the tranform palette, and throw your die off by half the thickness of your stroke. .5pt is often preferred for die line thickness, unless large scale itmes can go to 1 pt.

  • Making a straight line aligned with text and adjusting length

    i am trying to make a simple straight line above some text in after effects cs5 that lines up with the beginning and end of the text. Not touching or anything. Nothing special. then a line perpendicular to it of a different stroke size.
    Also i want it to draw or fill in.
    first of all, if i dont get the line length perfect the first time, and i try to adjust the length, then the stroke width changes, also the scale.
    this is maddening because if i am making other pieces that i will animate together, i want them to be the same. and every time i try to adjust the line length, it changes everything...
    or do i just make all lines and type graphics in illustrator or photoshop and import them...and make a mask.  I almost think that all those extra steps might be easier...(sarcasm)
    any tips on this as this super basic task is turning into a headache?
    thx in advance.

    Well, because if you dont get it exactly correct and you want to make it longer or shorter, not only does that change, but the stroke starts scaling. So now you have simply a shorter line, you have a line that appears the same but is inevitably different point sizes and different scales that are maddening to match. I am animating several iterations of a kind of type/line template in 3d space. So i have made each on its own composition that is not 3d.
    i thought you would be able to just click on the end of the line/path and make the line itself shorter or longer like in illustrator. Also when the scale changes, it changes its position slightly from its anchor point....

  • How do I make a line graph that shows temperature readings taken on specific dates?

    I need to make a chart of my wife's temperatures taken via ovulation thermometer. All I need is a line graph that shows the temperatures on the y axis and the dates the measurements were taken on the x axis.
    What is the easiest way to do this? Please explain in detail as I have almost no knowledge regarding Numbers. Thanks.

    You can construct a Line graph or a Scatter graph to display this.
    The two are similar, but require the data to be set up differently.
    A Line graph is a Category graph.
    Each 'date' is a text value that names a category. The categories are equally spaced along the category axis. Measurements taken on day 1, day 2 and day 4 would be equally spaced with respect to the category axis.
    Each temperature reading is a numerical value, and is represented on the graph by the distance it is placed from (and usually above) the category axis.
    A Scatter graph has two value axes.
    Each Date is a Date and Time value. The position of these values depends on the value itself. Measurements taken on February 1, February 2 and February 4 wound not be equally spaced. Along the Date axis, the space between the second and third dates would be twice as wide as that between the first and second cates.
    Each temperature reading is a numerical value, and is represented on the graph in the same manner as in the Line graph.
    Here's an example of each, using the same data set.
    For the Line graph (left), the Category labels (dates) are placed in a Header column, and the temperatures in a non-header column,
    For the Scatter graph, both the dates and the temperatures are in non-header columns. The Chart Inspector was used to Connect the data points with straight line segments, and the weight of these lines (and size of the data point markers) was increased to more closely match the weights and sizes in the Line graph.
    Data on the graphs is not intended to have any similarity to temperatures measured for your purposes.
    The construct the line graph:
    Set up the data and labels as described.
    Select the cells containing the data (column B in my case).
    Click the Chart button and choose the Line Chart.
    To construct the scatter chart:
    Set up the data as described.
    Select the cells containing the date and temperature data (columns A and B for mine)
    Click the Chart button, and select the Scatter Chart button (third from the bottom),
    In the Chart inspector, click the Series button, then set the Connection Points menu to Straight. Change the Data Point size yo 15.
    Click anywhere on the line to select it, then in the Stroke section of the Format bar, set the line weight to 4 points.
    Click on the box containing the dates (under the chart), then click the ruler icon in the Inspector to show the metrics inspector. Use the Rotate control to set the angle for the dates to about 75°.
    Click on the label for this axis (Dates) and drag it to a position below the date labels.
    Regards,
    Barry

  • [PS6]Control width of bezier line

    Using [PS6], how can I make the line left by the pen tool heavier?
    I've been pounding away with google and PS6 manual... but must be using off the wall search stings or something because I'm getting piles of info about bezier curves and using the pen tool but no mentions of making the damn line at least visible.
    Even on High desktop sizes that darn line, if anywhere near any other border or line, is nearly invisible.
    I'd like a line several pixels wide... but not seeing how that is done.

    conroy wrote:
    "PS6" normally means Photoshop 6 which must be more than a decade old. Maybe you mean CS6.
    If CS6 then you could use the Pen Tool in "Shape" mode with no Fill and a coloured Stroke with the weight that you prefer. The controls for that are in Options bar when the tool is active.
    Yes, CS6, setting shape mode, no file and set stroke size was just the ticket.  Thank you

  • Drawling a rectangle creates a Giant rectangle instead of the size I want

    Whenever I try to draw a rectangle/square using the Rectangle Tool, I end up creating a GIANT rectangle instead of size I want (something closer to 400x80 px).
    Here's what I am doing:
    create new layer from Layers Panel
    select Rectangle Tool
    click and hold click to start drawling the rectangle > immediately the coordinate are something like x: -8800 y: -1550 (see pic1)
    if I release the click now, I end up drawling a GIANT rectangle (see pic2)
    now I have to zoom way really far inorder to see the edges of rectangle and be able to resize
    This only happens when I use the Rectangle Tool, not the Rounded Rectangle, Elipse, Polygon, Line or Custom Shapes Tools.
    Any ideas on what I'm doing wrong here? I really appreciate your help!
    I'm using the latest version of Photoshop on a brand new iMac.

    Hi,
    Perhaps under Geometry Options you have fixed size or one of the other options besides unconstrained ticked with a preset size entered?
    You probably want it set to Unconstrained.

  • Aggregate Polygons

    Does anyone know of tool that moves polygons toward each other so that they are all adjacent to one another in a grouping? For example, say you have a bunch of polygons in a layer scattered across your screen and the tool would automatically move all the polygons so that they retain their shape and size, but are now adjacent to each other, preferably edges touching. It doesn't have to be super accurate I just want all my scattered polygons to line up with each other, sort of like the game Tetris.
    This doesn't necessarily have to be done in Illustrator, I'm just casting a wide net to see what tools are out there. Perhaps there's something in AutoCAD that can do this. Also, hopefully this question makes sense. My background is in GIS and I don't have too much experience with Illustrator.

    Not getting the results I wanted. I have about 1000 odd shaped polygons of various size.
    Original:
    Output:
    Perhaps if I only select 10-20 polygons at a time?

  • How to make line thicker?

    When I use "g.drawLine()" to draw a line,the size of the line is fixed.But I would like to make the line thicker.How to do that?Thanks!

    one of my favorite methods :)
        void drawThickLine(Graphics2D g, double x1, double y1, double x2, double y2, double width) {
            Stroke orig = g.getStroke();
            BasicStroke wideStroke = new BasicStroke((float)width);
            g.setStroke(wideStroke);
            g.draw(new java.awt.geom.Line2D.Double(x1,y1,x2,y2));
            g.setStroke(orig);
        }Enjoy!

Maybe you are looking for

  • How can I stop Pages from changing the date of a document?

    It seems that at some point not long ago, I set Pages to use the current date when I use a template that I made.  But now, when I open an existing document that was made with that template, the date is changed to the current date.  I certainly do not

  • Currency conversion with variable in selection mask with problems

    Hi Gurus, I want to implement a currency conversion as follows: when the query is opened a selection screen should appear and the whole data which is with Euro currency should be possible to translate in USD, GBP, FRF (depends on which currency the u

  • IPhoto 6 and TiVo sharing

    I have always been able to view my iPhotos thru my TiVo. Now with iPhoto 6 I cannot. I have opened the TCP port on my firewall, I have enabled sharing in iPhoto and in shapring preferences. My TiVo gets to the internet, and I can ping it from my comp

  • Please Advice the following

    Dear friends, Can anyone show me a Tutorail or so. where it explain how to create rotating banners, I mean like When we go to a website, they have a banner. And then like in 1 second some text comes, then something else comes, and repeats same thing.

  • Adobe Premiere Pro showing "licence for this product has stopped working"

    I have tried everything in the forum but nothing helps. please help me Only Adobe Premere Pro is showing this problem while rest all the things are working fine. not is it showing any error number.