Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Trouble with coordinates

Stephen Donovn
Greenhorn
Posts: 15
Hi

I'm having trouble in 2 areas.

I'm trying to draw 3 triangles 'on top' of each other to give appearance of a tree. My code is below and struggling with how to set my x/y cords correctly.

Also I've drawn a rectangle under a new class and that should then be appearing in the south location of my border layout (south) - but it's not.

Can anyone guide me into where I'm going wrong.

Thanks

christmas.jpg

Campbell Ritchie
Sheriff
Posts: 55344
157
Start with squared paper and mark from 0 at the left to several hundred on the right, along the top.
Mark from 0 at the top to several hundred at the bottom, along the left side.
That will give you some idea of the coordinate system. More details in the Java Tutorials.
What you want is for the x coordinate of the midline of the three triangles to be the same. You can start at the top using the same x for each triangle. If you start at the left, try
panelWidth ÷ 2 − triangleWidth ÷ 2

You can alter the coordinate system with translate and scale. Something like this will turn the coordinates into what we used in school maths:

Stephen Donovn
Greenhorn
Posts: 15
Thanks so much for the reply.

Your suggestion led me to excel and from there drawed the triangle - got the size in cm's and converted to pixel size via a great calculator I found online..... and now have triangle located where I want it

So thanks again....

Re the 2nd issue I had in that the 2nd class 'MyPanel2' - what am I missing there as that drawing (rect) not being displayed and I'm expecting it to show in the 'south' position.

Thanks again

Steve

Rob Camick
Ranch Hand
Posts: 2787
12
Re the 2nd issue I had in that the 2nd class 'MyPanel2' - what am I missing there as that drawing (rect) not being displayed and I'm expecting it to show in the 'south' position.

The "SOUTH" position of the BorderLayout respects the preferred size of the component. You created a custom component which has a size of (0, 0) so there is nothing to paint. You need to override the getPreferredSize() method of your class to provide the size of the component. Read the section from the Swing tutorial on Custom Painting for more information and working examples.

Stephen Donovn
Greenhorn
Posts: 15
Thanks for the reply.... I've read through what you've suggested but sorry I'm still confused.

Probably really simple but any further help would be appreciated as tried amending my code based on the examples but still not displaying the final drawing.

Thanks again

Rob Camick
Ranch Hand
Posts: 2787
12
Post your SSCCE that demonstrates the problem.

Campbell Ritchie
Sheriff
Posts: 55344
157