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!

Recommended Answers

You can override the paint method of your canvas, which you have already done and then call g.drawLine(x1,y1,x2,y2). where x1 y1 etc. are the points which the line is drawn between. Look at the Graphics and Graphics2D api for more info.

Jump to Post

All 2 Replies

Here is an example that was in my textbook. Maybe this will help.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class SketchPad extends JFame implements MouseListener, MouseMotionListener{
    private int last_x;
    private int last_y;

    public static void main(String[] args){
        SketchPad frame = new SketchPad();
        frame.setVisible(true);
    }

    public SketchPad(){
        setTitle("SketchPad");
        setSize(450. 300);
        setResizable(false);
        setLocation(150, 200);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        last_x = last_y = 0;

        addMouseListener(this);
        addMouseMotionListener(this);
    }

//Mouse Event Handling

    public void mousePressed(MouseEvent event){
        int x = event.getX();
        int y - event.getY();

        if(event.isMetaDown()){
            Graphics g = getGraphics();
            Rectangle r = getBounds();
            g.ClearRect(0, 0, r.width, r.height);
            g.dispose();
        } else {
            last_x = x;
            last_y = y;
        }
    }

    public void mouseClicked(MouseEvent event){}
    public void mouseEntered(MouseEvent event){}
    public void mouseExited(MouseEvent event){}
    public void mouseReleased(MouseEvent event){}

    public void mouse Dragged(MouseEvent event){
        int x = event.getX();
        int y = event.getY();

        if(!event.isMetaDown()) {
            //don't process the right button drag
            Graphics g = getGraphics();

            g.drawLine(last_x, last_y, x, y);
            g.dispose();

            last_x = x;
            last_y = y;
        }
    }

    public void mouseMoved(MouseEvent event) {}
}

I got this from "A Comprehensive Introduction to Object-Oriented Programming with Java" by C. Thomas Wu

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.