Question: I have a code with missing components, need assistance with solution. It's a GUI panel that sketches real time motion with the mouse. import javax.swing.*;

I have a code with missing components, need assistance with solution. It's a GUI panel that sketches real time motion with the mouse.

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

public class Project4 extends JPanel implements MouseListener, MouseMotionListener {

private static final int WIDTH = 600; private static final int HEIGHT = 500; int xStart, yStart; int xStop, yStop;

boolean clickedStart = false; boolean mouseDragged = false; BasicStroke aStroke = new BasicStroke(5.0f, BasicStroke.JOIN_ROUND, BasicStroke.CAP_ROUND);

public Project4() { addMouseListener(this); addMouseMotionListener(this); }

@Override public void mouseClicked(MouseEvent arg0) { }

@Override public void mouseEntered(MouseEvent arg0) { }

@Override public void mouseExited(MouseEvent arg0) { }

@Override public void mousePressed(MouseEvent arg0) { System.out.println("Mouse Pressed"); if(clickedStart) { repaint(); System.out.println("Start is set. Draw Line."); } xStart = arg0.getX(); yStart = arg0.getY(); mouseDragged = false; }

@Override public void mouseDragged(MouseEvent arg0) { // System.out.println("Mouse Dragged");

xStop = arg0.getX(); yStop = arg0.getY(); repaint(); xStart = xStop; yStart = yStop; mouseDragged = true; }

@Override public void mouseReleased(MouseEvent arg0) { System.out.println("Mouse Released"); xStop = arg0.getX(); yStop = arg0.getY(); System.out.println("x: " + xStart + ", y: " + yStart); System.out.println("x: " + xStop + ", y: " + yStop); if(clickedStart) { clickedStart = false; } }

@Override Graphics2D gr2D = (Graphics2D) g; g.drawLine(xStart, yStart, xStop, yStop); }

public static void main(String[] args) { JFrame frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

import javaxswing import javaawt import javaawtevent import javaawtgeom public class Project4 extend... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!