Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For Lab 2, you're modifying the Dots and DotsPanel classes from your text to make a simple Paint program. Modify the program to I have

For Lab 2, you're modifying the Dots and DotsPanel classes from your text to make a simple Paint program. Modify the program to

I have no clue how to do this part please help!

  1. Paint with continuous dots by adding a MouseMotionListener that adds points to the pointsList when the mouseDragged event is triggered.

this is what i have so far!

//Dots.java //by Aaron Velazquez //CSCI 1302 //Lab# 2 //01/30/20

package paintDots;

import javax.swing.JFrame;

public class Dots {

public static void main(String[] args) { // TODO Auto-generated method stub JFrame frame = new JFrame("Dots"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

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

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

}

}

===========================================================

//DotsPanel.java //by Aaron Velazquez //CSCI 1302 //Lab# 2 //01/30/20

package paintDots;

import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import javax.swing.JPanel; import java.awt.Point; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.security.SecureRandom; import java.util.ArrayList;

public class DotsPanel extends JPanel {

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

private ArrayList pointList;

//----------------------------------------------------------------- // Constructor: Sets up this panel to listen for mouse events. //----------------------------------------------------------------- public DotsPanel() { pointList = new ArrayList();

addMouseListener (new DotsListener());

setBackground(Color.black); setPreferredSize(new Dimension(300, 200)); }

//----------------------------------------------------------------- // Draws all of the dots stored in the list. //----------------------------------------------------------------- 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.fillOval(spot.x-SIZE, spot.y-SIZE, SIZE*2, SIZE*2);

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

//***************************************************************** // Represents the listener for mouse events. //***************************************************************** private class DotsListener implements MouseListener { //-------------------------------------------------------------- // Adds the current point to the list of points and redraws // the panel whenever the mouse button is pressed. //-------------------------------------------------------------- public void mousePressed(MouseEvent event) { pointList.add(event.getPoint()); repaint(); }

//-------------------------------------------------------------- // Provide empty definitions for unused event methods. //-------------------------------------------------------------- public void mouseClicked(MouseEvent event) {} public void mouseReleased(MouseEvent event) {} public void mouseEntered(MouseEvent event) {} public void mouseExited(MouseEvent event) {} } }

How can i paint with continuous dots when i drag the mouse?

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Spatial Databases 2nd Symposium Ssd 91 Zurich Switzerland August 1991 Proceedings Lncs 525

Authors: Oliver Gunther ,Hans-Jorg Schek

1st Edition

3540544143, 978-3540544142

More Books

Students also viewed these Databases questions

Question

Reconsider the transportation problem formulated in Prob. 9.1-7a.

Answered: 1 week ago