pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

Java:鼠標在圖形界面中拖動和移動

Java: mouseDragged and moving around in a graphical interface(Java:鼠標在圖形界面中拖動和移動)
本文介紹了Java:鼠標在圖形界面中拖動和移動的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

這里是新手程序員.

我正在制作一個程序,在笛卡爾坐標系中呈現用戶輸入的方程.目前,我在讓用戶在坐標中自由移動視圖方面遇到了一些問題.目前使用 mouseDragged 用戶可以稍微拖動視圖,但是一旦用戶釋放鼠標并嘗試再次移動視圖,原點就會重新回到鼠標光標的當前位置.讓用戶自由移動的最佳方式是什么?提前致謝!

I'm making a program that renders user-inputted equations in a Cartesian coordinate system. At the moment I'm having some issues with letting the user move the view around freely in the coordinate. Currently with mouseDragged the user can drag the view around a bit, but once the user releases the mouse and tries to move the view again the origin snaps back to the current position of the mouse cursor. What is the best way to let the user move around freely? Thanks in advance!

這是繪圖區的代碼.

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import javax.swing.JPanel;
public class DrawingArea extends JPanel implements MouseMotionListener {

private final int x_panel = 350; // width of the panel
private final int y_panel = 400; // height of the panel
private int div_x; // width of one square
private int div_y; // height of one square

private int real_y;
private int real_x;
private Point origin; // the origin of the coordinate
private Point temp; // temporary point

private static int y = 0;
private static int x = 0;

DrawingArea() {
    setBackground(Color.WHITE);

    real_x = x_panel;
    real_y = y_panel;

    setDivisionDefault();
    setOrigin(new Point((real_x / 2), (real_y / 2)));

    setSize(x_panel, y_panel);
    addMouseMotionListener(this);

}

DrawingArea(Point origin, Point destination) {
     this.origin = origin;
     this.destination = destination;
     panel = new JPanel();
     panel.setSize(destination.x, destination.y);
     panel.setLocation(origin);
     this.panel.setBackground(Color.red);
     panel.setLayout(null);


}

@Override
public void paintComponent(Graphics g) {

    super.paintComponent(g);

    Graphics2D line = (Graphics2D) g;


    temp = new Point(origin.x, origin.y);

    line.setColor(Color.red);
    drawHelpLines(line);

    line.setColor(Color.blue);
    drawOrigin(line);

    line.setColor(Color.green);
    for (int i = 0; i < 100; i++) { // This is a test line
        //temp = this.suora();

        temp.x++;
        temp.y++;

        line.drawLine(temp.x, temp.y, temp.x, temp.y);

    }



}

public void setOrigin(Point p) {
    origin = p;


}



public void drawOrigin(Graphics2D line) {
    line.drawLine(origin.x, 0, origin.x, y_panel);
    line.drawLine(0, origin.y, x_panel, origin.y);
}

public void drawHelpLines(Graphics2D line) {

    int xhelp= origin.x;
    int yhelp= origin.y;
    for (int i = 0; i < 20; i++) {
        xhelp+= div_x;
        line.drawLine(xhelp, 0, xhelp, y_panel);
    }
    xhelp= origin.x;
    for (int i = 0; i < 20; i++) {
        xhelp-= div_x;

        line.drawLine(xhelp, 0, xhelp, y_panel);
    }

    for (int i = 0; i < 20; i++) {
        yhelp-= div_y;
        line.drawLine(0, yhelp,x_panel, yhelp);
    }
    yhelp= origin.y;
    for (int i = 0; i < 20; i++) {
        yhelp+= div_y;
        line.drawLine(0, yhelp, x_panel, yhelp);
    }

}

public void setDivisionDefault() {
    div_x = 20;
    div_y = 20;

}

@Override
public void mouseDragged(MouseEvent e) {


    //Point temp_point = new Point(mouse_x,mouse_y);
    Point coords = new Point(e.getX(), e.getY());



    setOrigin(coords);

    repaint();

}

@Override
public void mouseMoved(MouseEvent e) {
}
}

推薦答案

基于這個example,下面的程序允許用戶將坐標軸的交點拖動到任意點 origin,該點從面板的中心開始.

Based on this example, the following program allows the user to drag the axes' intersection to an arbitrary point, origin, which starts at the center of the panel.

import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * @see https://stackoverflow.com/a/15576413/230513
 * @see https://stackoverflow.com/a/5312702/230513
 */
public class MouseDragTest extends JPanel {

    private static final String TITLE = "Drag me!";
    private static final int W = 640;
    private static final int H = 480;
    private Point origin = new Point(W / 2, H / 2);
    private Point mousePt;

    public MouseDragTest() {
        this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                mousePt = e.getPoint();
                repaint();
            }
        });
        this.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                int dx = e.getX() - mousePt.x;
                int dy = e.getY() - mousePt.y;
                origin.setLocation(origin.x + dx, origin.y + dy);
                mousePt = e.getPoint();
                repaint();
            }
        });
    }

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

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawLine(0, origin.y, getWidth(), origin.y);
        g.drawLine(origin.x, 0, origin.x, getHeight());
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame f = new JFrame(TITLE);
                f.add(new MouseDragTest());
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.pack();
                f.setLocationRelativeTo(null);
                f.setVisible(true);
            }
        });
    }
}

這篇關于Java:鼠標在圖形界面中拖動和移動的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉換為公歷?)
Java: What/where are the maximum and minimum values of a GregorianCalendar?(Java:GregorianCalendar 的最大值和最小值是什么/在哪里?)
Calendar to Date conversion for dates before 15 Oct 1582. Gregorian to Julian calendar switch(1582 年 10 月 15 日之前日期的日歷到日期轉換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當前星期幾的值)
主站蜘蛛池模板: 全自动烧卖机厂家_饺子机_烧麦机价格_小笼汤包机_宁波江北阜欣食品机械有限公司 | 上海单片机培训|重庆曙海培训分支机构—CortexM3+uC/OS培训班,北京linux培训,Windows驱动开发培训|上海IC版图设计,西安linux培训,北京汽车电子EMC培训,ARM培训,MTK培训,Android培训 | 一体化污水处理设备_生活污水处理设备_全自动加药装置厂家-明基环保 | 硅胶管挤出机厂家_硅胶挤出机生产线_硅胶条挤出机_臣泽智能装备 贵州科比特-防雷公司厂家提供贵州防雷工程,防雷检测,防雷接地,防雷设备价格,防雷产品报价服务-贵州防雷检测公司 | SF6环境监测系统-接地环流在线监测装置-瑟恩实业 | 防水套管厂家_刚性防水套管_柔性防水套管_不锈钢防水套管-郑州中泰管道 | 酒水灌装机-白酒灌装机-酒精果酒酱油醋灌装设备_青州惠联灌装机械 | 上海深蓝_缠绕机_缠膜机-上海深蓝机械装备有限公司 | 细胞染色-流式双标-试剂盒免费代做-上海研谨生物科技有限公司 | 「阿尔法设计官网」工业设计_产品设计_产品外观设计 深圳工业设计公司 | 【连江县榕彩涂料有限公司】官方网站 | 浙江宝泉阀门有限公司| 苏州西装定制-西服定制厂家-职业装定制厂家-尺品服饰西装定做公司 | 拉伸膜,PE缠绕膜,打包带,封箱胶带,包装膜厂家-东莞宏展包装 | PTFE接头|聚四氟乙烯螺丝|阀门|薄膜|消解罐|聚四氟乙烯球-嘉兴市方圆氟塑制品有限公司 | 电镀电源整流器_高频电解电源_单脉双脉冲电源 - 东阳市旭东电子科技 | 立式_复合式_壁挂式智能化电伴热洗眼器-上海达傲洗眼器生产厂家 理化生实验室设备,吊装实验室设备,顶装实验室设备,实验室成套设备厂家,校园功能室设备,智慧书法教室方案 - 东莞市惠森教学设备有限公司 | 温控器生产厂家-提供温度开关/热保护器定制与批发-惠州市华恺威电子科技有限公司 | 耐压仪-高压耐压仪|徐吉电气 | 模温机-油温机-电加热导热油炉-工业冷水机「欧诺智能」 | 一体化净水器_一体化净水设备_一体化水处理设备-江苏旭浩鑫环保科技有限公司 | 黑龙江「京科脑康」医院-哈尔滨失眠医院_哈尔滨治疗抑郁症医院_哈尔滨精神心理医院 | 泰兴市热钻机械有限公司-热熔钻孔机-数控热熔钻-热熔钻孔攻牙一体机 | 北京公积金代办/租房发票/租房备案-北京金鼎源公积金提取服务中心 | 威海防火彩钢板,威海岩棉复合板,威海彩钢瓦-文登区九龙岩棉复合板厂 | 本安接线盒-本安电路用接线盒-本安分线盒-矿用电话接线盒-JHH生产厂家-宁波龙亿电子科技有限公司 | 安驭邦官网-双向万能直角铣头,加工中心侧铣头,角度头[厂家直销] 闸阀_截止阀_止回阀「生产厂家」-上海卡比阀门有限公司 | 东莞压铸厂_精密压铸_锌合金压铸_铝合金压铸_压铸件加工_东莞祥宇金属制品 | 恒温振荡混匀器-微孔板振荡器厂家-多管涡旋混匀器厂家-合肥艾本森(www.17world.net) | Boden齿轮油泵-ketai齿轮泵-yuken油研-无锡新立液压有限公司 | 时代北利离心机,实验室离心机,医用离心机,低速离心机DT5-2,美国SKC采样泵-上海京工实业有限公司 工业电炉,台车式电炉_厂家-淄博申华工业电炉有限公司 | 温州食堂承包 - 温州市尚膳餐饮管理有限公司 | 直流电能表-充电桩电能表-导轨式电能表-智能电能表-浙江科为电气有限公司 | 展厅设计公司,展厅公司,展厅设计,展厅施工,展厅装修,企业展厅,展馆设计公司-深圳广州展厅设计公司 | 精密光学实验平台-红外粉末压片机模具-天津博君| 书信之家_书信标准模板范文大全| J.S.Bach 圣巴赫_高端背景音乐系统_官网 | 房车价格_依维柯/大通/东风御风/福特全顺/江铃图片_云梯搬家车厂家-程力专用汽车股份有限公司 | 电机保护器-电动机综合保护器-浙江开民 | 济南展厅设计施工_数字化展厅策划设计施工公司_山东锐尚文化传播有限公司 | 数字展示在线_数字展示行业门户网站 |