如何创建用于绘制多边形的Graphics对象?

时间:2013-07-08 20:04:21

标签: java graphics drawing polygon java-2d

我需要绘制一个多边形 - 通过连接连续点然后将最后一个点连接到第一个点。

有了这个目标,我尝试使用drawPolygon(xPoints, yPoints, nPoints)。在我看来,它是实现这一目标的更方便的方法

Graphics类是抽象类,我不能创建实例对象并调用drawPolygon()方法?

代码:

public void draw() {
        Graphics g = null;
        int xPoints [] = new int[pointsList.size()];
        int yPoints [] = new int[pointsList.size()];
        int nPoints = pointsList.size();

        for (int i = 0; i < pointsList.size(); i++) {
            xPoints [i] = (int) pointsList.get(i).getX();
            yPoints [i] = (int) pointsList.get(i).getY();
        } 

        g.drawPolygon(xPoints, yPoints, nPoints);
    }
  • 我们能否以任何方式规避调用此方法?
  • 也许存在其他一些方法来实现这个目标?

3 个答案:

答案 0 :(得分:2)

开发人员制作Graphics抽象的原因是图形对象需要来自某个地方。例如,JPanel或JFrame对象具有与它们相关联的图形对象,因为它们将可视区域呈现给屏幕。通常使用getGraphics()方法分配图形对象。这是一个简单的例子:

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;

    public class Polygon extends JFrame {
        public static void main(String args[]){
            Test a = new Test();
            a.drawAPolygon();
        }


        public Polygon(){
            setSize(300,300);
            setVisible(true);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }

        void drawAPolygon(int[] xPoints, int[] yPoints, int numPoints){
            Graphics g = getGraphics();
            g.drawPolygon(xPoints, yPoints, numPoints);
        }
        //@override
            public void paint(Graphics g){
            super.paint(g);
            //could also do painting in here.
        }
    }

答案 1 :(得分:1)

我遇到了同样的问题,这就是我绕过它的方式:

//assuming you are displaying your polygon in a JFrame with a JPanel
public class SomeDrawingFrame extends JPanel{
    SomeDrawingFrame(){
    }

    @Override   //JFrame has this method that must be overwritten in order to 
                  display a rendered drawing.

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Polygon square = new Polygon();

        // these points will draw a square
        square.addPoint((0, 0));    //use this.getWidth() method if you want to 
                                         create based on screen size
        square.addPoint((0, 100));
        square.addPoint((100, 100));
        square.addPoint((100, 0)); 
        int y1Points[] = {0, 0, 100, 100};

        g.draw(polygon);
    }
}

现在只需创建一个这样的实例,并将其添加到最小高度和宽度均为100的JFrame中。您可以使用JFrame的getWidth()方法,该方法将返回JFrame的大小并使用它来设置您的点(这是更好的),因为这样图像将相对于帧本身的大小呈现。

答案 2 :(得分:1)

绘画由RepaintManager控制。 Swing中的绘画是通过一系列方法完成的,当RepaintManager决定您的组件需要更新时,代表您调用(当然,您可以请求重新绘制,但RepaintManager将决定何时,什么和多少)。

为了在Swing中执行自定义绘制,您需要覆盖作为绘制周期的一部分调用的方法之一。

您覆盖paintComponent

recommended

你可以看看

了解更多详情。

在您的示例中,您的Graphicsnull ... Graphics g = null;,这对您没有帮助......

enter image description here

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class SimplePloy {

    public static void main(String[] args) {
        new SimplePloy();
    }

    public SimplePloy() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new PloyPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class PloyPane extends JPanel {

        private int[] xPoints;
        private int[] yPoints;

        public PloyPane() {
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        public void invalidate() {

            xPoints = null;
            yPoints = null;

            super.invalidate(); 
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            if (xPoints == null || yPoints == null) {

                int width = getWidth() - 1;
                int height = getHeight() - 1;

                int halfWidth = width / 2;
                int halfHeight = height / 2;
                int innerWidth = width / 8;
                int innerHeight = height / 8;

                xPoints = new int[9];
                yPoints = new int[9];

                xPoints[0] = halfWidth;
                yPoints[0] = 0;

                xPoints[1] = halfWidth - innerWidth;
                yPoints[1] = halfHeight - innerHeight;  

                xPoints[2] = 0;
                yPoints[2] = halfHeight;

                xPoints[3] = halfWidth - innerWidth;
                yPoints[3] = halfHeight + innerHeight;  

                xPoints[4] = halfWidth;
                yPoints[4] = height;

                xPoints[5] = halfWidth + innerWidth;
                yPoints[5] = halfHeight + innerHeight;  

                xPoints[6] = width;
                yPoints[6] = halfHeight;

                xPoints[7] = halfWidth + innerWidth;
                yPoints[7] = halfHeight - innerHeight;  

                xPoints[8] = halfWidth;
                yPoints[8] = 0;  

            }
            g2d.drawPolygon(xPoints, yPoints, xPoints.length);
            g2d.dispose();
        }

    }

}