I have a JFrame object in which a JPanel and a JTextArea exist. And in the JPanel, users can use mouse to draw some lines. If the user draw new things in the JPanel, the JTextArea in JFrame can update some information.
How can make the textarea update information about the JPanel, in other words, how can JPanel build a listener to update JTextArea?
Basic code is
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class DrawPanel extends JPanel implements MouseListener, MouseMotionListener
{
DrawPanel()
{
setLayout(null);
addMouseListener(this);
addMouseMotionListener(this);
}
public void mousePressed(MouseEvent e)
{
p1 = p2 = e.getPoint();
Graphics g = getGraphics();
g.setColor(drawColor);
g.setXORMode(getBackground());
g.drawLine(p1.x, p1.y, p2.x, p2.y);
}
public void mouseDragged(MouseEvent e)
{
p3 = e.getPoint();
paintMyComponent();
p2 = p3;
}
void paintMyComponent()
{
Graphics g = getGraphics();
g.setColor(drawColor);
g.setXORMode(getBackground());
g.drawLine(p1.x, p1.y, p2.x, p2.y);//erase
g.drawLine(p1.x, p1.y, p3.x, p3.y); //draw
}
}
class drawpic extends JFrame implements ActionListener
{
DrawPanel myDrawPanel;
JTextArea tf = new JTextArea("",20,20);
//********************************
//When new lines are drawn in DrawPanel, the tf should should update some information about the lines accordingly.
// QUESTIONS: HOW TO MAKE IT???? THANKS!!!!!!!!
//********************************
drawpic()
{
Container cP = getContentPane();
cP.setLayout(new BorderLayout());
myDrawPanel = new DrawPanel();
myDrawPanel.setBounds(0, 50, 600, 550);
cP.add(myDrawPanel);
Container textbn = new Container();
textbn.setLayout(new FlowLayout());
tf.setEditable(false);
cP.add(textbn, BorderLayout.SOUTH);
textbn.add(tf);
}
public static void main(String [] args)
{
drawpic application = new drawpic();
application.setDefaultCloseOperation(JFrame.EXIT_O N_CLOSE);
}
}