尝试执行鼠标事件分配。这个想法是当用户点击鼠标时程序绘制一个小绿点,当鼠标被释放时,它应该创建一个小的橙色点,向下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);
}
}
答案 0 :(得分:0)
首先,您应该使用Iterator<Dots> pointIterator = pointList.iterator();
。这有助于编译器在uncastable类型的情况下发出警告(这是你得到的第一个错误)。在paintComponent
方法中,您应该稍后使用Point drawPoint = pointIterator.next().getPointP();
和Point drawPoint2 = pointIterator.next().getPointP();
视图行。
在修改之后,程序会运行(至少在我的机器上)。< / p>