Java 如何在另一种方法中使用鼠标点
所以基本上我是在玩图形和一些有趣的东西,在做游戏之前我想知道,我想知道如何使用不同的循环来做一些有趣的事情,但我不知道如何使用我在mouseMoved方法中生成的INT,然后在图形方法中使用它。代码可能会显示一个更好的示例,说明我试图解释的内容Java 如何在另一种方法中使用鼠标点,java,swing,graphics,jframe,mousemotionlistener,Java,Swing,Graphics,Jframe,Mousemotionlistener,所以基本上我是在玩图形和一些有趣的东西,在做游戏之前我想知道,我想知道如何使用不同的循环来做一些有趣的事情,但我不知道如何使用我在mouseMoved方法中生成的INT,然后在图形方法中使用它。代码可能会显示一个更好的示例,说明我试图解释的内容 package com.martin; import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.e
package com.martin;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Random;
import javax.swing.*;
public class Grids extends JFrame implements MouseMotionListener {
JPanel p = new JPanel();
public int width = 1200;
public int height = 800;
public Grids() {
addMouseMotionListener(this);
windowLoader();
}
public static void main(String[] args){
new Grids();
}
public void windowLoader() {
setPreferredSize(new Dimension(width, height));
setMaximumSize(new Dimension(width, height));
setMinimumSize(new Dimension(width, height));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setLocationRelativeTo(null);
// setBackground(Color.BLACK);
p.setSize(width, height);
p.setOpaque(false);
// p.setBackground(Color.BLACK);
add(p);
pack();
setVisible(true);
}
public void mouseMoved(MouseEvent e) {
double mouseX = e.getX();
double mouseY = e.getY();
}
public void mouseDragged(MouseEvent e) {
}
public void paint(Graphics g) {
Random rand = new Random();
int cols, rows;
int size = 8;
Color color;
for (rows = 0; rows < width; rows++) {
for (cols = 0; cols < height; cols++) {
color = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
// g.setColor(color);
// g.fillRect(rows * size, cols * size, size, size);
// g.drawRect(rows * size, cols * size, size, size);
// g.drawLine(rows * size, cols * size, size, size);
// g.drawRoundRect(rows * size, cols * size, size, size, 10, 10);
// g.fillRoundRect(rows * size, cols * size, size, size, 10, 10);
}
}
// int x = 0;
// int y = 0;
// int spacing = rand.nextInt(20) + 1;
//
// while (spacing > -1) {
// spacing = spacing + rand.nextInt(20);
// }
//
// while (x < width) {
// g.drawLine(x, 0, x, height);
// x = x + spacing;
// }
// while (y < height) {
// g.drawLine(0, y, width, y);
// y = y + spacing;
// }
// Point mouseL = MouseInfo.getPointerInfo().getLocation();
// double mouseX = mouseL.getX();
// double mouseY = mouseL.getY();
int x = 0, y = 0;
//Can't access the int from the mouseMoved I get red underline for error (variable cannot be found)
while (x < width) {
if (mouseX < 1) {
x = x + 10;
} else {
x = x + (int)mouseX;
}
while (y < height) {
if (mouseY < 1) {
y = y + 10;
} else {
y = y + (int)mouseY;
}
g.fillRoundRect(x, y, 10, 10, 10, 10);
}
}
repaint();
}
}
package com.martin;
导入java.awt.*;
导入java.awt.event.MouseAdapter;
导入java.awt.event.MouseEvent;
导入java.awt.event.MouseListener;
导入java.awt.event.MouseMotionListener;
导入java.util.Random;
导入javax.swing.*;
公共类Grids扩展JFrame实现MouseMotionListener{
JPanel p=新的JPanel();
公共整数宽度=1200;
公共内部高度=800;
公共电网(){
addMouseMotionListener(此);
windowLoader();
}
公共静态void main(字符串[]args){
新网格();
}
public void windowLoader(){
setPreferredSize(新尺寸(宽度、高度));
设置最大尺寸(新尺寸(宽度、高度));
设置最小尺寸(新尺寸(宽度、高度));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
可设置大小(假);
setLocationRelativeTo(空);
//挫折背景(颜色:黑色);
p、 设置尺寸(宽度、高度);
p、 设置不透明(假);
//立根(颜色:黑色);
加(p);
包装();
setVisible(真);
}
public void mouseMoved(MouseEvent e){
双鼠标=e.getX();
双鼠标=e.getY();
}
公共无效鼠标标记(鼠标事件e){
}
公共空间涂料(图g){
Random rand=新的Random();
int列,行;
int size=8;
颜色;
用于(行=0;行<宽度;行++){
for(cols=0;cols-1){
//间距=间距+随机数下一个(20);
// }
//
//while(x
这是全部代码,我尝试使用MouseInfo获取指针位置,但它获取JFrame组件的位置,我希望获取JFrame本身上的鼠标位置,而不是组件上的鼠标位置。我相信以下内容演示了您想要的功能使用
drawTrail
打开或关闭轨迹图形:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Grids extends JPanel implements MouseMotionListener {
public final static int WIDTH = 1200, HEIGHT = 800, POINT_SIZE = 10;
private double mouseX, mouseY;
private List<Point> points ; //stores all trail points
private boolean drawTrail = true; //change to false to draw only one point at the mouse location
public Grids() {
addMouseMotionListener(this);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setBackground(Color.WHITE);
if(drawTrail) {
points = new ArrayList<>(); //add mouse point to collection
}
}
@Override
public void mouseMoved(MouseEvent e) {
//set a min distance between points
if( Math.abs(mouseX - e.getX()) >= POINT_SIZE || Math.abs(mouseY - e.getY()) >= POINT_SIZE ) {
if(drawTrail) {
points.add(new Point(e.getX(),e.getY()));
}
mouseX = e.getX();
mouseY = e.getY();
repaint();
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Random rand = new Random();
if(drawTrail){
//draw all collected points
for (Point p : points){
Color color = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
g.setColor(color);
g.fillOval(p.x, p.y, POINT_SIZE, POINT_SIZE);
}
}else{
//if you only want the point at the
Color color = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
g.setColor(color);
g.fillOval((int)mouseX, (int)mouseY, POINT_SIZE, POINT_SIZE);
}
}
@Override
public void mouseDragged(MouseEvent e) {/*todo*/}
public static void main(String[] args0) {
JFrame frame = new JFrame();
frame.add(new Grids());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationByPlatform(true);
frame.pack();
frame.setVisible(true);
}
}
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.Graphics;
导入java.awt.Point;
导入java.awt.event.MouseEvent;
导入java.awt.event.MouseMotionListener;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.Random;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
公共类网格扩展JPanel实现MouseMotionListener{
公共最终静态内部宽度=1200,高度=800,点尺寸=10;
私人双摩丝,摩丝;
私有列表点;//存储所有跟踪点
private boolean drawTrail=true;//更改为false以仅在鼠标位置绘制一个点
公共电网(){
addMouseMotionListener(此);
setPreferredSize(新尺寸(宽度、高度));
挫折地面(颜色:白色);
if(牵引){
points=new ArrayList();//将鼠标点添加到集合中
}
}
@凌驾
public void mouseMoved(MouseEvent e){
//设置点之间的最小距离
if(Math.abs(mouseX-e.getX())>=POINT_SIZE | | Math.abs(mouseY-e.getY())>=POINT_SIZE){
if(牵引){
添加(新点(e.getX(),e.getY());
}
mouseX=e.getX();
mouseY=e.getY();
重新油漆();
}
}
@凌驾
公共组件(图形g){
超级组件(g);
Random rand=新的Random();
if(牵引){
//画出所有收集的点
对于(点p:点){
颜色=新颜色(rand.nextInt(256)、rand.nextInt(256)、rand.nextInt(256));
g、 设置颜色(颜色);
g、 圆角(p.x,p.y,点尺寸,点尺寸);
}
}否则{
//如果你只想在这一点上
颜色=新颜色(rand.nextInt(256)、rand.nextInt(256)、rand.nextInt(256));
g、 设置颜色(颜色);
g、 圆角((int)mouseX,(int)mouseY,POINT_SIZE,POINT_SIZE);
}
}
@凌驾
公共图书馆