我正在努力为学校完成一个项目。我得到它时已部分完成。我无法测试我所写的任何内容(除了布局,通过评论错误)。我有一个错误“Rect无法解析为某种类型”。以为我做错了,我发现这个完整的代码在网上发布,并认为我会看到有什么区别。我在这里得到了相同的错误...很多。是什么赋予了?注意*我没有试图通过这个程序,只是想看看它是如何工作的,因为我希望我的同样的事情。什么是“Rect”类?
/**
* 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可能是您错过了从您复制此代码的位置复制的类 – fmodos
您正在使用的类Rect不属于您导入的任何软件包的一部分。 –
这是否意味着它不是一个标准的java类?因为它不在我的作业文件中,并且用ctrl-a复制/粘贴这个程序。我想我可以理解,如果我希望为我的作业写一个“Rect”类。我只是对这个计划感到困惑,我期望完成。 – user3277465