【问题标题】:Java JPanel drawing shapesJava JPanel 绘制形状
【发布时间】:2012-12-09 20:59:16
【问题描述】:

我是第一次使用 JPanel 并在 JPanel 上绘制基本形状。

我已经为这样的形状编写了代码:

public class Shape extends JPanel{

int x,y;

public Shape(int x, int y){
    this.x = x;
    this.y = y;
}

public void paint(Graphics g){
    super.paint(g);
    g.setColor(Color.black);
    g.drawRect(x, y, 20, 20);
    }
}

我有另一堂课,我将使用这个形状。它扩展了 JFrame 并实现了 MouseListener。在这个 JFrame 上,我放置了 JPanel,它被简单地称为“面板”。

我有读取鼠标位置并在“面板”上绘制形状的方法。

public void mouseClicked(MouseEvent e){
    Shape shape = new Shape(e.getX(),e.getY());
    panel.add(shape);
    panel.revalidate();
    panel.repaint();
}

问题是它没有在我的鼠标所在的坐标上绘制形状。它只是在上面的面板上绘制并绘制成一条线。

感谢您的回答。

【问题讨论】:

    标签: java swing drawing jpanel shapes


    【解决方案1】:
    public class ShapesPanel extends JPanel {
    
        private java.util.List shapesList ;
    
        /**
         * Constructs <code>ShapesPanel</code>
         */
        public ShapesPanel() {
            shapesList = new java.util.ArrayList() ;
            this.addMouseListener(new MouseClickListener())  ;
        }
    
    
        /**
         * Creates a shape
         * @param bounds
         * @return
         */
        private Shape createShape( Rectangle bounds ) {
            Shape shape = new Ellipse2D.Double(bounds.x, bounds.y, bounds.width, bounds.height );
    
    /*
            To use the following shapes, you need to have java shapes library, which can
            be downloaded from <a href="http://wwww.jshapes.com">Java Shapes Library</a>
    */
    /*
            // To create star shape
            Shape shape = new Star( bounds, 50, Star.STAR_8_POINTS );
            // To create triangle shape
            Shape shape = new Triangle( bounds, Triangle.UP );
            // To create diamond shape
            Shape shape = new Diamond( bounds );
            // To create Parallelogram shape
            Shape shape = new Parallelogram( bounds  );
    */
    
            return shape ;
        }
    
        /**
         *  MouseClickListener
         */
        private class MouseClickListener extends MouseAdapter {
            @Override
            public void mouseClicked(MouseEvent e) {
                Point pt = e.getPoint();
                Dimension size = new Dimension(100, 100 );
                Rectangle bounds = new Rectangle(pt.x, pt.y, size.width, size.height );
                Shape shape = createShape(bounds);
                shapesList.add( shape );
                repaint();
            }
        }
    
        /**
         * Paints the component
         * @param g
         */
        protected void paintComponent(Graphics g) {
    
            Graphics2D g2 = (Graphics2D) g;
            Rectangle bounds = new Rectangle(0,0,getWidth(), getHeight() );
            g2.setPaint( Color.white );
            g2.fill( bounds );
    
            for (Iterator iterator = shapesList.iterator(); iterator.hasNext(); ) {
                Shape shape = (Shape) iterator.next();
                g2.setPaint( Color.cyan );
                g2.fill( shape );
                g2.setPaint( Color.black );
                g2.draw( shape );
            }
        }
    
        /**
         * Driver
         * @param args
         */
        public static void main(String[] args) {
            JFrame frame = new JFrame("Draw Shapes") ;
            frame.getContentPane().add( new ShapesPanel() );
            frame.setSize(600, 600);
            frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
            frame.setVisible( true );
    
        }
    }
    

    希望这有助于在面板中绘制 java 形状。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多