简体   繁体   English

Java JPanel绘图形状

[英]Java JPanel drawing shapes

I am for the first time working with JPanel and drawing basic shapes on the JPanel. 我是第一次使用JPanel并在JPanel上绘制基本形状。

I have written code for the shape like this: 我已经为这种形状编写了代码:

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);
    }
}

I have another class where I will be using this shape. 我将在另一堂课上使用这种形状。 It extends JFrame and implements MouseListener. 它扩展了JFrame并实现了MouseListener。 On this JFrame I have put the JPanel it is called simply "panel". 在此JFrame上,我已将JPanel简称为“面板”。

I have the method, which reads the mouse position, and draws the shape on the "panel". 我有一种方法,可以读取鼠标位置,并在“面板”上绘制形状。

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

The problem is that it doesn't draw the shape on the coordinate where my mouse is. 问题在于它不会在鼠标所在的坐标上绘制形状。 It just draws on panel at the upper-side and draws, them in a line. 它只是在上侧的面板上绘制,然后将它们画成一条线。

Thank you for you answers. 谢谢您的回答。

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 );

    }
}

Hope this helps to draw java shapes in a panel. 希望这有助于在面板中绘制Java形状。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM