Here is my code so far:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.*;
public class Paint extends JFrame {
private static PCanvas canvas = new PCanvas();
public Paint() {
// Create a panel to group three buttons
JPanel p1 = new JPanel(new GridLayout (1, 3));
JButton jbtClear = new JButton("Clear");
JButton jbtFG = new JButton("FG");
JButton jbtBG = new JButton("BG");
jbtClear.setToolTipText("Clears Canvas");
jbtFG.setToolTipText("This changes Foreground Color");
jbtBG.setToolTipText("This changes Background Color");
p1.add(jbtClear);
p1.add(jbtFG);
p1.add(jbtBG);
// Create a panel to group two labels
JPanel p2 = new JPanel(new GridLayout (1, 4));
JButton jbtLine = new JButton("Line");
JButton jbtPolyLine = new JButton("PolyLine");
JButton jbtOval = new JButton("Oval");
JButton jbtFilledOval = new JButton("FilledOval");
jbtLine.setToolTipText("Creates a line");
jbtPolyLine.setToolTipText("Creates a Poly Line");
jbtOval.setToolTipText("Creates an oval");
jbtFilledOval.setToolTipText("Creates a filled oval");
p2.add(jbtLine);
jbtLine.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
canvas.line();
}
});
p2.add(jbtPolyLine);
p2.add(jbtOval);
p2.add(jbtFilledOval);
// Create a panel to group two labels
JPanel p3 = new JPanel(new GridLayout (1, 4));
JButton jbtRectangle = new JButton("Rectangle");
JButton jbtFilledRectangle = new JButton("FilledRectangle");
JButton jbtRoundRectangle = new JButton("RoundRectangle");
JButton jbtFilledRoundRectangle = new JButton("FilledRoundRectangle");
jbtRectangle.setToolTipText("Creates a Rectangle");
jbtFilledRectangle.setToolTipText("Creates a Filled Rectangle");
jbtRoundRectangle.setToolTipText("Creates a round rectangle");
jbtFilledRoundRectangle.setToolTipText("Creates a filled round rectangle");
p3.add(jbtRectangle);
p3.add(jbtFilledRectangle);
p3.add(jbtRoundRectangle);
p3.add(jbtFilledRoundRectangle);
// Add two panels to the frame
setLayout(new GridLayout(4, 1, 1, 1));
add(canvas, BorderLayout.NORTH);
add(p1);
add(p2);
add(p3);
}
public static void main(String[] args) {
// Create a frame and set its properties
JFrame frame = new Paint();
frame.setTitle("TestSwingCommonFeatures");
frame.setSize(700, 700);
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static class PCanvas extends JPanel {
private int x = 0;
private int y = 0;
public void line(){
addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
x = e.getX();
y = e.getY();
System.out.println(x + y);
repaint();
}
});
}
/** Repaint the line */
protected void paintComponent(Graphics g) {
super.paintComponent(g);
}
}
}
For now, I just want to foucs on drawing a line by clicking on the line button and dragging the mouse to draw the line on the canvas. I'm a beginner at this, can someone help me. I believe what do I do so when I click on the canvas and drag the mouse, a line comes out and stays on it? Thanks in advance!