更新列表<点>在 componentResized 事件上 [英] Updating a List&lt;Point&gt; on componentResized event

查看:26
本文介绍了更新列表<点>在 componentResized 事件上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a JInternalFrame that I draw on with Graphics2D. Each Graphics2D object is drawn in a location based on a java.util.List. I am interested in updating the location of the points on a componentResized event and then repaint the Graphics2D objects, in effect scaling with the window, but I cannot figure out how to do it. I tried overloading the componentResized to get the new window size and add to the points in the list but this isn't working.

在 Camickr 的一些帮助(好吧,很多帮助)之后,我想出了如何解决这个几天来最烦人的问题.固定代码如下:

After some help (alright a lot of help) from Camickr I figured out how to solve this issue that has been most annoying for several days. Fixed code is below:

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

推荐答案

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

One last answer, unless you start provide a proper SSCCE with your question.

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

The code below is an example of a SSCCE. Only the code needed to demonstrate the problem (or solution) is posted.

如果你不能简化你的代码,那么这意味着你不理解你的问题或你的问题.

If you can't simplify your code then it means you don't understand your question or your problem.

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 添加到面板,以便您可以动态更改比例.

I'll let you add the ComponentListener to the panel so you can dynamically change the scale.

如果你不能解决问题,那么你有一个合适的 SSCCE,你可以用它来更新你的问题.

If you can't solve the problem, then you have a proper SSCCE that you can update your question with.

一旦您解决了问题并理解了概念,那么您就可以修复您的实际程序.

Once you solve the problem and understand the concept, then you fix your real program.

这篇关于更新列表<点>在 componentResized 事件上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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