Link to home
Start Free TrialLog in
Avatar of justbringit69
justbringit69

asked on

draw a shape

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,
Avatar of RodionP
RodionP

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
Avatar of justbringit69

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of RodionP
RodionP

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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
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.