Solved

draw a shape

Posted on 2003-10-26
8
756 Views
Last Modified: 2007-12-19
hi could someone please tell me how to
1. let a user drag the mouse making the shape of a triangle
then
2. using the points of the triangle, make a straight lined one and display it.

i'm new to java so any help is appreciated... thanks in advance,
0
Comment
Question by:justbringit69
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
8 Comments
 
LVL 1

Expert Comment

by:RodionP
ID: 9623203
as for number 1 I am not sure what exactly you can, you want to DRAG a triangle or you want to make a triangle by dragging something ?

as for number 2:

you use pain() mathod of your component if you use Canvas or paintComponent if you painting on JComponent, and you make a GeneralShape, using an array of your points, and then when you have shape you invoke draw method of the Graphics that is a parameter of paintComponent/paint method. Notice, paint and paintComponent is invoked every time when something needs to be drawn... it is invoked by system.

this example will draw a rectangle of width/height 100 pixls starting at 10, 10

public void paintComponent(Graphics in){
      super.paintComponent(in);
      Graphics2D currentGraphics = (Graphics2D)in;
                currentGeraphics.draw(new Rectangle2D.Double(10,10,100,100))
}


if you clarify you question better, I could help you writing the code
0
 

Author Comment

by:justbringit69
ID: 9623256
hi again
i mean dragging the mouse into the shape of a triangle. the same way that you would use the pencil on ms paint. then using the coords of where you press the mouse button and release the mouse button, to work out the points of the triangle. then using  lines to connect these points into a triangle.
hope this is worded better?
thanks
0
 
LVL 1

Accepted Solution

by:
RodionP earned 125 total points
ID: 9623349
ok again, you have a triangle, then you move(move or drag ? drag is same as move but with pressed mouse) into that triangle and within that triangle you want to draw another triangle by pressing mouse three times, and coordinates where you pressed the mouse would be vertices of a new(within the old triangle) triangle, is that correct ?


well, let's assume that's what you want, you have to write MouseListener and you have to write mousePressed() method this way:

assume you have a shape of your "big" triangle

GeneralPath smallTriangle = new GeneralPath();
int currentPoint = 0;

public void mousePressed(MouseEvent event){
   if(!bigTriangle.contains(event.getPoint()))
      return;//this ensures that you are in the triangle shape
   if(currentPoint == 0)
      smallTriangle.moveTo(event.getX(),event.getY())
   else
      smallTriangle.lineTo(event.getX(),event.getY())
   currentPoint++;
   if(currentPoint > 2){
      smallTriangle.closePath();
      ((Graphics2D)this.getGraphics).draw(smalTriangle)
      smalTriangle.reset();
      currentPoint  = 0;
   }
}
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 1

Expert Comment

by:RodionP
ID: 9623358
oh yes, little note, I invoked this.getGraphics()  this means your class should be a Component, if you don't like that, you can use any Component on which you would like to draw that triangle, just simply use its obj refernce instead of "this".

Above example draws triangle when you press your mouse (if you want click or release, simply user mouseClicked() or mouseReleased method instead of mousePressed()) 3 times, and when you press last time it draws a triangle. Within a shape of bigger triangle, you can construct a bigger triangle same way as I constructed smaller triangle by using GeneralPath class
0
 

Author Comment

by:justbringit69
ID: 9627123
sorry i'm kinda new to java, i'm not sure what code i'm supposed to add to that. doesn't help that I've had a bit of a break from programming! sorry if i sound really bad here :S
0
 
LVL 1

Expert Comment

by:RodionP
ID: 9631478
ok, could you please explain what exactly you want ?
is it you want to draw a triangle inside another triangle ?
or you want to drag some triangle into some different triangle, or what exactly you want to do, because I have difficulties understaning specs of your task, I will help you write the class, starting from main method to what you want to do, but I have to understand what exactly is it that you want. If you can post your code, that would be helpful.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question