Creating lines in an applet

Hi,
I was wondering how would I use the mouse to create lines between two vertices (also drawn by the mouse) within a Java applet. I need to be able to click on two different vertices and this should result in a line being drawn between them.
I use the mouseDown handler and need to somehow get the x and y positions when the mouse is clicked so I can draw a line between two points. I have created this in a JFrame but use the mousePressed handler and hence can type
xPos = e.getX() and yPos = e.getY(). Is there any way of doing the same thing using mouseDown.
Here is the code
public boolean mouseDown(Event e, int x, int y)
xPos = e.x;
yPos = e.y;
int i;
if (inter < max_number_of_vertices) {//draws the vertices
inter++;
circle_points[inter] = x;
circle_points2[inter] = y;
repaint();
else {
if (first_time == 0) {
JOptionPane.showMessageDialog(null, "now enter your edges");
first_time = 1;
else if (click_Second_boolean == true) {
second_click(); // the code for this is below
testing++;
else {
first_click(); //the code for this below
number_of_lines++;
return true;
///this is the code for the methods first click and second click.
public void first_click()
closest_point = 100000;
for(int i =0; i<max_number_of_vertices;i++)
int compare1a = circle_points;
int compare1b = circle_points2[i];
double compare1_phy = Math.sqrt(((compare1a - xPos) *( compare1a - xPos ))+ ((compare1b -yPos ) *( compare1b -yPos)));
if(compare1_phy < closest_point)
closest_point = compare1_phy;
closest_point_PT1 = i;
lines_array[0][testing] = circle_points[closest_point_PT1];
click_Second_boolean = true;
void second_click()
closest_point =10000;
for(int i =0; i<max_number_of_vertices;i++)
int compare2a = circle_points[i];
int compare2b = circle_points2[i];
double compare2_phy = Math.sqrt(((compare2a - xPos) *( compare2a - xPos ))+ ((compare2b -yPos ) *( compare2b -yPos)));
if(compare2_phy < closest_point)
closest_point = compare2_phy;
closest_point_PT2 = i;
lines_array[1][testing] = circle_points[closest_point_PT2];
click_Second_boolean = false;
first_matrix[closest_point_PT1][closest_point_PT2] = true;
repaint();
I then need to be able to use this as a matrix to do calculatoins on. Any help on this matter would be good.
Thanks
KP

No matter what mouse event you use, you should be able to get the X,Y of the hit. You can then feed that into your app and draw a line between the two.

Similar Messages

  • Help with creating an animated java applet...

    Hello, everyone! I'm working on a homework assignment for my Intro to Java class, and I'm having a problem I've never run into before. My assignment is to create a animated Java applet which displays an image of an alien spaceship which travels in diagonal lines across the applet area. My main problem is that when I try to compile my code, I get three "cannot find symbol" errors about my variable "alien." I can't figure out why declaring it as a class variable hasn't solved my problem. The errors pop up for the repaint() method, as well as the pane.add and the paintIcon. Here's my code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.Timer;
    class DrawAlien extends JPanel
    public class Alien extends JApplet implements ActionListener
      ImageIcon alien;
      public void init()
        Container pane = getContentPane();
        alien = new ImageIcon(getClass().getResource("alien.gif"));
        pane.setBackground(Color.black);
        pane.add(alien);
        Timer timer = new Timer(30, this);
        timer.start();
      public void actionPerformed(ActionEvent e)
        alien.repaint();
    }I've only posted the section which I think is relevant to my problem, but I can post the rest if anyone thinks it's necessary.
    I'm aware that the code isn't ready yet in several other ways, but I don't think I can move on until I figure out what's up with the "alien" variable. I don't need anyone to write code for me or anything like that, I just lack perspective here. A couple hints in the right direction would be invaluable. Thanks!
    Edited by: springer on Nov 25, 2007 10:46 PM

    You can?t add ImageIcon into pane, you can do like below.
    alien = new ImageIcon(getClass().getResource("alien.gif"));
        pane.setBackground(Color.black);
        JLabel label = new JLabel(alien);
        pane.add(label);

  • Change of additional acct. assignment in automatically created line items

    Hello,
    How can we change the additional account assignment of automatically created line items in transaction MIRO. In my case the tax line item is created automatically in MIRO.
    I am able to change the account assignment of consumption line item. But not Tax line item.
    Best Regards,
    Tapan

    But FBZP is for APP not for Tax line item. Is it possible to change the additional account assignement (from cost center to internal order) in tax line item in MIRO.
    Best Regards,
    Tapan

  • Tax Cide Field as editable for Automatically created Line Item

    Hi
    How can we make the tax code editable for automatically created line item, when an FI Document is simulated.
    Thanks & Regards
    Sanil K Bhandari

    Hi
    I want to edit the line item which has been created by the system, when the document is in simulation mode. The entry has not been posted yet.
    Thanks & Regards
    Sanil K Bhandari

  • Creating Line Graphs in Portal

    Is it possible to create line graphs in Portal, I can create the bar graphs but they dont suit my needs.
    If not in Portal, are there any Oracle products that can create line graphs on data in a database?
    Thanks

    What about Oracle Graphics?
    <BLOCKQUOTE><font size="1" face="Verdana, Arial">quote:</font><HR>Originally posted by Elliott Hamm:
    Is it possible to create line graphs in Portal, I can create the bar graphs but they dont suit my needs.
    If not in Portal, are there any Oracle products that can create line graphs on data in a database?
    Thanks<HR></BLOCKQUOTE>
    null

  • How to create line in smart form

    dear all,
            i hv to create lines in smartform.
    BOX XPOS '0' MM YPOS '6' MM HEIGHT '0' MM WIDTH '190' MM FRAME 1 PT
    by using above statement i can create line in SAPSCRIPT.
    is there any similar statement in smartform to create lines
    if So plz send me a code.
    regards
    Vinod.

    create window and provide these parameters  in the output options to get the line. and  in  line  select check box line with                                                                               
    Left margin          0.00      CH    Width           80.00       CH   
      Upper margin     12.50      LN    Height             0.05       LN                                                                               
    line
       check the box  line with                                                                               
    Gray value       0   %     
       Width                     1.00       TW                       (0% = not filled)          
       vertical spacing       0.00       CM                                
       horizontal spacing   0.00       CM

  • Need to Create Line Items through CRM_ORDER_MAINTAIN

    Hi all,
    I need code for creating Line Items with product and Quantity through CRM_ORDER_MAINTAIN,
    I am able to create header and the partner data through CRM_ORDER_MAINTAIN.
    Suggest the solution.

    Since you have already used the function module CRM_ORDER_MAINTAIN for partners, it shouldn't be difficult for you to use it for more sets of data. Well, the idea is to know what is required to be filled in the input fields and keys. There is a report program CRM_ORDER_READ. Use this with an existing transaction that contains the type of data you want to know about. You will get lot of insight into the data required to for any set of data.
    Also, see this thread for some sample code
    Set item, partner data of sales order using BAPI_BUSPROCESSND_CREATEMULTI

  • Automatically created line items will be deleted-Withholding tax

    Dear Experts,
    Please let me know the reason for generating message as Automatically created line items will be deleted while simulating.On second simulation onwards,TDS entries is deleting and the same is disappearing on posting.

    Hi,
    You might be getting this error while do clearing of vendor who is liable for withholding tax.
    You are getting this error because you have to do payment of Withholding tax.
    If this is not the scenario please revert with exactly when you are getting said error.
    Regards,
    Tejas

  • When we create Line item dimension Like before implementation or  productio

    Hi BW experts ,
    i have doubt Regarding the creation of Line item Dimension
    When we creating the lineitem dimension means After Implementation or Before implementation plz Give the Exact answer .
    if u give the exact answer i will assign the moer points
    Regards
    prakash.v
    [email protected]

    Hi Prakash,
                     You can create the line dimension before implenmentation and after implementation.. if u know the size of data u can create at initial stage otherwise go RSRV tcode u can know the size and can create the line dimension, otherwise if dimension table is bigger than fact table u can create line dimension.

  • KKA3 "Do not create line IDs of 'Settlement' cat. when defining assignmnt"

    Hi gurus,
    when i am runing KKA3 getting message "Do not create line IDs of 'Settlement' cat. when defining assignmnt rules". can any one guid me to come out from this issue.
    Regards,
    Ram

    Hi Ram,
    I am sorry, but as I wrote this might be not just the technical issue when you can simply change from category A to category N.  In OKG4 you classify the line ID to one of the SAP predefined categories, while in OKG5 you define the sourse for the line ID.  Both settings require understanding of business requirements and knowledge of many other things, not just Result Analysis, but also cost element coding rules, etc.  Try to get in touch with functional guys who are the design owners.
    Paulo

  • How to create line indesign sdk?

    hi,
       I had created line but i cannot able to set length for this line,pls help me.
    here my code is,
    UIDRef lineRef=Utils<IPathUtils>()->CreateLineSpline
    (spreadLayerUIDRef,points,INewPageItemCmdData::kDefaultGraphicAttributes,kTrue,Transform:: InnerCoordinates());
    thanks,
    vijay.

    Hi,
    If the requirement is on a small scale and u have only a few no of users (To Restrict) and only a few forms, what i did was
    1.Get the curently logged in user.
    2.Check if he is a restricted user.
    3.Check if the form opened is a restricted form for the user.
    4.Then on the form load or on the ITEM (UID 1) make bubble event to FALSE and show msg that the  user is not supposed to make the changes.
    This is what i did as mine was limited to only some forms...:)
    Hope this helps u..
    Vasu Natari.

  • Creating multiple Threads in applets

    Hi, I need some help creating multiple threads in applets. If you look at the simple code example below you�ll see that it uses one thread that calls the findTarg() method that gives its sleep() a value and makes the oval find its new target.
    What I want to know is how do I create a new thread in this applet? � So for example I could call a findTarg2() method from within a new thread that sets its sleep() to a faster or slower value and starts at the same time as findTarg().
    Hope that makes sense.
    <applet code="thread.class" height=400 width=400>
    </applet>
    import java.awt.Graphics;
    import java.awt.*;
    import java.awt.event.*;
    public class thread extends java.applet.Applet implements Runnable {
    int x = 200;
    int y = 200;
    int xTarg;
    int yTarg;
    int sleeper;
    boolean go;
    Thread first;
    public void start(){
    if(first == null); {
    first = new Thread(this);
    first.start();
    public void run(){
    while (first != null) {
    findTarg();
    try {
    Thread.sleep(sleeper);
    } catch (InterruptedException e) { }
    public void findTarg(){    
    if(go == true){
    sleeper = 10;
    if(x < xTarg)x++;
    if(x > xTarg)x--;
    if(y < yTarg)y++;
    if(y > yTarg)y--;
    repaint();
    if((x == xTarg)&&(y == yTarg))go = false;
    public boolean mouseDown(Event evt, int x, int y) {
    xTarg= x;
    yTarg= y;
    go = true;
    return true;
    public void paint(Graphics g) {
    g.setColor(Color.black);
    g.fillOval(x,y,20,20);
    }

    I've found another example and this time it extends JApplet. I'm sorry I can't help you more, but I'm new as well.
    // Fig. 15.7: RandomCharacters.java
    // Demonstrating the Runnableinterface
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class RandomCharacters extends JApplet
    implements Runnable,
    ActionListener {
    private String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private JLabel outputs[];
    private JCheckBox checkboxes[];
    private final static int SIZE = 3;
    private Thread threads[];
    private boolean suspended[];
    public void init()
    outputs = new JLabel[ SIZE ];
    checkboxes = new JCheckBox[ SIZE ];
    threads = new Thread[ SIZE ];
    suspended = new boolean[ SIZE ];
    Container c = getContentPane();
    c.setLayout( new GridLayout( SIZE, 2, 5, 5 ) );
    for ( int i = 0; i < SIZE; i++ ) {
    outputs[ i ] = new JLabel();
    outputs[ i ].setBackground( Color.green );
    outputs[ i ].setOpaque( true );
    c.add( outputs[ i ] );
    checkboxes[ i ] = new JCheckBox( "Suspended" );
    checkboxes[ i ].addActionListener( this );
    c.add( checkboxes[ i ] );
    public void start()
    // create threads and start every time start is called
    for ( int i = 0; i < threads.length; i++ ) {
    threads[ i ] =
    new Thread( this, "Thread " + (i + 1) );
    threads[ i ].start();
    public void run()
    Thread currentThread = Thread.currentThread();
    int index = getIndex( currentThread );
    char displayChar;
    while ( threads[ index ] == currentThread ) {
    // sleep from 0 to 1 second
    try {
    Thread.sleep( (int) ( Math.random() * 1000 ) );
    synchronized( this ) {
    while ( suspended[ index ] &&
    threads[ index ] == currentThread )
    wait();
    catch ( InterruptedException e ) {
    System.err.println( "sleep interrupted" );
    displayChar = alphabet.charAt(
    (int) ( Math.random() * 26 ) );
    outputs[ index ].setText( currentThread.getName() +
    ": " + displayChar );
    System.err.println(
    currentThread.getName() + " terminating" );
    private int getIndex( Thread current )
    for ( int i = 0; i < threads.length; i++ )
    if ( current == threads[ i ] )
    return i;
    return -1;
    public synchronized void stop()
    // stop threads every time stop is called
    // as the user browses another Web page
    for ( int i = 0; i < threads.length; i++ )
    threads[ i ] = null;
    notifyAll();
    public synchronized void actionPerformed( ActionEvent e )
    for ( int i = 0; i < checkboxes.length; i++ ) {
    if ( e.getSource() == checkboxes[ i ] ) {
    suspended[ i ] = !suspended[ i ];
    outputs[ i ].setBackground(
    !suspended[ i ] ? Color.green : Color.red );
    if ( !suspended[ i ] )
    notify();
    return;
    }

  • Creating methods in an applet

    I am confused on how to create methods in an applet. I know how to create them in an application. However, if an applet, do you create the methods in the Class Header, after the init() method, or the paint (Graphics g) method.
    I am trying to create a method called StudentName where the Student name and class.
    I originally had the method in the class body but I received error messages -- mainly because I hadn't started the paint method yet.
    thanks for your help.
    Here is a sample of the code I am using:
    import javax.swing.*;
    import java.awt.Graphics;
    public class ClassAverageApplet extends JApplet
          String first,second, third, fourth,fifth, name, className;
          double a,b,c,d,e,f, sum;
        public void init() 
          ClassAverageApplet student = new ClassAverageApplet();
          name = JOptionPane.showInputDialog("Student's name please");
          className = JOptionPane.showInputDialog("Class");
          first = JOptionPane.showInputDialog("Please enter first Grade");
          a = Double.parseDouble(first);
          second = JOptionPane.showInputDialog("please enter second Grade");
          b = Double.parseDouble(second);
          third = JOptionPane.showInputDialog("please enter Third Grade");
          c = Double.parseDouble(third);
          fourth = JOptionPane.showInputDialog("please enter Fourth Grade");
          d = Double.parseDouble(fourth);
          fifth= JOptionPane.showInputDialog("please enter Fifth Grade");
          e = Double.parseDouble(fifth);
          f = (a + b + c + d + e)/5;
          sum = a + b + c + d + e;
       public void paint(Graphics g)
          public void StudentName()
             g.drawString("Student:    " + name,25,25);
             g.drawLine(25,25,70,25);
             g.drawString("Class:      " + className,25,40);
             g.drawLine(25,40,60,40);
           g.drawRect(15,10,300,150);
       if(f  > 89)
         student.StudentName();
    //I am replacing these next four statements with the StudentName method
       // g.drawString("Student:    " + name,25,25);
       // g.drawLine(25,25,70,25);
       // g.drawString("Class:      " + className,25,40);
       // g.drawLine(25,40,60,40);
        g.drawString("Your final grade is an A ",25,50);
        g.drawString("The sum of your grades out of 500 points is " + sum ,25,60);
        g.drawString("The grade average is " + f,25,70);
        g.drawString("Here are your grades for the class",70,90);
        g.drawLine(70,90,255,90);
        g.drawString("First Grade:    " + a,25,110);
        g.drawLine(25,110,90,110);
        g.drawString("Second Grade:   " + b ,25,120);
        g.drawLine(25,120,90,120);
        g.drawString("Third Grade:    " + c,25,130);
        g.drawLine(25,130,90,130);
        g.drawString("Fourth Grade:   " + d ,25,140);
        g.drawLine(25,140,90,140);
        g.drawString("Fifth Grade:    " + e,25,150);
        g.drawLine(25,150,90,150);
        }

    i don't think you know what you're talking about. there's no difference between the way you write an applet or application. one has an init() method one has main(String[] args) method. one extends applet, but i won't tell you which.
    i'm sure you need to define half of your methods inside the paint() method and the other half inside the init() method. it does matter what goes where as long as it's half and half. the important thing is to make sure you have an even number of methods, otherwise your applet will behave oddly.

  • How to create line chart

    Hii Experts,
    How to create line chart?
    I want to  prepaire a line chart in a report. i have 1 table. In a table have 2 fields. chart is created in these 2 field refrence how It's possible?
    Thanx in Advance
    Regards
    Vshal

    I found it very easy to produce a line chart in a window container using FM GFW_PRES_SHOW.  In my case this produces a basic line graph with three lines across 15 time periods.
    See transaction GRAL for various chart types...they're relatively easy to produce...  I picked DEMO_GFW_PRES_SHOW report program from GRAL as my example for how to produce a simple line chart very quickly.

  • Creating line item for a sales order in user exit

    Hi
    I have to create a line item through user exit "userexit_save_document_prepare" when saving the sales order.I am doing this by appending a row to the internal table XVBAP in the user exit.
    The row is getting appended to the internal table but the line item is not getting created in the sales order .I dont getting any error also.
    Can anyone tell me what could be the reason for this and is there any other means by which i can create a line item through the user exit.

    You have to set the creation flag UPDKZ to 'I'.
    to correctly use the user exit refer to OSS note 0381348 Using user exit, customer exit, VOFM in SD.
    pls reward if helpful
    Roberto

Maybe you are looking for