hello i m writing a program in which i try to write on the frame by mousemotionlistener and mouselistener methods till now i did the writing part and i i want to start that writing by clicking the button Ok then another frame is open in which i can write anything all the code is done in java format.
public class Paint {
JFrame f = new JFrame;
JButton ok = new JButton("OK");
public Paint() {
f.getContentPane().add(ok);
bok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
f.dispose();
new new2();
}
});
f.setSize(100,100);
f.setVisible(true);
}
public static void main(String[] args) {
new Paint();
}}
and by using mouseevents methods for the writing in new2 class
in above class call the new2 to open up new frame in which writing is to be done.
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;
class new2 extends JPanel implements MouseListener,MouseMotionListener
{
int xPressed,yPressed;
int xReleased,yReleased;
int xDragged,yDragged;
JFrame f = new JFrame();
public new2()
{
System.out.println("Frame ");
f.setSize(500, 500);
f.show();
addMouseListener(this);
addMouseMotionListener(this);
System.out.println("start");
}
public void paint(Graphics g)
{
System.out.println("draw");
g.setColor(Color.blue);
g.drawLine(xPressed,yPressed,xDragged,yDragged);
xPressed=xDragged;
yPressed=yDragged;
}
public void mouseDragged(MouseEvent me) {
System.out.println("Dragged");
Graphics g = getGraphics();
g.drawLine(xPressed, yPressed, me.getX(), me.getY());
xDragged = me.getX();
yDragged = me.getY();
repaint();
}
public void mouseMoved(MouseEvent me) {}
public void mouseClicked(MouseEvent me) {}
public void mouseEntered(MouseEvent me) {}
public void mouseExited(MouseEvent me) { }
public void mousePressed(MouseEvent me) {
System.out.println("pressed");
xPressed = me.getX();
yPressed = me.getY();
}
public void mouseReleased(MouseEvent me) {}
}