How do you make text in a frame change dependent on cursor position?

僤鯓⒐⒋嵵緔 提交于 2019-12-13 17:39:42

问题


I have a program that is basically just supposed to change the text of a label when your cursor enters a polygon that is shown on the JPanel. I have tried a few different things with nothing working. Currently I am trying an if statement to make it choose which button to add but it still doesn't change if i move my cursor into the polygon. when the cursor is outside of the polygon the label should say "point is not in the polygon" and when inside it should say "point is in the polygon". Any help would be greatly appreciated.

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class Chapter3Lab1  extends JFrame{

private RegularPolygonPanel canvas = new RegularPolygonPanel();

public Chapter3Lab1()
{

    JPanel panel = new JPanel();


    add(canvas, BorderLayout.CENTER);
    add(panel, BorderLayout.SOUTH);


    canvas.setFocusable(true);
    canvas.requestFocusInWindow();



}


public static void main (String[] args)
{

     String isInside = "The point is in the polygon";
     String notInside = "The point is not in the polygon";


     //Create a Polygon object
     Polygon polygon = new Polygon();

    polygon.addPoint(40,20);
    polygon.addPoint(70,40);
    polygon.addPoint(60,80);
    polygon.addPoint(45,45);
    polygon.addPoint(20,60);


    JLabel label = new JLabel(isInside, JLabel.CENTER);
    JLabel notlabel = new JLabel(notInside, JLabel.CENTER);

    Chapter3Lab1 frame = new Chapter3Lab1();;
    frame.setTitle("Chapter 3 Lab 1");
    frame.setLayout(new FlowLayout());
    frame.setLocationRelativeTo(null);// Center the frame
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200,200);
    while (true)
    {
        PointerInfo a = MouseInfo.getPointerInfo();
        Point b = a.getLocation();
        int x = (int) b.getX();
        int y = (int) b.getY();
        if(polygon.contains(x, y) == false)
        {
        frame.remove(label);
        frame.add(notlabel);
        }
        else 
        {
        frame.remove(notlabel);
        frame.add(label);
        }

        frame.setVisible(true);
        try
        {
            Thread.sleep(1000);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }



}

class RegularPolygonPanel extends JPanel
{




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

         //Create a Polygon object
         Polygon polygon = new Polygon();

        polygon.addPoint(40,20);
        polygon.addPoint(70,40);
        polygon.addPoint(60,80);
        polygon.addPoint(45,45);
        polygon.addPoint(20,60);

         //Draw the polygon
         g.drawPolygon(polygon);
     }

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

回答1:


Start by taking a look at How to Write a Mouse Listener

You will need to maintain a reference to the Polygon object and make use of it's contains method to determine if the mouse is within the Polygon itself...

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

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

    public Test() {
        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 RegularPolygonPanel polyPanel;
        private JLabel label;

        public TestPane() {
            polyPanel = new RegularPolygonPanel();
            polyPanel.addMouseMotionListener(new MouseAdapter() {
                @Override
                public void mouseMoved(MouseEvent e) {
                    if (polyPanel.isWithinPolygon(e.getPoint())) {
                        label.setText("Is inside");
                    } else {
                        label.setText("Is outside");
                    }
                }
            });
            label = new JLabel("...");
            setLayout(new BorderLayout());
            add(polyPanel);
            add(label, BorderLayout.SOUTH);
        }

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

    }

    class RegularPolygonPanel extends JPanel {

        private Polygon polygon;

        public RegularPolygonPanel() {
            //Create a Polygon object
            polygon = new Polygon();

            polygon.addPoint(40, 20);
            polygon.addPoint(70, 40);
            polygon.addPoint(60, 80);
            polygon.addPoint(45, 45);
            polygon.addPoint(20, 60);
        }

        public boolean isWithinPolygon(Point p) {
            return polygon.contains(p);
        }

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

            //Draw the polygon
            g.drawPolygon(polygon);
        }

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


来源:https://stackoverflow.com/questions/28423506/how-do-you-make-text-in-a-frame-change-dependent-on-cursor-position

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!