Results 1 to 11 of 11
  1. #1
    thestarncy is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default Putting a graph on another JPanel in another JFrame

    I have been looking at a way of adding an ECG type graph onto a JPanel which is on my main JFrame. This JFrame consists of many JPanels that are interlinked. So, my aim is to have a a graph that show some blood pressure and other values on the screen when the program runs. I found some codes, one of them is the following. Can someone help me, on how to extract it onto a JPanel, that is running on the main JFrame without affecting the other Jframe?

    Your help is highly appreciated as we learn Java.


    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import java.math.*;
    import java.util.*;
    
    public class ecg1 extends JFrame
    {
    	myView view = new myView();
    
    	ArrayList al = new ArrayList();
    
     	data a;
    
    	public ecg1()
    	{
    	addWindowListener(new WindowAdapter()
            {	public void windowClosing(WindowEvent ev)
    		{	dispose();
    			System.exit(0);}});
    
    		for (int i=0; i <15000; i++)//(int i=0; i >15000; i++)
    	{
    		/**Random p1=new Random();
    		int p=p1.nextInt(19)+6;
    		//int p = (int)(Math.random() * 50);//graph size 260
    		vr.add(new Point(0,p-130));// Graph position 130**/
    	}
    	setBounds(3,10,625,350);
    	setLocation(100,100);
    	getContentPane().add(view);
    	getContentPane().setLayout(null);
    
    	a = new data(al);
    
    	setVisible(true);
    
    		while (true)
    		{
    		try
    		{
    			view.move_1();
    			repaint();
    			Thread.sleep(500);
    		} catch (InterruptedException e) {}
    		}
    	}
    	public class myView extends JPanel
    	{
    		BufferedImage I;
     		Graphics2D    G;
    
    //point spacing, defining the steepness of the graph, but i don't understand why i need it?
     		int point_s = 4;
    		public myView()
    		{
    		setBounds(10,10,600,300);
    I = new BufferedImage(getWidth(),getHeight(),BufferedImage.TYPE_INT_ARGB);
    		G = I.createGraphics();
    G.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    
    			G.setColor(Color.black);
    			G.fillRect(0,0,getWidth(),getHeight());
    			/**G.setColor(Color.white);
    			for (int h=0; h<100; h++) G.fillRect(0,h*20,getWidth(),2);
    			repaint();**/
    
    			G.fillRect(0,0,600,300);
    
    		}
    			public void clear(int x, int w)
    			{
    				G.setColor(Color.black);
    
    	G.fillRect(x,0,w,getHeight()); //fill the rectangle with the specified int x, int y, int width, int height
    
    			}
    
    			public void move_1()
    			{
    
    				if (al.size() < 2) return;
    	G.drawImage(I,0,0,getWidth()-point_s,getHeight(),point_s,0,getWidth(),getHeight(),null);
    
    
    				clear(getWidth()-point_s,point_s);
    
    				G.setColor(Color.white);
    				Point p1,p2;
    
    				p1 = (Point)al.get(0);
    				p2 = (Point)al.get(1);
    
    		G.drawLine(getWidth()-point_s-1,p1.y+getHeight()/2,getWidth()-1,p2.y+getHeight()/2);
    
    				al.remove(0);
    
    			}
    
    			public void paint(Graphics g)
    			{
     				g.drawImage(I,0,0,null);
    			}
    	}
    
    	private class data extends Thread
    	{
    	ArrayList d_al;
    
    		public data(ArrayList a_l)
    		{
    			d_al = a_l;
    			this.start();
    		}
    	public void run()
    	   {
    
    		while (true)
    			{
    			try
    			{
    				while (d_al.size() < 240)
    				{
    	// Am i correct to infer that multiply by 100 because heart beat max is 100
    				int p = (int)(Math.random() * 100);
    
    	// Add new Point int x, y coordinates. But why for the y coordinates need to p-50?
    	// i test it out and the graph shift up, but i don't understand.
    				d_al.add(new Point(0,p-50));
    				}
    			Thread.sleep(2000);
    
    			} catch (InterruptedException e) {}
    				}
    
    			}
    
    	}
    
    	public static void main (String[] args)
    	{
    		new ecg1();
    	}
    }
    Last edited by thestarncy; 12-30-2009 at 11:31 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  3. #3
    thestarncy is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default

    @Fubarable
    Ok its like this. I am making ICU monitoring equipment, I have aranged all other JPanels correctly except the ekg graph that shows heart beat. I got the above code from another site, can you help on how to make it so that it can only be imported into a jpanel, because the area I need to put the graph is a JPanel which is in a JFrame.As the above code stands, its full with JFrame, and i can import a JFrame into a JPanel.

    I hope am making sense here.. thanks though

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Read the code that you have. It's also creating a panel and adding it to a frame. If all you need is the panel, then just instantiate it and add it to your frame as is done in the code.

  5. #5
    thestarncy is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default

    I have tried that, but the code confuses the other JFrame, as i have to create other methods too. The repaint thing, and the setting of the
    Java Code:
     
    for (int i=0; i <15000; i++)//(int i=0; i >15000; i++)
    	{
    		/**Random p1=new Random();
    		int p=p1.nextInt(19)+6;
    		//int p = (int)(Math.random() * 50);//graph size 260
    		vr.add(new Point(0,p-130));// Graph position 130**/
    	}
    and
    Java Code:
    while (true)
    		{
    		try
    		{
    			view.move_1();
    			repaint();
    			Thread.sleep(500);
    		} catch (InterruptedException e) {}
    		}
    is confusing the main JFrame am using.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You are probably better off not "borrowing" the code but instead creating a class on your own that you understand.

  7. #7
    thestarncy is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default

    When I went online for a code, that meant I had no clue how to create it. If you can, please, help me on that, I'll appreciate alot.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    OK, to sum things up, here's what we know about your problem so far: you want to display a graphic on a JPanel that represents a simulated EKG tracing, have the panel update, and place the panel on your JFrame. We see some code that you've borrowed but don't understand and thus can't use as is in your application.

    What we still need to know in order to help is: what you are trying to achieve exactly, how you would break this problem down into its constituent parts, what you have tried yourself as a first good-faith effort to solve this problem, and what exactly you are having problems with.

    I tried to do this, and my code has one class that creates a buffered image of the EKG tracing. This is simply done by creating a BufferedImage, getting its Graphics context object via getGraphics and drawing on this with a bunch of g.drawLine(...) calls and thereby drawing a single EKG complex (look it up on wikipedia as it has a P wave, a QRS wave, an S-T segment and a T wave. Since I wanted to show at least four of these complexes in a row, I had a method that would draw a couple of these images in a row in another BufferedImage. This is done by again using the new BufferedImage's Graphics object, a for loop and drawing the four images in it via g.drawImage(image, x, y, null) (the x increments the width of a single tracing * i -- the loop counter), the y remains 0).

    I then drew this BufferedImage in the main JPanel (the EkgTracing class itself) by overriding its paintComponent method. This will show the EKG tracing. To give it a realistic look, as if the tracing were being made in real time, I gave the JPanel a null layout and added a black overlay panel (a JPanel same size as the main JPanel with its Background set to black) on top of it. I then moved the overlay panel in a Swing Timer by incrementing it's location by calling setLocation(overlayX, 0).

    The bottom line here is:
    1) Find a way to draw the image
    2) Display the image in a JPanel
    3) Cover over the image by another JPanel
    4) Move the covering JPanel in a Swing Timer to expose the underlying image.

    Your way to do this may be different, but the key is to experiment.

    And no, I would rather not give you the code, but I'll try to show you a demo of what I'm talking about in the file linked below. Please change the extension of the file from zip to Jar file to be able run it.
    Attached Files Attached Files

  9. #9
    thestarncy is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default

    I will like to learn, and yes, the example is the perfect graph am loking for, but what if i need to know how you made it? Just let me use your code so that i learn from you.

    Anyway, whichever you may decide, I have appreciated your guidance, if I was a java guru, i could have grasped your ideas immedietely and used them. But, am a beginner with a an assignement.

    Thanks Fubarable. cheers

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Asking for the code won't help you as you really should try on your own. Otherwise, how do you learn? I've given some suggestions above that have worked for me. If you can try them, great, if any confuse you, then by all means ask and let's discuss this further.

  11. #11
    thestarncy is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

Similar Threads

  1. JPanel/JFrame
    By Mayur in forum New To Java
    Replies: 2
    Last Post: 12-20-2009, 05:07 AM
  2. Drawing Graph and Add to JPanel
    By halo3 in forum NetBeans
    Replies: 17
    Last Post: 09-22-2009, 01:31 PM
  3. Embedding Jframe displayin a graph in webpage
    By bharath333 in forum Java 2D
    Replies: 1
    Last Post: 04-23-2009, 08:12 PM
  4. Replies: 5
    Last Post: 02-25-2009, 08:01 AM
  5. how to draw x-y graph in Jpanel.--not in APPLET.
    By vincent2001@gmail.com in forum New To Java
    Replies: 2
    Last Post: 08-24-2008, 05:01 AM

Posting Permissions

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