⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 paintpanel.java

📁 用java画动态图,包括圆,直线,矩形等,且图形是运动的
💻 JAVA
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;

public class PaintPanel extends JPanel
                        implements MouseListener, MouseMotionListener,
                                   ActionListener {
   private Vector shapes;   // dynamically resizeable array of Objects from java.util
   private Shape currentShape;
   private String currentShapeType = "Line";   // default
   private Color currentColor = Color.black;
   Timer timer = new Timer( 40, this );

   public PaintPanel()
   {
      shapes = new Vector();   // creates empty Vector
      addMouseListener( this );
      addMouseMotionListener( this );
      setBackground( Color.white );
      timer.start();
   }

   public void paintComponent( Graphics g )
   {
      super.paintComponent( g );

      Shape current;

      for ( int i = 0; i < shapes.size(); i++ ) {
         current = ( Shape ) shapes.elementAt( i );
         current.draw( g );
      }
   }

   public void actionPerformed( ActionEvent e )
   {
      repaint();
   }

   /*
      In the following method, we use features that have not been covered in
      the text. We could have used an if/else structure to choose which shape
      to create. Instead,

         Class c = Class.forName( currentShapeType );

      loads the class definition for the class with the String name stored in
      currentShapeType. The statement

         currentShape = (Shape) c.newInstance();

      creates a new instance of the class that was loaded. The next line adds
      the new shape to the Vector (at the end).

      The try/catch syntax is covered in chapter 14. It is required here
      because both the Class.forName method call and the c.newInstance method
      call can cause runtime exceptions (problems). In this case we are
      saying "if a runtime problem occurs, ignore it."
   */
   public void mousePressed( MouseEvent e )
   {
      try {         
         Class c = Class.forName( currentShapeType );
         currentShape = (Shape) c.newInstance();
         currentShape.setX1( e.getX() );
         currentShape.setY1( e.getY() );
         currentShape.setX2( e.getX() );
         currentShape.setY2( e.getY() );
         currentShape.setColor( currentColor );
         shapes.addElement( currentShape );
      }
      catch (Exception ex) { ex.printStackTrace(); }
   }

   public void mouseReleased( MouseEvent e )
   {
      currentShape.setX2( e.getX() );
      currentShape.setY2( e.getY() );

      // NOTE: We should store these for termination later.
      MovingShape mover =
         new MovingShape( currentShape, this,
                          (int)(Math.random() * 5 + 1),
                          (int)(Math.random() * 5 + 1) );
      mover.start();

      repaint();
   }

   public void mouseClicked( MouseEvent e )
   {

   }

   public void mouseEntered( MouseEvent e )
   {

   }

   public void mouseExited( MouseEvent e )
   {

   }

   public void mouseMoved( MouseEvent e )
   {

   }

   public void mouseDragged( MouseEvent e )
   {
      currentShape.setX2( e.getX() );
      currentShape.setY2( e.getY() );
      repaint();
   }

   public void setCurrentShapeType( String s )
   {
      currentShapeType = s;
   }

   public void setCurrentColor( Color c )
   {
      currentColor = c;
   }
}


 /**************************************************************************
  * (C) Copyright 1992-2004 by Deitel & Associates, Inc. and               *
  * Pearson Education, Inc. All Rights Reserved.                           *
  *                                                                        *
  * DISCLAIMER: The authors and publisher of this book have used their     *
  * best efforts in preparing the book. These efforts include the          *
  * development, research, and testing of the theories and programs        *
  * to determine their effectiveness. The authors and publisher make       *
  * no warranty of any kind, expressed or implied, with regard to these    *
  * programs or to the documentation contained in these books. The authors *
  * and publisher shall not be liable in any event for incidental or       *
  * consequential damages in connection with, or arising out of, the       *
  * furnishing, performance, or use of these programs.                     *
  **************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -