2013-07-08 157 views
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); 
    } 
  • 我们能否绕过停靠任何方式这种方法吗?
  • 也许存在其他一些方法来达到这个目的?
+0

**但Graphics类是抽象类,我不能创建实例对象,并调用drawPolygon()方法?** 1.把所有'Objects'到'array' ,2.使用'Swing JComponent'或'JPanel'(重写'getPreferredSize'),''custom painting'在'paintComponent'中完成,4.'paintComponent'在''prepared''数组中循环,5 use用于在'JComponent'或'JPanel'中定位'Objects'的局部变量 – mKorbel

+0

您需要将它渲染到哪里?屏幕或文件? – MadProgrammer

+0

@MadProgrammer屏幕 –

回答

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

现在只是创造了这样一个实例,并把它添加到最小高度的一个JFrame宽度为100。您可以使用JFrame的getWidth()方法,该方法将返回JFrame的大小,并用它来设置您的点(这更好),因为这样图像将相对于框架本身的大小进行渲染。

+0

@ChrisMcJava它给出了语法错误'对于g.draw(多边形)类型Graphics,未定义方法draw(GeneralPath) )'。这里'Graphics'是'Paint'的参数 –

+0

@nazar_art试试这个。它应该工作。 – ChrisMcJava

+0

您应该使用自己的组件的宽度和高度值,因为它可能会添加到另一个子组件,并且可能在布局管理器的控制之下 – MadProgrammer

2

开发人员制作图形抽象的原因是图形对象需要来自某个地方。例如,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-您不能依赖getGraphics返回有效值。 2- getGraphics的结果将在repaint循环中被覆盖3-您实际上没有调用drawAPolygon – MadProgrammer

1

绘画由RepaintManager控制。在Swing中绘画是通过一系列方法完成的,当您决定组件需要更新时(您当然可以请求重绘,但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(); 
     } 

    } 

} 
+0

尽管从技术上讲,“绘画由RepaintManager“句子,我不会告诉初学者关于RepaintManager,他们只是不需要知道它,我认为它会让他们感到困惑。初学者应该如何处理这些信息?开始学习RepaintManager API?当他们只需要一个图形对象时,它就没有什么帮助...... – lbalazscs

+0

@lbalazscs提及重绘管理器的一点是告诉OP他们没有对绘制过程的控制,还有一些其他系统到位,是的,我希望他们确实调查重绘经理以及绘画在Swing中的工作方式,这将使他们更好地了解他们如何从中受益。 OP所面临的问题之一是缺乏对油漆工艺的了解 – MadProgrammer