Hi people, i did a little modification to the code extracted from this web: http://forums.sun.com/thread.jspa?threadID=211818&start=15&tstart=0
The default code has a JFrame itself. I'm modifying such that instead extending JFrame, i extend JPanel and then calling it from another class or from a main method that i created in the code itself which create a JFrame and append the JPanel to JFrame.
The code runs with no problem, however my JFrame isn't appearing. I had setVisible to True.
Would appreciate if someone here can help me.
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 JPanel
{
myView view = new myView();
ArrayList al = new ArrayList();
data a;
JPanel p = new JPanel();
public JPanel getECG(){
return p;
}
public ecg1()
{
//p.setBounds(0,0,800,200);
p.add(view);
p.setLayout(null);
a = new data(al);
setVisible(true);
while (true)
{
try
{
view.move_1();
repaint();
Thread.sleep(100);
} catch (InterruptedException e) {}
}
}
private class myView extends JPanel
{
BufferedImage I;
Graphics2D G;
int point_s = 4;
public myView()
{
setBounds(0,0,800,200);
I = new BufferedImage(getWidth(),getHeight(),BufferedImage.TYPE_INT_ARGB);
G = I.createGraphics();
G.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
G.setColor(Color.white);
G.fillRect(0,0,800,200);
}
public void clear(int x, int w)
{
G.setColor(Color.white);
G.fillRect(x,0,w,getHeight());
}
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.gray);
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)
{
int p = (int)(Math.random() * 100);
d_al.add(new Point(0,p-50));
}
Thread.sleep(2000);
} catch (InterruptedException e) {}
}
}
}
public static void main(String[] args){
initGUI();
}
public static void initGUI() {
JFrame f = new JFrame("Frame ");
f.setSize(1200, 800);
//ecg1 ecgGraph = new ecg1();
//f.add(ecgGraph.getECG());
/*I tried both above and below method, it doesn't work*/
//f.add(new ecg1());
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}