Solved

draw a shape

Posted on 2003-10-26
8
741 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java 1603 Error 3 50
simple java question 3 44
stackato and cloud 4 74
diffSum example 4 26
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

867 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now