imagepanel.java

来自「用java模拟的电梯程序,可以选择有几层楼,一共有几个人在哪几层等电梯,可以不断」· Java 代码 · 共 119 行

JAVA
119 行
字号
// ImagePanel.java// JPanel subclass for positioning and displaying ImageIconpackage com.Anance.elevator.view;// Java core packagesimport java.awt.*;import java.awt.geom.*;import java.util.*;// Java extension packagesimport javax.swing.*;public class ImagePanel extends JPanel {   // identifier   private int ID;   // on-screen position   private Point2D.Double position;   // imageIcon to paint on screen   private ImageIcon imageIcon;   // stores all ImagePanel children   private Set panelChildren;   // constructor initializes position and image   public ImagePanel( int identifier, String imageName )   {      super( null ); // specify null layout      setOpaque( false ); // make transparent      // set unique identifier      ID = identifier;      // set location      position = new Point2D.Double( 0, 0 );      setLocation( 0, 0 );      // create ImageIcon with given imageName      imageIcon = new ImageIcon(         getClass().getResource( imageName ) );      Image image = imageIcon.getImage();      setSize(          image.getWidth( this ), image.getHeight( this ) );      // create Set to store Panel children      panelChildren = new HashSet();   } // end ImagePanel constructor   // paint Panel to screen   public void paintComponent( Graphics g )   {      super.paintComponent( g );      // if image is ready, paint it to screen      imageIcon.paintIcon( this, g, 0, 0 );   }   // add ImagePanel child to ImagePanel   public void add( ImagePanel panel )   {      panelChildren.add( panel );      super.add( panel );   }   // add ImagePanel child to ImagePanel at given index   public void add( ImagePanel panel, int index )   {      panelChildren.add( panel );      super.add( panel, index );   }   // remove ImagePanel child from ImagePanel   public void remove( ImagePanel panel )   {      panelChildren.remove( panel );      super.remove( panel );   }   // sets current ImageIcon to be displayed   public void setIcon( ImageIcon icon )   {      imageIcon = icon;   }   // set on-screen position   public void setPosition( double x, double y )   {      position.setLocation( x, y );      setLocation( ( int ) x,  ( int ) y );   }   // return ImagePanel identifier   public int getID()   {      return ID;   }   // get position of ImagePanel   public Point2D.Double getPosition()   {      return position;   }   // get imageIcon   public ImageIcon getImageIcon()   {      return imageIcon;   }   // get Set of ImagePanel children   public Set getChildren()   {      return panelChildren;   }}

⌨️ 快捷键说明

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