Solved

draw a shape

Posted on 2003-10-26
8
738 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 1

Expert Comment

by:RodionP
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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 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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

771 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

10 Experts available now in Live!

Get 1:1 Help Now