hello,
I have been meessing around with a simple drawing tool. It is an example out of begining Java 2. As an extra I was trying to add a draw triangle but got a little bit stumped. I can get the button to draw a static triangle. One size one place, but I wanted to be able to create a triangle of any size anywhere in the frame.

Below is the simple code to produce a satic triangle:

triangle= new GeneralPath();
triangle.moveTo(50.0f, 50.0f);
triangle.lineTo(150.0f, 50.0f);
triangle.lineTo(100.0f, 100.0f);

And below here is what i am trying for but not quite getting right. I want the user to click on the frame to start the triangle. Then two more clicks to produce the triangle:

public Triangle(Point start, Point next, Color color) {
super(color);
triangle= new GeneralPath();
triangle.moveTo(start.x, start.y);

// Add another side
public void modify(Point start, Point next) {
triangle.lineTo(150.0f, 50.0f);
triangle.lineTo(100.0f, 100.0f);
triangle.closePath();
}

}

Recommended Answers

All 3 Replies

Sorry I should have said. The program itself is complete. It can do rectangles, lines and ellipses already. Hence I have a mouse listener in the frame. It's just this class i.e. trying to add a triangle I am having trouble with. There is a mouse listener for Press, drag, and release.
Doing the other shapes seems quite simple, user presses - drags - releases. It seems slightly different to produce a triangle. The user need to press three times to give the three points of the triangle....or am i missing something?

The user need to press three times to give the three points of the triangle....or am i missing something?

Correct. Your mouse listener class can just keep a counter of the clicks as it collects the points. When you have collected the number of points needed for the polygon (any polygon would work, not just a triangle), you can draw the poly and reset the listener counter.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.