Как сделать динамически размер Grapich2D Java? - PullRequest
0 голосов
/ 13 ноября 2018

Я попробовал нарисовать алмаз grapich2D, как сделать SceleUp, когда я нажимаю кнопку Как нарисовать ромб в Java? переменная sheigth была успешно изменена, но не было никакого изменения в размере на алмазе?

вопрос в том, как изменить размер бриллианта при нажатии на кнопку? что-то не так с кодом ниже.

public class JavaApplication251 {

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

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

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private Diamond diamond;

        public TestPane() {
            Button scaleUP = new Button("Scale Up");
            scaleUP.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("ButonClick");
                    System.out.println(diamond.getSheight());
                    diamond.setSheight(diamond.getSheight() +30);
                    diamond.SceleUpdate();
                }
            });
            add(scaleUP);
            diamond = new Diamond(100, 100);
        }

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

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            int x = (getWidth() - diamond.getBounds().width) / 2;
            int y = (getHeight()- diamond.getBounds().height) / 2;
            AffineTransform at = AffineTransform.getTranslateInstance(x, y);
            Shape shape = at.createTransformedShape(diamond);
            g2d.setColor(Color.YELLOW);
            g2d.fill(shape);
            g2d.setColor(Color.RED);
            g2d.draw(shape);
            g2d.dispose();
        }
    }

    public class Diamond extends Path2D.Double {
           private  double sWidth;
           private double sheight;
        public Diamond(double width, double height) {
            sWidth = width;
            sheight = height;

            SceleUpdate();
        }

        void SceleUpdate(){
            moveTo(0, sheight / 2);
            lineTo(sWidth / 2, 0);
            lineTo(sWidth, sheight / 2);
            lineTo(sWidth / 2, sheight);
            closePath();
        }

        public double getsWidth() {
            return sWidth;
        }

        public void setsWidth(double sWidth) {
            this.sWidth = sWidth;
        }

        public double getSheight() {
            return sheight;
        }

        public void setSheight(double sheight) {
            this.sheight = sheight;
        }
    }
}
...