Using values of two different applications that are from the same class


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: Using values of two different applications that are from the same class

  1. #1
    Join Date
    Jan 2003
    Posts
    8

    Using values of two different applications that are from the same class

    Hi,

    See the following little program

    What I can't figure out is
    i) How to change the values if I add a button
    ii) How to print just app.x1 etc. The way I have it now, it will just print x1, y1 etc, from both app and app1, but I want to be able to distinguish between the two of them so I can use the values.....ie for example add app.x1 and app1.x2 (only using that as an example of what I want to use them for)

    I'd really appreciate if anyone could help me out, it's probably simple, but just cant figure it.


    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;


    public class drawLine extends JFrame{

    int x1, y1, x2, y2;
    JPanel topPanel;
    JButton button;
    public length l, l3;

    public drawLine(){
    super("drawLine");
    }
    public drawLine(length l){
    this.l = l;
    topPanel = new JPanel();
    button = new JButton("Change");

    topPanel.add(button);
    getContentPane().add(topPanel, BorderLayout.NORTH);

    x1 = 100;
    y1 = 100;
    x2 = l.x2;
    y2 = l.y2;

    System.out.println(x1 + " " + y1 + " " + x2 + " " + y2);


    /*button.addActionListener(
    new ActionListener(){
    public void actionPerformed(ActionEvent e){
    l.length();
    repaint();
    }
    }
    );*/

    }
    public void hello(){
    System.out.println(x1 + " " + y1 + " " + x2 + " " + y2);
    }

    public void paint(Graphics g){
    draw(g);
    }

    public void draw(Graphics g){
    g.drawLine(x1, y1, x2, y2);
    }

    public static void main(String args[]){
    length l1 = new length();
    length l2 = (length)l1.clone();
    l1.length();
    l2.length();

    drawLine app = new drawLine(l1);
    app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    app.setSize( 300, 300 );
    app.move(0,0) ;
    app.setVisible( true );


    drawLine app1 = new drawLine(l2);
    app1.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    app1.setSize( 300, 300 );
    app1.move(300,0) ;
    app1.setVisible( true );
    }
    }



    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class length implements Cloneable{

    String whatlength;
    int llength;
    int x1 = 100;
    int y1 = 100;
    public int x2, y2;


    public void length(){

    whatlength = JOptionPane.showInputDialog("What is the length of the line");
    llength = Integer.parseInt(whatlength);
    x2 = x1 + llength;
    y2 = y1;

    }
    public Object clone() {
    Object object = null;
    try {
    object = super.clone();
    }
    catch (CloneNotSupportedException exception) {
    System.err.println("AbstractSpoon is not Cloneable");
    }
    return object;
    }
    }

    Thanks

  2. #2
    Join Date
    Mar 2003
    Posts
    834
    "i) How to change the values if I add a button?"

    Do you mean changing the instance variables? What's the problem?

    "ii) How to print just app.x1 etc. "

    There is no nice way of doing this, except by passing a String into the constructor and then output the string before displaying the x1 etc. values.
    ArchAngel.
    O:-)

  3. #3
    Join Date
    Mar 2003
    Posts
    834
    Oh yeah, and it might be worth reading up on Object-orientation and Java naming conventions as your program doesn't really seem to follow either...
    ArchAngel.
    O:-)

  4. #4
    Join Date
    Jan 2003
    Posts
    8
    Yeah I know my naming is bad, but I only made up the program really quick so didn't really stick to the rules.

    I think you misunderstood what I meant by printing out app1.x1, it's the value of the variable that I want. Basically with my program, as it is, it will print out x1, x2 etc, but I want to be able to distinguish between which are the app variables and which are the app1.

    As with the button, I kept getting an error when I tried to use the button code that I have commented out

  5. #5
    Join Date
    Mar 2003
    Posts
    834
    "I think you misunderstood what I meant by printing out app1.x1, it's the value of the variable that I want. Basically with my program, as it is, it will print out x1, x2 etc, but I want to be able to distinguish between which are the app variables and which are the app1."

    I don't think I did. When you're calling drawLine() and the variable's values are being printed out, you want to know to whom those values belong i.e. on which object drawLine() was just called.

    Pass in a String when the object is created to give the object a name. Print out that name when you print out the values.
    ArchAngel.
    O:-)

  6. #6
    Join Date
    Jan 2003
    Posts
    8
    I've tried it like this, ie by adding an int to drawLine and trying to keep an array of lengths, which sortof works, but see the code I have commented out in the button actionListener class. What I thought I'd ba able to do is access the arrays like that.....how come it gives me an exception?


    public class drawLine extends JFrame{

    int x1, y1, x2, y2;
    JPanel topPanel;
    JButton button;
    public length[] linelength = new length[2];

    public drawLine(final length l, final int k){
    super("drawLine");

    this.linelength[k] = l;

    topPanel = new JPanel();
    button = new JButton("Change");

    topPanel.add(button);
    getContentPane().add(topPanel, BorderLayout.NORTH);

    x1 = 100;
    y1 = 100;
    x2 = linelength[k].x2;
    y2 = linelength[k].y2;




    button.addActionListener(
    new ActionListener(){
    public void actionPerformed(ActionEvent e){
    //Update the length object int's
    linelength[k].getLineLength();
    // Copy these ints back to the drawline object
    x2 = linelength[k].x2;
    y2 = linelength[k].y2;
    System.out.println(k + " " + linelength[k].x1 + " " + linelength[k].y1 + " " + linelength[k].x2 + " " + linelength[k].y2);
    repaint();




    /*********This bit gives me a null pointer exception ********
    linelength[0].x1 = linelength[1].x1;
    linelength[0].x2 = linelength[1].x2;
    linelength[0].y1 = linelength[1].y1;
    linelength[0].y2 = linelength[1].y2;
    ***************************************************/




    }
    }
    );

    }

    public void paint(Graphics g){
    g.clearRect( 0, 0, getWidth(), getHeight() );
    draw(g);

    }

    public void draw(Graphics g){
    g.drawLine(x1, y1, x2, y2);
    }

    public static void main(String args[]){
    length l1 = new length();
    length l2 = (length)l1.clone();
    l1.getLineLength();
    l2.getLineLength();

    drawLine app = new drawLine(l1, 0);
    app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    app.setSize( 300, 300 );
    app.move(0,0) ;
    app.setVisible( true );


    drawLine app1 = new drawLine(l2, 1);
    app1.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    app1.setSize( 300, 300 );
    app1.move(300,0) ;
    app1.setVisible( true );
    }
    }

  7. #7
    Join Date
    Jan 2003
    Posts
    8
    >>>When you're calling drawLine() and the variable's values are being printed out, you want to know to whom those values belong

    Thanks for the help so far ArchAngel, re the above, what I really want to know is not to whom these values belong, but I want to be able to distinguish between the both of them so I can use them whenever I want.

    Thanks

  8. #8
    Join Date
    Mar 2003
    Posts
    834
    You're getting a null pointer because although you declare the array, you never populate it!
    ArchAngel.
    O:-)

  9. #9
    Join Date
    Jan 2003
    Posts
    8
    By populating do you mean something like

    linelength[k].x1 = l.x1;
    linelength[k].y1 = l.y1;
    linelength[k].x2 = l.x2;
    linelength[k].y2 = l.y2;
    linelength[k].llength = l.llength;

    under the

    "linelength[k] = l;" line in the drawLine method.

    I tried this and it's still giving me an exception

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center