Get array from JFrame class to Applet
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Get array from JFrame class to Applet

  1. #1
    Join Date
    Dec 2010
    Posts
    6

    Get array from JFrame class to Applet

    Hi,

    I'm writing an applet, where I have multiple instances of a JFrame class that all have mouseinputs. I use the JFrames to draw on and store the pixels values in an array within the JFrame class. My question is; how do I get this array of pixel values into my applet class? I've tried retreiving it by

    array=jframeInstance.pixelarray

    But the values returned are all zeros. The values are read into pixelarray in my class when I change pixelvalues with my mouse and i can print them out to my console inside the JFrame class.

    Any suggestions or ideas are welcome!
    Thanks!

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Get array from JFrame class to Applet

    You need to store the values in an attribute of your frame class. It should be private rather than public and you should provide an access method to return it, or better still return a copy of it.

    As you haven't shown where/how you are storing the values it's hard to give any more advice. If you do post more code remember to use code tags.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Dec 2010
    Posts
    6

    Re: Get array from JFrame class to Applet

    Thanks for the feedback keang!

    My attribute of the Frame class is pixelData. I don't quite follow your response regarding changing this attribute to private. If I do so, then I won't be able to access it in my applet class... I've tried to change it to what you're suggesting (I think) but it's still not working. Here's the code:

    Code:
    public class MouseMotionEventDemo extends JPanel implements MouseMotionListener, MouseListener {
    	  
    	public passArray arrayInst;
    	public int mX,mY;
    	private static int size = 201;
    	private int prevX = 0;
    	private int prevY = size/2;
    	private Boolean mousePressed = false;
      
    	public int test = 2;
    	private double pixelData[] = new double[size]; 
    	public double outData[] = new double[size];
      
    	public MouseMotionEventDemo() {
    	    addMouseMotionListener(this);
    	    addMouseListener(this);
    	    setVisible(true);
    	}
    
    	public void mouseMoved(MouseEvent me) {
    	  
    	}
    
    	public void mouseDragged(MouseEvent me) {
    		mX = (int) me.getPoint().getX();
    		mY = (int) me.getPoint().getY();
    		repaint();
    		if(mX<size){
    			arrayInst.data[mX] = mY;
    			pixelData[mX] = mY;
    	    	//System.out.println("pixelData[" + mX + "]" + " is " + pixelData[mX]);
    	 }
    	  prevX=mX;
    	  prevY=mY;
      }
      
      public void mousePressed(MouseEvent e) {
    	  mousePressed = true;
      }
      
      public void paint(Graphics g) {
    	if(mousePressed==true){
        	g.setColor(Color.white);
        	g.fillRect(0, 0, getWidth(), getHeight());
        	g.setColor(Color.black);
        	g.drawLine(0, getHeight()/2, getWidth(),getHeight()/2);
        	mousePressed = false;
        }
        //g.fillRect(mX, mY, 5, 5);
        g.drawLine(prevX,prevY,mX,mY);
      }
    
      @SuppressWarnings("deprecation")
    	public static void run(int x, int y, String name) {
    	    JFrame f = new JFrame();
    	    f.getContentPane().add(new MouseMotionEventDemo());
    	    f.setSize(size, size);
    	    f.setTitle(name);
    	    f.setLocation(x,y);
    	    f.show();
    	}
      
      	public void mouseEntered(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    
    
    	public void mouseExited(MouseEvent e) {
    		// TODO Auto-generated method stub
    	}
    
    
    	public void mouseReleased(MouseEvent e) {
    		for(int x=0;x<size;x++){
    			outData[x] = (pixelData[x]/(size/2)-1)*-1;		//normalize to fit from 1 to -1
    			//System.out.println(outData[x]);
    		}
    	}
    
    	public void mouseClicked(MouseEvent e) {
    		// TODO Auto-generated method stub
    	}
    	
    	public double[] getData() {
    	    return outData;
    	}
    }

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Get array from JFrame class to Applet

    You should not have public attributes. You make then private and provide methods such as getXXX() to return the attribute or a copy of it if it is not immutable.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Dec 2010
    Posts
    6

    Re: Get array from JFrame class to Applet

    Okay. I followed your advice and I'm now able to return the array. Thank you!
    However, the pixelData array in my getData function is filled with zeroes. If I print out the array to the console from my mouseRelease function I get the pixel values that is in pixelData. How come I can access pixelData from my mouseRelease function and not my getData function??

    Code:
    public class MouseMotionEventDemo extends JPanel implements MouseMotionListener, MouseListener {
    	private int mX,mY;
    	private static int size = 201;
    	private int prevX = 0;
    	private int prevY = size/2;
    	private Boolean mousePressed = false;
    
    	private double pixelData[] = new double[size]; 
      
    	public MouseMotionEventDemo() {
    	    addMouseMotionListener(this);
    	    addMouseListener(this);
    	    setVisible(true);
    	}
    
    	public void mouseMoved(MouseEvent me) {
    	  
    	}
    
    	public void mouseDragged(MouseEvent me) {
    		mX = (int) me.getPoint().getX();
    		mY = (int) me.getPoint().getY();
    		repaint();
    		if(mX<size){
    			pixelData[mX] = mY;
    	 }
    	  prevX=mX;
    	  prevY=mY;
    	 }
    	  
    	 public void mousePressed(MouseEvent e) {
    		  mousePressed = true;
    	 }
    	  
    	 public void paint(Graphics g) {
    		 if(mousePressed==true){
    			 g.setColor(Color.white);
    			 g.fillRect(0, 0, getWidth(), getHeight());
    			 g.setColor(Color.black);
    			 g.drawLine(0, getHeight()/2, getWidth(),getHeight()/2);
    			 mousePressed = false;
    		 }
    		 //g.fillRect(mX, mY, 5, 5);
    		 g.drawLine(prevX,prevY,mX,mY);
    	  }
    
      @SuppressWarnings("deprecation")
    	public static void run(int x, int y, String name) {
    	    JFrame f = new JFrame();
    	    f.getContentPane().add(new MouseMotionEventDemo());
    	    f.setSize(size, size);
    	    f.setTitle(name);
    	    f.setLocation(x,y);
    	    f.show();
    	}
      
      	public void mouseEntered(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    
    
    	public void mouseExited(MouseEvent e) {
    		// TODO Auto-generated method stub
    	}
    
    
    	public void mouseReleased(MouseEvent e) {
    		for(int x=0;x<size;x++){
    			System.out.println((pixelData[x]/(size/2)-1)*-1);	//Prints the correct values      from pixelData
    		}
    	}
    
    	public void mouseClicked(MouseEvent e) {
    		// TODO Auto-generated method stub
    	}
    	
    	public double[] getData() {
    	    double[] outData = new double[size];
    	    for(int x=0;x<size;x++){
    			System.out.println(pixelData[x]/(size/2)-1)*-1);              //prints zeroes...?
    		}
    		return outData;
    	}
    }

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Get array from JFrame class to Applet

    Are you sure that is the code you are running? I ask because it won't compile.

    Please make sure you show the code you have actually compiled and run.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  7. #7
    Join Date
    Dec 2010
    Posts
    6

    Re: Get array from JFrame class to Applet

    Maybe its because the imports are missing... Here's the code with imports.

    Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class MouseMotionEventDemo extends JPanel implements MouseMotionListener, MouseListener {
    	  
    	private int mX,mY;
    	private static int size = 201;
    	private int prevX = 0;
    	private int prevY = size/2;
    	private Boolean mousePressed = false;
    
    	private double pixelData[] = new double[size]; 
    	//private double outData[] = new double[size]; 
      
    	public MouseMotionEventDemo() {
    	    addMouseMotionListener(this);
    	    addMouseListener(this);
    	    setVisible(true);
    	}
    
    	public void mouseMoved(MouseEvent me) {
    	  
    	}
    
    	public void mouseDragged(MouseEvent me) {
    		mX = (int) me.getPoint().getX();
    		mY = (int) me.getPoint().getY();
    		repaint();
    		if(mX<size){
    			pixelData[mX] = mY;
    	    	//System.out.println("pixelData[" + mX + "]" + " is " + pixelData[mX]);
    	 }
    	  prevX=mX;
    	  prevY=mY;
    	 }
    	  
    	 public void mousePressed(MouseEvent e) {
    		  mousePressed = true;
    	 }
    	  
    	 public void paint(Graphics g) {
    		 if(mousePressed==true){
    			 g.setColor(Color.white);
    			 g.fillRect(0, 0, getWidth(), getHeight());
    			 g.setColor(Color.black);
    			 g.drawLine(0, getHeight()/2, getWidth(),getHeight()/2);
    			 mousePressed = false;
    		 }
    		 //g.fillRect(mX, mY, 5, 5);
    		 g.drawLine(prevX,prevY,mX,mY);
    	  }
    
      @SuppressWarnings("deprecation")
    	public static void run(int x, int y, String name) {
    	    JFrame f = new JFrame();
    	    f.getContentPane().add(new MouseMotionEventDemo());
    	    f.setSize(size, size);
    	    f.setTitle(name);
    	    f.setLocation(x,y);
    	    f.show();
    	}
      
      	public void mouseEntered(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    
    
    	public void mouseExited(MouseEvent e) {
    		// TODO Auto-generated method stub
    	}
    
    
    	public void mouseReleased(MouseEvent e) {
    		for(int x=0;x<size;x++){
    			//outData[x] = (pixelData[x]/(size/2)-1)*-1;		//normalize to fit from 1 to -1
    			//System.out.println((pixelData[x]/(size/2)-1)*-1);	//works
    		}
    	}
    
    	public void mouseClicked(MouseEvent e) {
    		// TODO Auto-generated method stub
    	}
    	
    	public double[] getData() {
    	    double[] outData = new double[size];
    	    for(int x=0;x<size;x++){
    	    	outData[x] = (pixelData[x]/(size/2)-1)*-1;		//normalize to fit from 1 to -1
    			System.out.println(pixelData[x]);
    		}
    		return outData;
    	}
    }
    Here's the code for my applet class:

    Code:
    import java.awt.BorderLayout;
    import java.awt.Button;
    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.Label;
    import java.awt.Panel;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JApplet;
    
    import andreas2608.examples.MouseMotionEventDemo;
    
    public class finalProject_v2 extends JApplet implements ActionListener {
    	MouseMotionEventDemo sound1;
    	Button start;
    	Button stop;
    	Button setOsc1;
    	static int size = 201;
        double [] data = new double[size];
    	
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    
    	public void init() { 
    		
    		sound1 = new MouseMotionEventDemo();
    		sound1.run(810,0,"Sound 1");
    		buildGUI();
    	}
    	
    	/* When applet starts up */ 
    	public void start() {
    	}
    	
    	/* When applet stops*/ 
    	public void stop() {
    
    	}
    	
    	public void buildGUI(){
    		setBackground(Color.yellow);
    		setLayout(new BorderLayout());						//Divides into four boxes, NORTH, SOUTH, WEST, EAST
    		
    		start = new Button("Start");
    		stop = new Button("Stop");
    		setOsc1 = new Button("Set Osc");
    	
    		//SoundTester soundTester = new SoundTester(ins);
    		//add(BorderLayout.EAST, soundTester);
    		add(BorderLayout.NORTH, new Label("Welcome to Andreas' drawing sequencer"));
    		
    		Panel p = new Panel();
    		p.setLayout(new GridLayout(1,3));
    		p.add(start);
    		p.add(stop);
    		p.add(setOsc1);
    		add(BorderLayout.SOUTH,p);
    		
    		start.addActionListener(this);
    		stop.addActionListener(this);
    		setOsc1.addActionListener(this);
    	}
    	
    
    	//--------Button Action------------
    	
    	public void actionPerformed(ActionEvent event){ 
    		Object source = event.getSource();
    		if(source==start){
    		}
    		if(source==stop){
    		}
    		if(source==setOsc1){
    			System.out.println("you pressed setOsc");
    			data=sound1.getData();
    			for(int x=0;x<size;x++){
    				//System.out.println(data[x]);
    			}
    		}
    	}
    }

  8. #8
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Get array from JFrame class to Applet

    Maybe its because the imports are missing... Here's the code with imports.
    No, I think I could work that out for myself.
    The problem was you had a missing bracket in the code. I could have easily fixed it but it does mean it wasn't the code you compiled and ran hence my last post.

    Interestingly this code, as well as fixing the bracket problem, is slightly different from the last code so before I go to all the trouble of trying to solve your problem please confirm you have run this code and it still has the same problem. BTW you will need to uncomment the print line in the mouseReleased method to prove it has non-zero data.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  9. #9
    Join Date
    Dec 2010
    Posts
    6

    Re: Get array from JFrame class to Applet

    Hmm.. I might have changed some of the print commands to my console, but that's it. This is the code I'm running.

    The reason I've been moving the print commands around is to see where in the code I loose acces to my pixelData array. When I access the array from my getData() function it is empty. However, when I access it from other functions such as mouseReleased() I get the correct data.

    Thank you again for looking at it!

  10. #10
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Get array from JFrame class to Applet

    Ok. Your problem is you aren't using the same instance of MouseMotionEventDemo in the GUI display and the applet code.

    You create an instance of MouseMotionEventDemo in the applet init() method and call it's run method (which you shouldn't be calling from an instance reference as it's a static method). The static run method creates its own MouseMotionEventDemo instance which it displays. This is the instance that correctly collects values but you call the getData() method on the other instance of MouseMotionEventDemo (the one that hasn't been displayed).

    I suggest you change the run method to return the instance it creates and you call getData() on this returned object.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  11. #11
    Join Date
    Dec 2010
    Posts
    6

    Re: Get array from JFrame class to Applet

    That makes sense... It works now. Thanks!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center