为什么我的fillpolygon形状看起来很奇怪(Java GUI)?

时间:2014-10-06 04:59:15

标签: java swing jpanel

我试图制作一个有8个点的黑色多边形并将其放在按钮下方的中心位置。我相信我做的一切都是正确的,但是多边形看起来部分是按摩而不是按钮下方的中心。有人可以解释为什么它不能正常工作吗?

我的覆盖面板用于创建多边形。

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" Click Once")。

2 个答案:

答案 0 :(得分:2)

首先在您的方法中调用super.paintComponent(g);

不要使用绝对坐标。定义

int centerX=getWidht()/2;
int centerY=getHeight()/2;

然后使用中心坐标调整您的绘图。例如centerX + something,centerY + something as coordinates。

或者你可以使用g.drawString()传递“X”字符串并将字符串居中。

答案 1 :(得分:2)

好的,所以不要试图找出多边形与按钮的关系,这将需要相当多的工作,并且需要你必须能够预先计算按钮所需的空间量并且多边形需要,它会更简单地欺骗"并利用适当的布局管理器为您完成工作......

你也不应该依赖"魔法"数字并且应该依赖于在运行时实际已知的内容,例如组件的实际宽度和高度

Poly

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

    }

}