Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have Dots.java and DotsPanel.java. I need to add a mouseDragged Listener to it

ID: 3885475 • Letter: I

Question

I have Dots.java and DotsPanel.java. I need to add a mouseDragged Listener to it so that when you click and hold the mouse and drag it across the screen, it creates multiple squares.

Dots.java

//Dots.java
//Michael Dimond Jr
//CSCI 1302
//Lab #2
//9/6/2017

import javax.swing.JFrame;

public class Dots
{
//-----------------------------------------------------------------
// Creates and displays the application frame.
//-----------------------------------------------------------------
public static void main(String[] args)
{
JFrame frame = new JFrame("Dots");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().add(new DotsPanel());

frame.pack();
frame.setVisible(true);
}
}

DotsPanel.java

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JPanel;
import javax.swing.Timer;

public class DotsPanel extends JPanel {

private final int SIZE = 6; // radius of each dot

private ArrayList<Point> pointList;

AnimationListener am;

// -----------------------------------------------------------------

// Constructor: Sets up this panel to listen for mouse events.

// -----------------------------------------------------------------

public DotsPanel() {

pointList = new ArrayList();

addMouseListener(new DotsListener());
  
am = new AnimationListener();

am.timer.start();

setBackground(Color.black);

setPreferredSize(new Dimension(300, 200));

}

private void setColor(Color color) {

}

public void paint(Graphics page) {

super.paintComponent(page);

SecureRandom randomNum = new SecureRandom();

Color color = new Color(randomNum.nextInt(256), randomNum.nextInt(256),

randomNum.nextInt(256));

page.setColor(color);

for (Point spot : pointList)

page.fillRect(spot.x - SIZE, spot.y - SIZE, SIZE * 2, SIZE * 2);

page.drawString("Count: " + pointList.size(), 5, 15);

}


  
private class AnimationListener implements ActionListener {
int xRand[];
int yRand[];
Random rand;

public AnimationListener() {
rand = new Random();

xRand = new int[1000];
yRand = new int[1000];
  

for (int i = 0; i < 1000; i++) {
xRand[i] = rand.nextInt(20) - 10;
yRand[i] = rand.nextInt(20) - 10;
}

}
  

private Timer timer = new Timer(50, this);


public void actionPerformed(ActionEvent e) {

Rectangle window = getBounds();

for (int i = 0; i < pointList.size(); i++) {
Point spot = pointList.get(i);
spot.x += xRand[i];
spot.y += yRand[i];


if (spot.x <= 0) {
xRand[i] = Math.abs(xRand[i]);
} else if (spot.x >= window.width - (SIZE*2)) {
xRand[i] = -Math.abs(xRand[i]);
}


if (spot.y <= 0) {
yRand[i] = Math.abs(yRand[i]);
} else if (spot.y >= window.height - (SIZE*2)) {
yRand[i] = -Math.abs(yRand[i]);
}
}

repaint();

}

}
  

  

private class DotsListener implements MouseListener {

public void mousePressed(MouseEvent event) {

pointList.add(event.getPoint());

repaint();

}

public void mouseClicked(MouseEvent event) {

}

public void mouseReleased(MouseEvent event) {

}

public void mouseEntered(MouseEvent event) {

}

public void mouseExited(MouseEvent event) {

}

}

}

Explanation / Answer


Dots.java
---------------

import javax.swing.JFrame;
public class Dots {
// -----------------------------------------------------------------
// Creates and displays the application frame.
// -----------------------------------------------------------------
public static void main(String[] args) {
JFrame frame = new JFrame("Dots");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new DotsPanel());
frame.pack();
frame.setVisible(true);
}
}
---------------- End--------------------------
DotsPanel.java
-----------------------
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JPanel;
import javax.swing.Timer;
public class DotsPanel extends JPanel {
private final int SIZE = 6; // radius of each dot
private ArrayList<Point> pointList;
AnimationListener am;
// -----------------------------------------------------------------
// Constructor: Sets up this panel to listen for mouse events.
// -----------------------------------------------------------------
public DotsPanel() {
pointList = new ArrayList<Point>();
// addMouseListener(new DotsListener());
addMouseMotionListener(new DotsMouseMotionListener());
am = new AnimationListener();
am.timer.start();
setBackground(Color.black);
setPreferredSize(new Dimension(300, 200));
}
private void setColor(Color color) {
}
public void paint(Graphics page) {
super.paintComponent(page);
SecureRandom randomNum = new SecureRandom();
Color color = new Color(randomNum.nextInt(256), randomNum.nextInt(256),
randomNum.nextInt(256));
page.setColor(color);
for (Point spot : pointList)
page.fillRect(spot.x - SIZE, spot.y - SIZE, SIZE * 2, SIZE * 2);
page.drawString("Count: " + pointList.size(), 5, 15);
}
private class AnimationListener implements ActionListener {
int xRand[];
int yRand[];
Random rand;
public AnimationListener() {
rand = new Random();
xRand = new int[1000];
yRand = new int[1000];
for (int i = 0; i < 1000; i++) {
xRand[i] = rand.nextInt(20) - 10;
yRand[i] = rand.nextInt(20) - 10;
}
}
private Timer timer = new Timer(50, this);
public void actionPerformed(ActionEvent e) {
Rectangle window = getBounds();
for (int i = 0; i < pointList.size(); i++) {
Point spot = pointList.get(i);
spot.x += xRand[i];
spot.y += yRand[i];
if (spot.x <= 0) {
xRand[i] = Math.abs(xRand[i]);
} else if (spot.x >= window.width - (SIZE * 2)) {
xRand[i] = -Math.abs(xRand[i]);
}
if (spot.y <= 0) {
yRand[i] = Math.abs(yRand[i]);
} else if (spot.y >= window.height - (SIZE * 2)) {
yRand[i] = -Math.abs(yRand[i]);
}
}
repaint();
}
}
private class DotsListener implements MouseListener {
public void mousePressed(MouseEvent event) {
pointList.add(event.getPoint());
repaint();
}
public void mouseClicked(MouseEvent event) {
}
public void mouseReleased(MouseEvent event) {
}
public void mouseEntered(MouseEvent event) {
}
public void mouseExited(MouseEvent event) {
}
}
private class DotsMouseMotionListener implements MouseMotionListener {
@Override
public void mouseDragged(MouseEvent event) {
System.out.println(event.getPoint().toString());
pointList.add(event.getPoint());
repaint();
}
@Override
public void mouseMoved(MouseEvent e) {
}
}
}
-------------- End-------------------------
Description
----------------
1. We need to run main method of Dots.java class to get the desired output.
2. I have made two changes in exiting code which is mentioned in actual question.
A. Added one private class : DotsMouseMotionListener
B. changed the actionListener from DotsMouseMotionListener() so that mousedragged event can be captured.
C. Now Multiple squares shapes randered on mouse dragged event.
Output
------------
You have press mouse and drag it to some other palce on screen or frame and you will find that multiple squares will be rendering there.
Ealier one single square shape was randered when user clicked on screen.
Thanks, Let me knwo if you need more assistence on same.