2014-03-04 46 views
0

我正在努力为学校完成一个项目。我得到它时已部分完成。我无法测试我所写的任何内容(除了布局,通过评论错误)。我有一个错误“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(); 
      } 
     } 
    } 
+2

Rect可能是您错过了从您复制此代码的位置复制的类 – fmodos

+1

您正在使用的类Rect不属于您导入的任何软件包的一部分。 –

+0

这是否意味着它不是一个标准的java类?因为它不在我的作业文件中,并且用ctrl-a复制/粘贴这个程序。我想我可以理解,如果我希望为我的作业写一个“Rect”类。我只是对这个计划感到困惑,我期望完成。 – user3277465

回答

0

SDK中没有任何名称为Rect。当然,java.awt.Rectangle,这可能会帮助你的任务。

但是,看起来Rect只是您抓住的项目中的一些其他类,而您忘记抓取源代码(或者作者未提供该源代码)。

它不会需要一个特殊的import或任何东西,如果它是在同一个包DrawRects(默认的包,通过它的外观),或者如果它是,比方说,一个内部类的DrawRects(它不是)。

+1

原来他忘了提及我们应该写一个名为“Rect”的类。 – user3277465

+1

得到了编写的程序,并转入。等待我的分数。 – user3277465