Solved

draw a shape

Posted on 2003-10-26
8
754 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
  • 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
difference of if loops 23 62
Way to decrease size of apk file 9 88
login jsp example 24 68
Detect Closed Loops (circles, figure-8s, etc) in PNG Images 6 58
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.

828 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