1

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

为了这个目标,我尝试使用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);
    }
  • 我们能以任何方式规避调用这个方法吗?
  • 也许存在其他一些方法来实现这一目标?
4

3 回答 3

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.
        }
    }
于 2013-07-08T20:24:48.077 回答
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 的大小并使用它来设置您的点(这更好),因为这样图像将相对于框架本身的大小进行渲染。

于 2013-07-08T20:25:14.313 回答
1

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

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

建议您覆盖paintComponent

你可以签出

更多细节。

在你的例子中,你Graphicsnull......Graphics g = null;这无济于事......

在此处输入图像描述

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

    }

}
于 2013-07-09T04:59:02.080 回答