更新componentResized事件上的List <point>

时间:2015-08-29 22:41:39

标签: java swing

我有一个使用Graphics2D绘制的JInternalFrame。每个Graphics2D对象都在基于java.util.List的位置绘制。我有兴趣更新componentResized事件上的点的位置,然后重新绘制Graphics2D对象,实际上与窗口缩放,但我无法弄清楚如何做到这一点。我尝试重载componentResized以获取新窗口大小并添加到列表中的点但这不起作用。

编辑: 在Camickr的一些帮助(好的帮助)之后,我想出了如何解决这个问题已经困扰了好几天。固定代码如下:

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;

public class ShapeTransform extends JPanel implements ComponentListener
{
    private ArrayList<Shape> shapes = new ArrayList<Shape>();
    private double scale = 1.0;

    public ShapeTransform()
    {
        shapes.add(new Ellipse2D.Double(10, 10, 20, 20));
        shapes.add( new Ellipse2D.Double(30, 30, 20, 20) );
        shapes.add( new Ellipse2D.Double(50, 50, 20, 20) );
        shapes.add( new Ellipse2D.Double(70, 70, 20, 20) );

        addComponentListener(this);

            addMouseListener( new MouseAdapter()
        {
            @Override
            public void mousePressed(MouseEvent e)
            {
                Point p = new Point((int)(e.getX() / scale), (int)(e.getY() / scale));

                for (Shape shape : shapes)
                {
                    if (shape.contains(p))
                    {
                        System.out.println("shape pressed");
                        return;
                    }
                }
                System.out.println("no shape pressed");
            }
        });
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D)g.create();
        g2d.setColor( Color.BLUE );

        AffineTransform tx = new AffineTransform(); //
        tx.concatenate( g2d.getTransform() );
        tx.scale(scale, scale);
        g2d.setTransform(tx);

        shapes.forEach(g2d::fill);

        g2d.dispose();
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("ShapeTransform");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new ShapeTransform());
        frame.setLocationByPlatform(true);
        frame.setSize(400, 400);
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(() -> createAndShowGUI());
    }

    @Override
    public void componentResized(ComponentEvent e) {

        scale = ((1.0 - ((300 - getHeight()) * .0035)) + (1.0 - ((300 - getWidth()) * .0035)) / 2);

        System.out.println(scale);
        System.out.println(getHeight());
    }

    @Override
    public void componentMoved(ComponentEvent e) {

    }

    @Override
    public void componentShown(ComponentEvent e) {

    }

    @Override
    public void componentHidden(ComponentEvent e) {

    }

    public void setScale(double scale)
    {
        this.scale = scale;
    }
}

1 个答案:

答案 0 :(得分:2)

最后一个答案,除非您开始提供适当的SSCCE问题。

以下代码是SSCCE的示例。只发布了演示问题(或解决方案)所需的代码。

如果您无法简化代码,则意味着您无法理解您的问题或问题。

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;

public class ShapeTransform extends JPanel
{
    private ArrayList<Shape> shapes = new ArrayList<Shape>();
    private double scale = 1.0;

    public ShapeTransform()
    {
        shapes.add( new Ellipse2D.Double(10, 10, 20, 20) );
        shapes.add( new Ellipse2D.Double(30, 30, 20, 20) );
        shapes.add( new Ellipse2D.Double(50, 50, 20, 20) );
        shapes.add( new Ellipse2D.Double(70, 70, 20, 20) );

        addMouseListener( new MouseAdapter()
        {
            @Override
            public void mousePressed(MouseEvent e)
            {
                Point p = new Point((int)(e.getX() / scale), (int)(e.getY() / scale));

                for (Shape shape: shapes)
                {
                    if (shape.contains(p))
                    {
                        System.out.println("shape pressed");
                        return;
                    }
                }

                System.out.println("no shape pressed");
            }
        });
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D)g.create();
        g2d.setColor( Color.BLUE );

        AffineTransform tx = new AffineTransform(); //
        tx.concatenate( g2d.getTransform() );
        tx.scale(scale, scale);
        g2d.setTransform(tx);

        for (Shape shape : shapes)
        {
            g2d.fill( shape );
        }

        g2d.dispose();
    }

    public void setScale(double scale)
    {
        this.scale = scale;
    }


    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("ShapeTransform");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new ShapeTransform());
        frame.setLocationByPlatform( true );
        frame.setSize(400, 400);
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
}

我将允许您将ComponentListener添加到面板,以便您可以动态更改比例。

如果你无法解决问题,那么你有一个适当的SSCCE,你可以用它来更新你的问题。

一旦解决了问题并理解了概念,就可以修复真正的程序。