尝试分配鼠标事件。这个想法是,当用户点击鼠标时,程序绘制一个小绿点,当鼠标被释放时,它应该创建一个小橙色点,向下5像素,右边5像素。作业需要三个程序,Dots, DotsPanel和DrawDots。该程序还应该有一个总点数计数器。现在程序可以编译了,但是程序完全崩溃了,当我试图运行它时,它给了我大量的错误。我现在有点迷路了,因为我是这个GUI和用户mouseEvents的新手。谢谢你!我特别遇到了如何将Dots类转换为Point的麻烦。
这是说明书的副本:
修改"Dots"示例。包括一个Dots类,该类包含一个构造函数,用于初始化圆点的颜色和点位置。Dots类还应该包括两个访问器方法。ArrayList现在将包含一个要绘制的点的列表,而不是点。当按下鼠标时,程序绘制一个绿色的圆圈(与原始设计一样)。当鼠标被释放时,程序绘制一个黄色圆圈,从释放位置向右平移5像素,向下平移5像素。一定要记录点的总数。你应该有三个文件:Dots, DotsPanel, DrawDots。(10分)
/**
* @(#)Dots.java
*
*
* @author
* @version 1.00 2015/5/16
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Dots
{
Point p;
Color col;
public Dots(Color c, Point x)
{
p=x;
col=c;
}
public Point getPointP()
{
return p.getLocation();
}
public Color getPointC()
{
return col;
}
}
…
//represents the primary panel for the Dots program on which the
//dots are drawn
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class DotsPanel extends JPanel
{
private final int WIDTH = 300, HEIGHT = 300;
private final int RADIUS = 6;
private ArrayList<Dots> pointList;
private int count;
//Sets up this panel to listen for mouse events.
public DotsPanel()
{
pointList = new ArrayList<Dots>();
count = 0;
addMouseListener(new DotsListener());
setBackground(Color.black);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
}
//Draws all of the dots stored in the list
public void paintComponent(Graphics page)
{
super.paintComponent(page);
//Retrieve an iterator for the ArrayList of points
Iterator pointIterator = pointList.iterator();
while(pointIterator.hasNext())
{
page.setColor(Color.green);
Point drawPoint = (Point) pointIterator.next();
page.fillOval(drawPoint.x - RADIUS, drawPoint.y - RADIUS,
RADIUS * 2, RADIUS * 2);
page.setColor(Color.yellow);
Point drawPoint2 = (Point) pointIterator.next();
page.fillOval(drawPoint2.x - RADIUS+5, drawPoint2.y - RADIUS+5,
RADIUS * 2, RADIUS * 2);
}
page.drawString("Count: " + count, 5, 15);
}
//represents the lister for mouse events
private class DotsListener implements MouseListener
{
//Adds the current point to the list of points and redraws
//whenever the mouse button is pressed
public void mousePressed(MouseEvent event)
{
Dots dot1 = new Dots(Color.GREEN, event.getPoint());
pointList.add(dot1);
count++;
repaint();
}
public void mouseReleased(MouseEvent event)
{
Dots dot2 = new Dots(Color.YELLOW, event.getPoint());
pointList.add(dot2);
count++;
repaint();
}
//Provide empty definitions for unused event methods
public void mouseClicked(MouseEvent event) {}
public void mouseEntered(MouseEvent event) {}
public void mouseExited(MouseEvent event) {}
}
}
…
//demonstrates mouse events and drawing on a panel
import javax.swing.*;
public class DrawDots
{
//Creates and displays the application frame
public static void main(String[] args)
{
JFrame dotsFrame = new JFrame("Dots");
dotsFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
dotsFrame.getContentPane().add(new DotsPanel());
dotsFrame.pack();
dotsFrame.setVisible(true);
}
}
首先应该使用Iterator<Dots> pointIterator = pointList.iterator();
。这有助于编译器在不可浇注类型的情况下警告您(这是您得到的第一个错误)。在paintComponent
方法中,您应该稍后使用Point drawPoint = pointIterator.next().getPointP();
和Point drawPoint2 = pointIterator.next().getPointP();
视图线。
修改后程序运行(至少在我的机器上)