关于在java中绘制多边形 [英] about drawing a Polygon in java

查看:139
本文介绍了关于在java中绘制多边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在那里,我正在努力提高自己的java2D,首先我正在处理绘制多边形。但是,我无法在框架上看到多边形。我阅读了一些教程和示例,但正如我所说,我面临着问题。这是绘制多边形的示例代码;

hi there i'm trying to improve myself about java2D and first of all i'm dealing with drawing polygons. However, i can not see the polygon on frame. I read some tutorials and examples but as i said i face with problems. here is the sample code of drawing a polygon;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;

import javax.swing.JFrame;

public class jRisk extends JFrame {


    private JFrame mainMap;
    private Polygon poly;

    public jRisk(){

        initComponents();

    }

    private void initComponents(){

        mainMap = new JFrame();
        mainMap.setSize(800, 600);
        mainMap.setResizable(false);

        mainMap.setVisible(true);
        mainMap.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        int xPoly[] = {150,250,325,375,450,275,100};
        int yPoly[] = {150,100,125,225,250,375,300};

        poly = new Polygon(xPoly, yPoly, xPoly.length);

    }

    protected void paintComponent(Graphics g){

        super.paintComponents(g);

        g.setColor(Color.BLUE);
        g.drawPolygon(poly);

    }   

    /**
     * @param args
     */
    public static void main(String[] args) {

        new jRisk();

    }

}


推荐答案

JFrame 没有 paintComponent(Graphics g) 方法。添加 @Override 注释,您将收到编译时错误。

JFrame does not have a paintComponent(Graphics g) method. Add the @Override annotation and you will get a compile time error.

1)使用 JPanel 和覆盖 paintComponent (您只需将 JPanel 添加到 JFrame viad JFrame #add(..)

1) Use JPanel and override paintComponent (you would than add JPanel to the JFrame viad JFrame#add(..))

2)覆盖 getPreferredSize()返回正确的 Dimension ,它们适合您在Graphics对象上的绘图,否则它们将不会被视为 JPanel 没有组件的大小是0,0

2) Override getPreferredSize() to return correct Dimensions which fit your drawing on Graphics object or else they wont be seen as JPanel size without components is 0,0

3)不要在 JFrame上调用 setSize ...而是使用正确的 LayoutManager 和/或覆盖 getPrefferedSize()并调用添加所有组件后但在设置可见之前 pack() JFrame

3) dont call setSize on JFrame... rather use a correct LayoutManager and/or override getPrefferedSize() and call pack() on JFrame after adding all components but before setting it visible

4)阅读 Swing中的并发特别是关于事件发送线程

4) Have a read on Concurrency in Swing specifically about Event Dispatch Thread

5)监视类命名方案应以大写字母开头,此后新单词的每个首字母都应大写

5) watch class naming scheme should begin with a capital letter and every first letter of a new word thereafter should be capitalized

6)你还扩展JFrame 并有一个变量 JFrame ?拿走扩展JFrame 并保留 JFrame 变量,因为我们不想要2 JFrame s以及扩展 JFrame 的不良做法,除非添加功能

6) Also you extend JFrame and have a variable JFrame? Take away the extend JFrame and keep the JFrame variable as we dont want 2 JFrames and its not good practice to extend JFrame unless adding functionality

这是你的代码以上修复(原谅图片质量,但不得不调整大小或者是800x600):

Here is your code with above fixes (excuse picture quality but had to resize or it was going to 800x600):

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Polygon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class JRisk {

    private JFrame mainMap;
    private Polygon poly;

    public JRisk() {

        initComponents();

    }

    private void initComponents() {

        mainMap = new JFrame();
        mainMap.setResizable(false);

        mainMap.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        int xPoly[] = {150, 250, 325, 375, 450, 275, 100};
        int yPoly[] = {150, 100, 125, 225, 250, 375, 300};

        poly = new Polygon(xPoly, yPoly, xPoly.length);
        JPanel p = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(Color.BLUE);
                g.drawPolygon(poly);
            }

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(800, 600);
            }
        };
        mainMap.add(p);
        mainMap.pack();
        mainMap.setVisible(true);

    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new JRisk();
            }
        });
    }
}

根据您的评论:


我正在准备一张包含大量多边形的地图,昨天i
在JFrame上使用了JPanel,我试图检查鼠标是否在$ b内带有MouseListener的多边形的$ b。后来我看到mouseListener给了
false响应(就像鼠标不在多边形内部,但它像
一样,就像它在多边形内部一样)。所以我删除了JPanel然后它
工作

i am preparing a map which includes lots of polygons and yesterday i used a JPanel over a JFrame and i tried to check if mouse was inside of the polygon with MouseListener. later i saw that mouseListener gave false responds (like mouse is not inside of the polygon but it acts like it was inside the polygon). so i deleted the JPanel and then it worked

以下是使用 MouseAdapter更新的代码并覆盖 mouseClicked 以检查点击是否在多边形内。

Here is updated code with MouseAdapter and overridden mouseClicked to check if click was within polygon.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class JRisk {

    private JFrame mainMap;
    private Polygon poly;

    public JRisk() {
        initComponents();
    }

    private void initComponents() {

        mainMap = new JFrame();
        mainMap.setResizable(false);

        mainMap.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        int xPoly[] = {150, 250, 325, 375, 450, 275, 100};
        int yPoly[] = {150, 100, 125, 225, 250, 375, 300};

        poly = new Polygon(xPoly, yPoly, xPoly.length);

        JPanel p = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(Color.BLUE);
                g.drawPolygon(poly);
            }

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

        MouseAdapter ma = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent me) {
                super.mouseClicked(me);

                if (poly.contains(me.getPoint())) {
                    System.out.println("Clicked polygon");
                }

            }
        };
        p.addMouseListener(ma);//add listener to panel
        mainMap.add(p);

        mainMap.pack();
        mainMap.setVisible(true);

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new JRisk();
            }
        });
    }
}

这篇关于关于在java中绘制多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆