为什么我的fillpolygon形状看起来很奇怪(Java GUI)? [英] Why is my fillpolygon shape looking odd (Java GUI)?

查看:147
本文介绍了为什么我的fillpolygon形状看起来很奇怪(Java GUI)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个具有8个点的黑色多边形,并将其放在按钮下方的中央.我相信我所做的一切都正确,但是多边形看起来有些模糊,并且不在按钮下方居中.有人可以解释为什么它无法正常工作吗?

I'm trying to make a black polygon with 8 points and put it in the center underneath the button. I believe I did everything correctly, but the polygon looks partly smushed and not centered under the button. Could someone explain why it's not working properly?

用于覆盖多边形的覆盖面板.

My overriding panel to create the polygon.

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

class OwnJpanel extends JPanel{
    public void paintComponent(Graphics g){

        int[] xpoints = {10, 20, 25, 25, 20, 10, 5, 5};
        int[] ypoints = {10, 25, 20, 27, 37, 37, 27, 20};

        g.setColor(Color.black);
        g.fillPolygon(xpoints, ypoints, 8);

   }
}

我的主班:

import java.awt.BorderLayout;
import java.awt.Graphics;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Main{

    public Main(){

    JFrame jf = new JFrame();
    jf.getContentPane();
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf.setSize(500,500);

    OwnJpanel oj1 = new OwnJpanel();
    JPanel jp1 = new JPanel();
    JButton jb1 = new JButton("Click Once");

    //jf.getContentPane().add(BorderLayout.CENTER, oj1); (Attempted this to center the polygon but it continues to be stuck in the left corner

    oj1.add(jb1);
    jf.add(oj1);

    jf.setVisible(true);

    }

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

最后,是否有一个JButton的按钮交叉了?因此是一个划掉的按钮(类似于X,位于当前JButton的单击一次"上).

Lastly, is there a JButton with a cross over the button? So a crossed out button (Like X, over the current JButton "Click Once").

推荐答案

好的,所以与其试图弄清楚多边形与按钮的关系,还需要花费大量的工作,并且涉及到必须能够预先计算按钮和多边形所需的空间要求,作弊"会更简单,并且可以使用适当的布局管理器为您完成工作...

Okay, so instead of trying to figure out the relationship of the polygon to the button, which would take a quite a bit of work and would involve you having to be able to pre-calculate the required amount of space that the button AND the polygon would require, it would be simpler to "cheat" and make use of an appropriate layout manager to do the work for you...

您也不应依赖于魔术"数字,而应依赖于运行时实际知道的内容,例如组件的实际宽度和高度

You should also not rely on "magic" numbers and should rely on what is actually known at runtime, such as the actual width and height of the component

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

public class PolyTest {

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

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

                JButton btn = new JButton("A Button");
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridwidth = GridBagConstraints.REMAINDER;

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                frame.add(btn, gbc);
                frame.add(new PolyPane(), gbc);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class PolyPane extends JPanel {

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

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.translate(5, 5);
            int width = getWidth() - 10;
            int height = getHeight() - 10;
            int x[] = new int[]{
                0, 
                width / 10, 
                width / 2, 
                width - (width / 10), 
                width, 
                width - (width / 4), 
                width / 4};
            int y[] = new int[]{
                height - (height / 3), 
                height / 5, 
                0, 
                height / 5, 
                height - (height / 3), 
                height, 
                height};
            g2d.setColor(Color.BLACK);
            g2d.drawPolygon(x, y, x.length);
            g2d.dispose();
        }

    }

}

这篇关于为什么我的fillpolygon形状看起来很奇怪(Java GUI)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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