【发布时间】:2014-03-04 20:37:53
【问题描述】:
我正在尝试为学校完成一个项目。当我得到它时,它已部分完成。我无法测试我写的任何东西(除了布局,通过注释掉错误)。我有一个“无法将矩形解析为类型”的错误。以为我做错了什么,我在网上找到了这个完整的代码,我想我会看看有什么不同。我在这里遇到同样的错误……很多。是什么赋予了?注意*我不是想把这个程序传递进去,只是想看看它是如何工作的,因为我希望我的也做同样的事情。
/**
* DrawRects.java
*
* Allows the user to enter a number of rectangles using mouse input.
* Keeps previous rectangles around.
* Inspired by a C++ class demo by THC
*
* @author Scot Drysdale on 4/19/00. Modified to a JApplet 1/16/2012
* Modified to add a "clear" button and use an ArrayList on 1/18/2012
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
public class DrawRects extends JApplet implements MouseListener,
MouseMotionListener {
private static final long serialVersionUID = 1L;
private Point pressedPoint = null; // place where mouse pressed down
private Rect currentRect = null; // rectangle being dragged.
private ArrayList<Rect> boxes = new ArrayList<Rect>(); // a list of rectangles
private static final Color[] colors = { Color.red, Color.cyan, Color.magenta,
Color.yellow };
private int colorIndex = 0; // index into colors of current color
private JButton clearButton; // Button to clear the screen
private static final int APPLET_WIDTH = 520; // Width of the applet
private static final int APPLET_HEIGHT = 550; // Height of the applet
private static final int CANVAS_WIDTH = 500; // Width of the canvas
private static final int CANVAS_HEIGHT = 500; // Height of the applet
/**
* Initializes the applet
*/
public void init() {
addMouseListener(this);
addMouseMotionListener(this);
setSize(APPLET_WIDTH, APPLET_HEIGHT);
Container cp = getContentPane(); // Content pane holds components
cp.setLayout(new FlowLayout()); // Fill left to right, top to bottom
Canvas canvas = new Canvas();
cp.add(canvas); // The canvas is the only component
// Make a button to clear the canvas, set the button's background
// to cyan, and add it to the content pane.
clearButton = new JButton("Clear");
clearButton.setBackground(Color.cyan);
clearButton.addActionListener(canvas);
cp.add(clearButton);
setVisible(true); // Makes the applet (and its components) visible
}
// Captures the position at which the mouse is initially pressed.
// It creates a new currentRect object, because the previous one
// will have been added to the ListOfRects.
public void mousePressed(MouseEvent event) {
pressedPoint = event.getPoint();
currentRect = new Rect(pressedPoint.x, pressedPoint.y, 0, 0, Color.black);
}
/**
* Gets the current position of the mouse as it is dragged and draws a
* rectangle with this point and pressedPoint as corners. This creates a
* rubberbanding rectangle effect.
*
* @param event the event that caused this callback
*/
public void mouseDragged(MouseEvent event) {
if (currentRect != null) { // make sure that currentRect exists
Point pt = event.getPoint();
currentRect.setX(Math.min(pt.x, pressedPoint.x));
currentRect.setY(Math.min(pt.y, pressedPoint.y));
currentRect.setWidth(Math.abs(pt.x - pressedPoint.x));
currentRect.setHeight(Math.abs(pt.y - pressedPoint.y));
repaint();
}
}
/**
* Done dragging mouse, so add current Rect to ListOfRects.
*
* @param event the event that caused this callback
*/
public void mouseReleased(MouseEvent event) {
if (currentRect != null) { // make sure that currentRect exists
currentRect.setColor(colors[colorIndex]);
colorIndex = (colorIndex + 1) % colors.length;
boxes.add(currentRect);
currentRect = null; // currentRect now in the list, so can't reuse it
}
repaint();
}
// Provide empty definitions for unused event methods.
public void mouseClicked(MouseEvent event) {}
public void mouseEntered(MouseEvent event) {}
public void mouseExited(MouseEvent event) {}
public void mouseMoved(MouseEvent event) {}
/**
* The canvas to draw upon
*/
private class Canvas extends JPanel implements ActionListener {
private static final long serialVersionUID = 1L;
/**
* Constructor to choose preferred size
*/
public Canvas() {
// Canvas is a subclass of JPanel. The way we set the size of
// a JPanel is by the setPreferredSize method. It takes a reference to
// a Dimension object, which just packages together a width and height.
setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
}
/**
* Draw the rectangles
*
* @param page the graphics object to draw on
*/
public void paintComponent(Graphics page) {
super.paintComponent(page);
page.setColor(Color.black);
page.drawRect(0, 0, CANVAS_WIDTH - 1, CANVAS_HEIGHT - 1); // Draw border
for (Rect rectangle : boxes) // Draw the saved rectangles
rectangle.fill(page);
if (currentRect != null) // Draw the rectangle being dragged out (if exists)
currentRect.draw(page);
}
/**
* Handle the button - provide an actionListener
* @param event the event that caused this callback
*/
public void actionPerformed(ActionEvent event) {
boxes.clear();
repaint();
}
}
}
【问题讨论】:
-
Rect 可能是您错过从复制此代码的位置复制的类
-
您正在使用一个 Rect 类,它不是您导入的任何包的一部分。
-
这是否意味着它不是标准的 java 类?因为它不在我的作业文件中,所以用 ctrl-a 复制/粘贴了这个程序。我想我可以理解我是否应该为我的家庭作业写一个“矩形”类。我只是对这个计划感到困惑,我希望它是完整的。
-
@user3277465 是的,它不是标准的 Java 类。也许您忽略了另一个文件,或者提供给您的来源不完整。