让每个点使用不同的随机颜色并保持相同的颜色,而不必使用dr.java更改每个点的相同颜色时遇到麻烦。它将反复将旧点和新点更改为相同的颜色,并在制作新点时将一个点保留为旧颜色
import java.util.ArrayList;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class DotsPanel extends JPanel
{
private final int SIZE = 6;
private ArrayList<Point> pointList;
public DotsPanel()
{
pointList = new ArrayList<Point>();
addMouseMotionListener (new DotsListener());
addMouseListener (new DotsListener());
setBackground(Color.black);
setPreferredSize(new Dimension(500, 500));
}
当我放置一个新的点时,它不能只画一个,它将旧的点也更改为新的颜色
public void paintComponent(Graphics page)
{
super.paintComponent(page);
Random rand= new Random();
int r=rand.nextInt(255);
int g=rand.nextInt(255);
int b=rand.nextInt(255);
page.setColor(new Color(r,g,b));
for (Point spot : pointList)
{
page.fillOval(spot.x-SIZE, spot.y-SIZE, SIZE*2, SIZE*2);
}
}
private class DotsListener implements MouseMotionListener, MouseListener
{
public void mouseDragged(MouseEvent event)
{
pointList.add(event.getPoint());
repaint();
////////
/////// was unable to put the page color down here it would not recognize it.
}
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) {}
public void mouseMoved(MouseEvent event) {}
}
}