Thread: How to provide x & y values for polygon ? Plz refer below

1. Member
Join Date
Mar 2011
Posts
12
Rep Power
0

How to provide x & y values for polygon ? Plz refer below

:confused:

hi to all

Here i have been trying to do a paint application something like there are
2 buttons such as RECTANGLE , ELLIPSE by following those buttons you have some
EMPTY SPACE which enables you to draw these shapes mentioned Above

based on your need you can select a shape either one of these before you draw
so that selected image will be drawn in the EMPTY SPACE but the image appears only after your mouse pointer drawn on the empty space, but not image appeared automatically when the button is selected

So to implement this RECTANGLE ,ELLIPSE i just want these two methods

drawRect(x,y,w,h)

Ellipse2D.Float(x,y,w,h)

for EXAMPLE Consider about RECTANGLE in this methods to find height & width of RECTANGLE i just create 2 variable and by subtracting end point of cursor with the Starting point found height and width of rectangle
this is inside a public void paint method

int width = end.x - start.x; // declaring variables
int height = end.y - start.y; // declaring variables

g.drawRect(start.x, start.y, width, height); // declaring variables to draw RECTANGLE

but now i want to implement POLYGON and the method of this something like
this X & Y points with the N Points

int xpoints[] = {25, 145, 25, 145, 25};
int ypoints[] = {25, 25, 145, 145, 25};
int npoints = 5;

g.drawPolygon(xpoints, ypoints, npoints);

so in this case i exactly don't know How to provide X & Y values based on where the polygon drawn in empty space

and i'm not allowed to draw polygon just by providing X & Y Values(NUMBERS)

Instead of that i need something like getting X & Y values from a EMPTY SPACE and apply those X & Y values instead of numbers

DO I need to use something like X1.y1.x2,y2,x3,y3........x5,y5

i tried for this but not working any help would be much appreciated

Mano

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18
i tried for this but not working

What did you try?

The method you give the user for entering the coordinates of the polygon should be consistent with whatever method they use for entering the rectangle. So, for example, if the rectangle is entered with left mouse clicks, you might do the same for the polygon vertices adding a double click to signal the last vertex of the polygon.

In terms of how you accumulate the vertices before you create the poly gon, don't use variables xN, yN, etc because you have no idea how many you will need. Instead use a list - or, if you haven't come across lists, you might try an array. Lists take time to learn about, but you may find them a more natural solution than arrays.

3. Member
Join Date
Mar 2011
Posts
12
Rep Power
0
thnx for being replied anyhow i got the idea of yours but how do i implement the double click to signal for the vertex of polygon

i don't have any idea can you give me an example for implementing double click to polygon vertex

in additon of that have you got any reference sites to create color panel something like whenever a color is chosen from a list panel so as based on the color selected, that color is to be filled inside shapes

and the same thing has to be done to color lines of shapes
thanks

4. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18
how do i implement the double click to signal for the vertex of polygon

There's a full example in Oracle's Tutorial on the page How to Write a Mouse Listener.

have you got any reference sites to create color panel

See How to Use Color Choosers.

The Swing section of the Tutorial has plenty of discussion and examples.
Last edited by pbrockway2; 03-15-2011 at 06:55 PM.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•