java - 如何在JFrame上绘图

标签 java swing

public class MouseTracker extends JPanel{
static List<Double> xL = new ArrayList<Double>();
static List<Double> yL = new ArrayList<Double>();

public static void main(String args[]) throws InterruptedException{
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(1920,1080);
    frame.setVisible(true);

    Timer time = new Timer();
    time.schedule(new TimerTask(){
        public void run(){
            String coords = getCoords();
            System.out.println(coords);
        }
    }, 0, 250);
}


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

    List<Integer> x = new ArrayList<Integer>();
    List<Integer> y = new ArrayList<Integer>();

    x.add(xL.get(xL.size() - 1).intValue());
    x.add(xL.get(xL.size() - 2).intValue());

    y.add(yL.get(yL.size() - 1).intValue());
    y.add(yL.get(yL.size() - 2).intValue());

    Graphics2D g2d = (Graphics2D) g;

    g2d.setColor(Color.red);
    g2d.drawLine(x.get(0), y.get(0), x.get(1), y.get(1));
}


public static String getCoords(){
    double xCoord = MouseInfo.getPointerInfo().getLocation().getX();
    double yCoord = MouseInfo.getPointerInfo().getLocation().getY();

    xL.add(xCoord);
    yL.add(yCoord);

    String coords = Double.toString(xCoord) + " : " + Double.toString(yCoord);
    return coords;
}
}

我正在尝试编写一个程序来创建鼠标移动的可视化。

上面的代码很好地获取了鼠标坐标,问题是我不确定如何调用paintComponent以便在框架上绘制。

如有任何帮助,我们将不胜感激。

最佳答案

首先,您必须将 MouseTracker 的新实例添加到内容 Pane :

JFrame frame = new JFrame();

frame.getContentPane().add(new MouseTracker());

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1920, 1080);
frame.setVisible(true);

然后在定时器中调用repaint:

Timer time = new Timer();
time.schedule(new TimerTask() {
    public void run() {
        String coords = getCoords();
        System.out.println(coords);
        frame.getContentPane().repaint();
    }
}, 0, 250);

所以整个代码将如下所示(其中行是持久的):

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.MouseInfo;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class MouseTracker extends JPanel {
    static ArrayList<Double> xL = new ArrayList<Double>();
    static ArrayList<Double> yL = new ArrayList<Double>();

    public static void main(String args[]) throws InterruptedException {
        JFrame frame = new JFrame();

        frame.getContentPane().add(new MouseTracker());

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1920, 1080);
        frame.setVisible(true);

        Timer time = new Timer();
        time.schedule(new TimerTask() {
            public void run() {
                String coords = getCoords();
                System.out.println(coords);
                frame.getContentPane().repaint();
            }
        }, 0, 250);
    }

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

        ArrayList<Integer> x = new ArrayList<Integer>();
        ArrayList<Integer> y = new ArrayList<Integer>();

        for (int i = 0; i < xL.size(); i++) {

            x.add(xL.get(i).intValue());
            y.add(yL.get(i).intValue());

        }

        Graphics2D g2d = (Graphics2D) g;

        g2d.setColor(Color.red);

        for (int i = 0; i < xL.size() - 1; i++) {

            g2d.drawLine(x.get(i), y.get(i), x.get(i + 1), y.get(i + 1));

        }

    }

    public static String getCoords() {
        double xCoord = MouseInfo.getPointerInfo().getLocation().getX();
        double yCoord = MouseInfo.getPointerInfo().getLocation().getY();

        xL.add(xCoord);
        yL.add(yCoord);

        String coords = Double.toString(xCoord) + " : "
                + Double.toString(yCoord);
        return coords;
    }
}

关于java - 如何在JFrame上绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32378740/

相关文章:

java - 使用代理时停止 Selenium WebDriver 的无限获取

java - Struts2 - 如何在不使用 struts.xml 的情况下重定向到操作?

java - android中不同屏幕尺寸的可绘制尺寸

java - BigDecimal 到 Java/Android 上 BigDecimal 的强大功能

Java 嵌套忽略内部属性

java - 如何在JTable中实现水平滚动

java - 自动 View 定位 JEditorPane

java - 如何通过选择标题来选择 JDialog 中 JTable 的列

java - 简单的 2D Java 游戏

java - TreeUI 的自定义 ui 委托(delegate)