Hey all,
My program creates panels with textfields. The actual goal is to draw lines between the textfields. But i'm stuck way before that stage. I can draw lines with the button connection lines and you click on two points and it draws a line. The 2 problems I have are:
1. The panels with textfields disappear when I draw a line, and they shouldn't.
2. Even though I create an array such that I can draw many lines, the previous line disappears when I draw a new one.
I think it's got to do something with the repaint() method but I don't know how to fix it..
Thanks for your help/advice!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.io.*;
public class Try8 extends JFrame implements MouseListener
{
public int i=0;
public int k=10;
public int b=20;
public int m=-1;
public int c=1;
public int d=0;
public int f=0;
public int a =0;
public int x;
public int y;
public int z=0;
public JPanel [] panel= new JPanel[31];
public JTextField [] text=new JTextField[16];
public ConnectionLines [] cl = new ConnectionLines[50];
public int [] xvalues = new int [50];
public int [] yvalues = new int [50];
public int [] cnt= new int [3];
public Try8()
{
JPanel p = new JPanel();
p.setLayout(new GridLayout(6,1, 5, 5));
JButton b1 = new JButton("input space");
JButton b2 = new JButton("generic space");
JButton b3 = new JButton("blend space");
JButton b4 = new JButton("remove textfield");
JButton b5 = new JButton("textfield");
JButton b6 = new JButton("connection line");
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
p.add(b5);
p.add(b6);
add(p, BorderLayout.WEST);
final JPanel q = new JPanel();
q.setLayout(new BorderLayout());
final JPanel r = new JPanel();
final JPanel s = new JPanel();
final JPanel t = new JPanel();
add(q, BorderLayout.CENTER);
q.add(r, BorderLayout.CENTER);
q.add(s, BorderLayout.EAST);
q.add(t, BorderLayout.SOUTH);
for (i=0; i<=30; i++)
{
panel[i]=new JPanel();
panel[i].addMouseListener(this);
panel[i].setName("" + i);
}
i=0;
cnt[0]=-1;
cnt[1]=-1;
cnt[2]=-1;
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
d=d+1;
i=i+1;
text[1]=new JTextField(10);
r.add(panel[i]);
panel[i].setLayout(new GridLayout(1,1,6,5));
panel[i].setBorder(new TitledBorder("input " + i));
panel[i].add(text[1]);
//panel[i].add(h);
}
});
b2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
d=d+1;
k=k+1;
text[1]=new JTextField(10);
s.add(panel[k]);
panel[k].setLayout(new GridLayout(1,1,6,5));
panel[k].setBorder(new TitledBorder("generic " + (k-10)));
panel[k].add(text[1]);
}
});
b3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
d=d+1;
b=b+1;
text[1]=new JTextField(10);
t.add(panel[b]);
panel[b].setLayout(new GridLayout(1,1,6,5));
panel[b].setBorder(new TitledBorder("blend "));
panel[b].add(text[1]);
}
});
b4.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (m==-1) {}
else
{
c = panel[m].getComponentCount();
if (c ==1) {}
else
{
Component component = panel[m].getComponent(c-1);
panel[m].remove(component);
c = panel[m].getComponentCount();
panel[m].setLayout(new GridLayout(c,1,6,5));
validate();
}
}
}
});
b5.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (m==-1) {}
else
{
if (c==14) {}
else
{
c = panel[m].getComponentCount();
text[c+1]=new JTextField(10);
panel[m].add(text[c+1]);
c = panel[m].getComponentCount();
panel[m].setLayout(new GridLayout(c,1,6,5));
validate();
}
}
}
});
b6.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
f=f+1;
cl[f] = new ConnectionLines();
q.add(cl[f]);
validate();
}
});
}
public void mousePressed(MouseEvent e)
{}
public void mouseReleased(MouseEvent e)
{}
public void mouseEntered(MouseEvent e)
{}
public void mouseExited(MouseEvent e)
{}
public void mouseClicked(MouseEvent e)
{
String name = e.getComponent().getName();
m = Integer.parseInt(name);
x = e.getX();
y = e.getY();
Point point= e.getPoint();
point.translate(x, y);
System.out.println(name);
System.out.println("x is " + e.getX());
System.out.println("y is " + e.getY());
System.out.println(point);
System.out.println(x + " " + y);
}
public static void main(String args[])
{
Try8 frame = new Try8();
frame.setTitle("let's try");
frame.setSize (400, 400);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible (true);
}
public class ConnectionLines extends JPanel
{
public ConnectionLines()
{
addMouseListener(new MouseListener()
{
public void mouseClicked(MouseEvent e)
{
if (z==2)
{
z=0;
}
xvalues[a]=e.getX();
yvalues[a]=e.getY();
cnt[z]=1;
System.out.println(a);
System.out.println(xvalues[0]);
System.out.println(yvalues[0]);
System.out.println("");
System.out.println(xvalues[1]);
System.out.println(yvalues[1]);
System.out.println("");
repaint();
}
public void mousePressed(MouseEvent e)
{}
public void mouseReleased(MouseEvent e)
{}
public void mouseEntered(MouseEvent e)
{}
public void mouseExited(MouseEvent e)
{}
});
}
protected void paintComponent( Graphics g)
{
super.paintComponent(g);
if((cnt[0] != -1) && (cnt[1] != -1))
{
g.drawLine(xvalues[a-1],yvalues[a-1],xvalues[a],yvalues[a]);
cnt[0] = -1;
}
z=z+1;
a=a+1;
}
}
}