1

So basically I'm playing around with Graphics and bunch fun things which I want to know before making a game I want to know different ways of how I can use different loops to do some fun things but I cannot figure out how I can use the INT I make in the method of mouseMoved and then use it in the Graphics Method. The code might show a better example of what I'm trying to explain.

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();
    }
}

This is the whole code and I tried using the MouseInfo to get the pointer location but it gets the location of the JFrame component and I want to get the mouse location on the JFrame itself and not off the component.

Ansharja
  • 1,237
  • 1
  • 14
  • 37
DaLegacy
  • 134
  • 1
  • 10
  • Do you still need an answer for it ? If you found a solution you may post an answer to your own question. – c0der Feb 13 '20 at 16:03
  • im open to any suggestions as this is for my college project and im trying to get highest marks so i need to refine my code as much as i can to get highest marks. – DaLegacy Feb 13 '20 at 16:13
  • 1
    See an [example](https://stackoverflow.com/a/46577656/3992939) of using mouse clicks to draw. – c0der Feb 13 '20 at 16:49
  • 1
    To accesses `mouseX, mouseY` all across the class make them fields: `private double mouseX, mouseY;` and use them: `public void mouseMoved(MouseEvent e) { mouseX = e.getX(); mouseY = e.getY(); }` – c0der Feb 14 '20 at 06:07
  • Thanks a lot for previous comment it was very helpful im just having trouble figuring out why im getting multiple mouses on screen when i drag my mouse higher it get more mouse inputs and if i drag my mouse lower it has less inputs on the screen (https://drive.google.com/open?id=1-qViyvlSB7maufXWy8W-g2hFFHC7zRwC) when im using this peace of code - On next comment. – DaLegacy Feb 14 '20 at 09:30
  • ``` int x = 0, y = 0; while (x < width) { if (mouseX <= 0) { x += 10; } else { x += mouseX; } while (y < height) { if (mouseY <= 0) { y += 10; } else { y += mouseY; } g.fillRoundRect(x, y, 10, 10, 10, 10); } } ``` Sorry for this way of sending code i do not know how else to send little snippets of code in the comments. – DaLegacy Feb 14 '20 at 09:30
  • It is not clear what you are trying to do. There are a few issues in the code.For example: the `for` and `while` loop within `paint` which draws multiple shapes, calling `repaint` within `paint` which should be avoided. Do your drawing on a `JPanel` by overriding `paintComponenet`. For more help post a new question. – c0der Feb 14 '20 at 10:19
  • I have fixed the JPanel as ive noticed that my self when i was trying to call the super.paintComponent. My problem is when my mouse reaches past the middle of the screen i get a new dot which get painted onto the screen. I just want 1 single dot which follows the mouse across the entire screen. If that makes sense if more information is needed ask as much as you need as ive been trying to figure this out for last few days. And thanks for all the support your giving me sorry if im little unclear sometimes. @c0der – DaLegacy Feb 14 '20 at 10:51
  • "_I just want 1 single dot which follows the mouse across the entire screen_" a single dot at the mouse last location, or a trail of dots following the mouse path ? – c0der Feb 14 '20 at 10:59
  • If you can give me a solution for both if possible. – DaLegacy Feb 14 '20 at 11:00

1 Answers1

1

I believe the following mre demonstrates the functionality you want.
Use drawTrail to turn trail drawing on or off:

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);
    }
}
c0der
  • 18,467
  • 6
  • 33
  • 65
  • OMG thanks your a genius i will have to look into this arraylist you have been using in past 2 examples you gave me as i think its a must for me to learn the functionality of that. Thank You so much!!! – DaLegacy Feb 14 '20 at 11:19