Java-如何在缩放操作后使用良好的鼠标位置使渲染居中
在下面的代码中,当我移动鼠标滚轮时,正方形会放大或缩小,如果我的鼠标进入正方形,即使地图在滚动,我也会在屏幕的任何地方检测到正方形的位置。 真是太棒了:) 如果我将布尔值“centerScreen”更改为true,我可以缩放,并且正方形始终位于屏幕的中心。 这真的非常棒 。。。但是鼠标检测不好:( 如何修改代码以放大或缩小渲染仍在屏幕中心,但使用正方形进行完美的鼠标检测 谢谢你的帮助 这是我完整的“干净代码”——使用复制粘贴(在3类中)-- 主类Java-如何在缩放操作后使用良好的鼠标位置使渲染居中,java,position,mouse,render,scale,Java,Position,Mouse,Render,Scale,在下面的代码中,当我移动鼠标滚轮时,正方形会放大或缩小,如果我的鼠标进入正方形,即使地图在滚动,我也会在屏幕的任何地方检测到正方形的位置。 真是太棒了:) 如果我将布尔值“centerScreen”更改为true,我可以缩放,并且正方形始终位于屏幕的中心。 这真的非常棒 。。。但是鼠标检测不好:( 如何修改代码以放大或缩小渲染仍在屏幕中心,但使用正方形进行完美的鼠标检测 谢谢你的帮助 这是我完整的“干净代码”——使用复制粘贴(在3类中)-- 主类 package main; import ja
package main;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ZoomDemo extends JPanel
{
private static final long serialVersionUID = 1L;
private Rectangle2D square;
private Mouse mouse;
private ScrollMap scrollMap;
private static int screenWidth;
private static int screenHeight;
private static int centerScreenWidth;
private static int centerScreenHeight;
private static boolean centerScreen;
private List<Point> listStar = new ArrayList<Point>();
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 800);
frame.setLocationRelativeTo(null);
screenWidth = frame.getWidth();
screenHeight = frame.getHeight();
centerScreenWidth = screenWidth / 2;
centerScreenHeight = screenHeight / 2;
centerScreen = false;
ZoomDemo zoomDemo = new ZoomDemo();
frame.add(zoomDemo);
frame.setVisible(true);
}
public ZoomDemo()
{
mouse = new Mouse();
addMouseListener(mouse);
addMouseMotionListener(mouse);
addMouseWheelListener(mouse);
scrollMap = new ScrollMap(mouse, screenWidth, screenHeight);
int centerX = (int) getCenter(0, screenWidth, 50, true);
int centerY = (int) getCenter(0, screenHeight, 50, true);
square = new Rectangle2D.Double(centerX, centerY, 50, 50);
Random rand = new Random();
for (int i = 0; i < 50; i++)
{
int x = rand.nextInt(screenWidth);
int y = rand.nextInt(screenHeight);
listStar.add(new Point(x, y));
}
}
@Override
public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D) g.create();
/* Optimization */
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_SPEED);
g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_OFF);
g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE);
/* Background */
g2d.setColor(Color.GRAY);
g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
/* Translatation center -- it is working, but mouse detection is not good */
if (centerScreen)
g2d.translate(centerScreenWidth, centerScreenHeight);
/* Listener Wheel Mouse for zooming and translation */
mouse.zoom(g2d);
/* Restore Translatation center */
if (centerScreen)
g2d.translate(-centerScreenWidth, -centerScreenHeight);
render(g2d);
g2d.dispose();
//@formatter:off
try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); }
//@formatter:on
repaint();
}
public void render(Graphics2D g2d)
{
/* Ask for scroll */
isScrolling(g2d);
/* Mouse Detection */
int xMouse = (int) (mouse.pMousePosition.x - scrollMap.getMapX());
int yMouse = (int) (mouse.pMousePosition.y - scrollMap.getMapY());
boolean detection = square.contains(xMouse, yMouse);
/* Graphic render */
g2d.setColor(Color.WHITE);
for (int i = 0; i < listStar.size(); i++)
g2d.fillRect(listStar.get(i).x, listStar.get(i).y, 4, 4);
g2d.setColor(detection ? Color.red : Color.black);
g2d.fill(square);
/* End of Scroll */
endScrolling(g2d);
}
private void isScrolling(Graphics2D g2d)
{
/* If Mouse on edge, so change value for scrolling */
scrollMap.scrolling();
/* Translation scrolling */
g2d.translate(scrollMap.getMapX(), scrollMap.getMapY());
}
private void endScrolling(Graphics2D g2d)
{
g2d.translate(-scrollMap.getMapX(), -scrollMap.getMapY());
}
public float getCenter(float startXZone, float longueurZone, float longueurElem, boolean round)
{
float s = startXZone + (longueurZone - longueurElem) / 2f;
return (round) ? Math.round(s) : s;
}
}
(可选)
滚动类
package main;
public class ScrollMap
{
private float mapX = 0;
private float mapY = 0;
private int speedScroll = 1;
private int edgeDetection = 70;
private int screenWidth;
private int screenHeight;
private Mouse mouse;
public ScrollMap(Mouse mouse, int screenWidth, int screenHeight)
{
this.mouse = mouse;
this.screenWidth = screenWidth;
this.screenHeight = screenHeight;
}
/**
* Scrolling ecran
*/
public void scrolling()
{
double scale = mouse.inverseScaleCoef;
/* Mouse to Right */
if ((mouse.pMousePosition.x > screenWidth * scale - edgeDetection))
{
for (int k = 0; k < speedScroll; k++)
moveMapToRight();
}
/* Mouse to Left */
else if ((mouse.pMousePosition.x < edgeDetection * scale))
{
for (int k = 0; k < speedScroll; k++)
moveMapToLeft();
}
/* Mouse to Down */
if ((mouse.pMousePosition.y < edgeDetection * scale))
{
for (int k = 0; k < speedScroll; k++)
moveMaptoDown();
}
/* Mouse to Up */
else if ((mouse.pMousePosition.y > screenHeight * scale - edgeDetection))
{
for (int k = 0; k < speedScroll; k++)
moveMapToUp();
}
}
public float moveMapToRight()
{
return mapX--;
}
public float moveMapToLeft()
{
return mapX++;
}
public float moveMapToUp()
{
return mapY--;
}
public float moveMaptoDown()
{
return mapY++;
}
public float getMapY()
{
return mapY;
}
public float getMapX()
{
return mapX;
}
}
packagemain;
公共类滚动映射
{
私有浮点mapX=0;
私有浮点mapY=0;
私用int-speedScroll=1;
私有内部边缘检测=70;
私有整数屏幕宽度;
私人室内屏幕高度;
私人老鼠;
公共滚动地图(鼠标、整数屏幕宽度、整数屏幕高度)
{
this.mouse=鼠标;
this.screenWidth=屏幕宽度;
this.screenHeight=屏幕高度;
}
/**
*滚动ecran
*/
公共无效滚动()
{
双刻度=鼠标。逆刻度系数;
/*鼠标右键*/
if((mouse.pMousePosition.x>screenWidth*scale-edgeDetection))
{
用于(int k=0;k屏幕高度*缩放-边缘检测))
{
用于(int k=0;k
一些问题会跳出来,包括覆盖paint而不是paintComponent,在覆盖中不调用super的等效绘制方法,从绘制方法中调用repaint()
(这是您永远不应该做的事情)…你的其他问题听起来像是几何问题,最好用铅笔和图表纸实验来解决。谢谢你的评论。不要担心重绘,它非常实用,而且只用于演示。回到我的问题上来,我想它确实是纯几何问题,但我的代码中的一个小演示会对我有很大帮助。谢谢你告诉我我需要改变什么。
package main;
public class ScrollMap
{
private float mapX = 0;
private float mapY = 0;
private int speedScroll = 1;
private int edgeDetection = 70;
private int screenWidth;
private int screenHeight;
private Mouse mouse;
public ScrollMap(Mouse mouse, int screenWidth, int screenHeight)
{
this.mouse = mouse;
this.screenWidth = screenWidth;
this.screenHeight = screenHeight;
}
/**
* Scrolling ecran
*/
public void scrolling()
{
double scale = mouse.inverseScaleCoef;
/* Mouse to Right */
if ((mouse.pMousePosition.x > screenWidth * scale - edgeDetection))
{
for (int k = 0; k < speedScroll; k++)
moveMapToRight();
}
/* Mouse to Left */
else if ((mouse.pMousePosition.x < edgeDetection * scale))
{
for (int k = 0; k < speedScroll; k++)
moveMapToLeft();
}
/* Mouse to Down */
if ((mouse.pMousePosition.y < edgeDetection * scale))
{
for (int k = 0; k < speedScroll; k++)
moveMaptoDown();
}
/* Mouse to Up */
else if ((mouse.pMousePosition.y > screenHeight * scale - edgeDetection))
{
for (int k = 0; k < speedScroll; k++)
moveMapToUp();
}
}
public float moveMapToRight()
{
return mapX--;
}
public float moveMapToLeft()
{
return mapX++;
}
public float moveMapToUp()
{
return mapY--;
}
public float moveMaptoDown()
{
return mapY++;
}
public float getMapY()
{
return mapY;
}
public float getMapX()
{
return mapX;
}
}