Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

draw a shape

Posted on 2003-10-26
8
Medium Priority
?
757 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 500 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.
Suggested Courses

688 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