Adding mouseevent to circle - java

i'm trying to make an AimTrainer where you click a circle and it makes another appear which I can do but I can not figure out how to make it so when you click a circle it registers a mouseevent. I have no idea where to start. Right now my code just draws circles and they get smaller and smaller over time until they disappear. I want to make my circles clickable. Any help appreciated.
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package testingapplication;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.MouseInfo;
import java.awt.Point;
import java.util.Timer;
import java.util.TimerTask;
/**
*
* #author 18086
*/
public class GameForm2 extends javax.swing.JFrame {
private ConstantsAndLogic constantsandlogic;
public void UpdateBoard(){
// Cursor();
ConstantsAndLogic.circlesize();
repaint();
if (ConstantsAndLogic.iDiameterX == 0){
ConstantsAndLogic.main();
ConstantsAndLogic.resetdiameter();
}
}
public GameForm2 (){
myTimer.scheduleAtFixedRate(task, 0, 50);
this.setSize(100, 100);
this.setVisible(true);
constantsandlogic = new ConstantsAndLogic();
ConstantsAndLogic.main();
}
#Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.RED);
g.fillOval(ConstantsAndLogic.iCircleX,ConstantsAndLogic.iCircleY,ConstantsAndLogic.iDiameterX,ConstantsAndLogic.iDiameterY);
System.out.println(ConstantsAndLogic.iCircleX);
System.out.println(ConstantsAndLogic.iCircleY);
}
public void Cursor() {
Point cursor = MouseInfo.getPointerInfo().getLocation();
}
Timer myTimer = new Timer();
TimerTask task = new TimerTask() {
#Override
public void run() {
UpdateBoard();
}
};
}

Related

Java painting mechanism on OverlayLayout

Is there a way to create a layered system of components in Java so that at one moment only one layer will be updated (repainted)?
The simplest example would be a large map, with thousands of shapes and labels that will be placed on a "bottom" layer. And on the "top" layer you have another small shape, a custom tooltip would be a good example for this scenario. Now, whenever the tooltip's position is getting updated (by mouse move), even if you specifically call repaint() on that component only, the whole frame is being updated (thus a repaint is called on each layer, making the whole system pretty slow and ineffective).
I know you can achieve some optimization by caching contents as images and paint images instead of thousands of shapes, but, still, I'm interested in a "simpler" solution.
Thank you.
Sample code:
Basically there is a main frame (m) which loads a container (mc) and this container has two "layers" (ml). Now, when I call repaint() just for the 2nd layer, it's easy to see all paintComponent() are called again (console text appears but also BOTH rectangles change color to a new random one). And that is what I need to prevent.
File: m.java
import javax.swing.JFrame;
import java.awt.EventQueue;
import java.util.Timer;
import java.util.TimerTask;
public class m extends JFrame {
public mc omc;
public m() {
initUI();
}
private void initUI() {
this.pack();
this.setSize(600, 680);
this.setResizable(true);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.omc = new mc();
this.add(omc);
m self = this;
Timer oTimer = new Timer();
oTimer.schedule(new TimerTask() {
#Override
public void run() {
self.omc.oml1.repaint();
}
}, 2000);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
m ex = new m();
ex.setVisible(true);
}
});
}
}
File: mc.java
import javax.swing.JPanel;
import javax.swing.OverlayLayout;
import java.awt.Graphics;
public class mc extends JPanel {
public ml oml0;
public ml oml1;
public mc() {
this.setLayout(new OverlayLayout(this));
this.oml0 = new ml(0);
this.oml1 = new ml(1);
this.add(oml0);
this.add(oml1);
}
public void paintComponent(Graphics g) {
System.out.println("mc");
super.paintComponent(g);
}
}
File: ml.java
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
public class ml extends JPanel {
private int l = 0;
public ml(int ll) {
this.l = ll;
this.setOpaque(false);
}
public void paintComponent(Graphics g) {
System.out.println("ml:" + this.l);
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
if (this.l == 0) {
g2d.setPaint(new Color((int)Math.round(100 + 150 * Math.random()), (int)Math.round(100 + 150 * Math.random()), (int)Math.round(100 + 150 * Math.random())));
g2d.fillRect(30, 30, 300, 100);
}
if (this.l == 1) {
g2d.setPaint(new Color((int)Math.round(100 + 150 * Math.random()), (int)Math.round(100 + 150 * Math.random()), (int)Math.round(100 + 150 * Math.random())));
g2d.fillRect(130, 80, 300, 100);
}
}
}

JFrame repaint() and revalidate() only updating when window is resized on Mac os

I use this class for my school app projects. It is how I set the application up and it extends JFrame and implements Runnable. Now whenever I use this in school on a Windows computer and everything works and the screen updates, but at home on a Mac it doesn't. I use Eclipse neon with JDK 1.8.0_101
Please help me out, I can't test any projects at home cause of this.
import java.awt.Graphics;
import javax.swing.JFrame;
public abstract class GUIApplication extends JFrame implements Runnable{
private Screen currentScreen;
//no main, cant instentiate an abstract class
public GUIApplication(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int x=40;
int y=40;
int width=1000;
int height=640;
setBounds(x,y,width,height);
initScreen();
setVisible(true);
}
//this is a method for creating the starting screen
protected abstract void initScreen();
public void setScreen(Screen screen){
//stop controls from previous screen
removeListeners();
setCurrentScreen(screen);
//add new controls
addListeners();
}
private void removeListeners(){
if(getCurrentScreen() != null){
if(getCurrentScreen().getMouseListener() != null) removeMouseListener(getCurrentScreen().getMouseListener());
if(getCurrentScreen().getMouseMotionListener() != null) removeMouseMotionListener(getCurrentScreen().getMouseMotionListener());
if(getCurrentScreen().getKeyListener() != null) removeKeyListener(getCurrentScreen().getKeyListener());
// if(currentScreen.getMouseWheelListener() != null) removeMouseWheelListener(currentScreen.getMouseWheelListener());
}
}
private void addListeners(){
if(getCurrentScreen() != null){
if(getCurrentScreen().getMouseListener() != null)addMouseListener(getCurrentScreen().getMouseListener());
if(getCurrentScreen().getMouseMotionListener() != null) addMouseMotionListener(getCurrentScreen().getMouseMotionListener());
if(getCurrentScreen().getKeyListener() != null){
addKeyListener(getCurrentScreen().getKeyListener());
}
// if(currentScreen.getMouseWheelListener() != null) addMouseWheelListener(currentScreen.getMouseWheelListener());
}
}
public void paint(Graphics g){
g.drawImage(getCurrentScreen().getImage(), 0, 0, null);
}
public void run(){
while(true){
getCurrentScreen().update();
repaint();
try {
Thread.sleep(30);
repaint();
revalidate();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public Screen getCurrentScreen() {
return currentScreen;
}
public void setCurrentScreen(Screen currentScreen) {
this.currentScreen = currentScreen;
}
}
This is how a game would start:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.ArrayList;
import javax.swing.JFrame;
import game.mainScreenTeam.Dragon;
import game.mainScreenTeam.HomeScreen;
import game.miniGameTeam.GameInstructions;
import game.miniGameTeam.GameScreen;
import game.miniGameTeam.HighScoreScreen;
import game.shopScreen.BuyScreenWendy;
import game.shopScreen.HomeShopScreen;
import game.shopScreen.SellShopZheng;
import guiPractice.GUIApplication;
import guiPractice.Screen;
import guiPractice.components.AnimatedComponent;
/**
* #author Kat
*
*/
public class DragonLand extends GUIApplication {
public static DragonLand game;
public static int coins = 1500;
public static HomeScreen homeScreen;
public static Screen shopMain; // shop 1
public static Screen sellScreen; // shop 2
public static Screen buyScreen; // shop 3
public static Screen highscoreScreen; // high score
public static GameScreen miniGameScreen; // minigame
public static Screen gameInstructionsScreen;
public static Screen HelpScreen;
public static Color NAVY;
public static Color BRIGHT_PINK;
public static Color LIGHT_PINK;
public static Color LIGHT_NUDE;
public static Color DARKER_NUDE;
/**
*
*/
// public static void addDragon(AnimatedComponent a){
// dragonList.add(a);
// }
public DragonLand() {
}
/* (non-Javadoc)
* #see guiPractice.GUIApplication#initScreen()
*/
#Override
protected void initScreen() {
initColors();
miniGameScreen = new GameScreen(getWidth(),getHeight());
shopMain = new HomeShopScreen(getWidth(),getHeight());
sellScreen = new SellShopZheng(getWidth(),getHeight());
homeScreen = new HomeScreen(getWidth(),getHeight());
buyScreen = new BuyScreenWendy(getWidth(),getHeight());
highscoreScreen = new HighScoreScreen(getWidth(),getHeight());
HomeScreen.jenCode = new game.mainScreenTeam.HomeJenniber();
gameInstructionsScreen = new GameInstructions(getWidth(), getHeight());
setScreen(homeScreen);
}
private void initColors() {
NAVY = new Color(62,74,99);
BRIGHT_PINK = new Color(224,102,102);
LIGHT_PINK = new Color(248,186,182);
LIGHT_NUDE = new Color(244,215,183);
DARKER_NUDE = new Color(230,195,147);
}
/**
* #param args
*/
public static void main(String[] args) {
game = new DragonLand();
Thread go = new Thread(game);
go.start();
}
//public coin getter + setter
public void setCoins(int x){
coins = x;
}
public int getCoins(){
return coins;
}
}
This is the home screen
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import game.DragonLand;
import guiPractice.ClickableScreen;
import guiPractice.components.Action;
import guiPractice.components.AnimatedComponent;
import guiPractice.components.Button;
import guiPractice.components.Graphic;
import guiPractice.components.TextLabel;
import guiPractice.components.Visible;
import guiPractice.sampleGames.MouseFollower;
/**
* #author Kat
* #author Jenniber
*
*/
public class HomeScreen extends ClickableScreen implements Runnable{
private Graphic background;
public static HomeJenniber jenCode;
public HomeScreen(int width, int height) {
super(width, height);
Thread play = new Thread(this);
play.start();
}
#Override
public void initAllObjects(ArrayList<Visible> viewObjects) {
background=new Graphic(0,0,getWidth(),getHeight(),"img/Grassland.png");
viewObjects.add(background);
HomeKat katCode=new HomeKat(viewObjects, getWidth(), getHeight());
}
#Override
public void run() {
}
}
katCode adds buttons to the screen and image annimations
public void paint(Graphics g){
g.drawImage(getCurrentScreen().getImage(), 0, 0, null);
}
Don't override paint() on a JFrame.
The proper way to do custom painting is to override paintComponent(...) on a JPanel (or JComponent) and then you can set the content pane of the frame to this panel. And don't forget to invoke super.paintComponent(...) as the first statement in the method. Read the section from the Swing tutorial on Custom Painting for more information and working examples.
However if you do get lazy, then at minimum you need to invoke super.paint(...) as the first statement in the paint(...) method.
Also, I doubt you need the revalidate(), since you don't appear to be adding/removing components from the frame.
But in general the order should be:
revalidate(); // to invoke the layout manager
repaint(); // paint components in new location.
I also don't know why you are invoking the update() method. That seems like old AWT code which you don't use in Swing. I suggest you take a look at the tutorial link I gave you and look at the table of contents for other Swing basics.

Repaint Aero JFrame

That when painted for the first time, it is not painted correctly.
However, when I minimize the frame and restore it, it is painted correctly.
How will I fix this? I have tried repaint().
Here is the code
import javax.swing.JComponent;
import javax.swing.JFrame;
import com.sun.jna.Function;
import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
import com.sun.jna.Structure;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinNT.HRESULT;
/**
* #author ex0b1t
*
*/
public class Aero {
public void enableAeroEffect(JFrame frame) {
NativeLibrary dwmapi = NativeLibrary.getInstance("dwmapi");
HWND aeroFrameHWND = new HWND(Native.getWindowPointer(frame));
MARGINS margins = new MARGINS();
margins.cxLeftWidth = -1;
margins.cxRightWidth = -1;
margins.cyBottomHeight = -1;
margins.cyTopHeight = -1;
Function extendFrameIntoClientArea = dwmapi
.getFunction("DwmExtendFrameIntoClientArea");
HRESULT result = (HRESULT) extendFrameIntoClientArea.invoke(
HRESULT.class, new Object[] { aeroFrameHWND, margins });
if (result.intValue() != 0)
System.err.println("Call to DwmExtendFrameIntoClientArea failed.");
frame.getRootPane().setDoubleBuffered(false);
frame.getRootPane().setOpaque(false);
if (frame.getRootPane().getContentPane() instanceof JComponent) {
JComponent content = (JComponent) frame.getRootPane().getContentPane();
content.setOpaque(false);
content.setDoubleBuffered(false);
}
}
/**
* #author ex0b1t
*
*/
public class MARGINS extends Structure implements Structure.ByReference {
public int cxLeftWidth;
public int cxRightWidth;
public int cyTopHeight;
public int cyBottomHeight;
}
}
import javax.swing.JFrame;
/**
* #author ex0b1t
*
*/
public class MediaManager extends JFrame {
private static final long serialVersionUID = -8440221168382362270L;
public MediaManager() {
setTitle("Media Manager");
setSize(800, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
/**
* #param args
*/
public static void main(String[]args){
MediaManager mediamanager = new MediaManager();
mediamanager.setVisible(true);
new Aero().enableAeroEffect(mediamanager);
mediamanager.repaint();
}
}
Thank in advance.
What do you mean when you say it is not correctly painted?
I had a similar problem where when my frame was painted for the first time the contentPane was totally black. When i minimized the frame and then maximizing it,it was painted correctly (but not always- there were times i had to do it 2 or 3 times.) It turned out it was the drivers of my graphics card. I reinstalled them and it worked fine! (I had a Randeon)
also try
SwingUtilities.invokeLater(new Runnable(){
#Override
public void run(){
mediamanager.setVisible(true);
new Aero().enableAeroEffect(mediamanager);
mediamanager.repaint();
}
}
Threads and Swing
http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html
SwingUtilities.invokeLater
setting the extended state of the frame to iconified and back to normal will paint the frame correctley. however when i add a component to the frame the backround is regrawn incorectley.
frame.setExtendedState(JFrame.ICONIFIED);
frame.setExtendedState(JFrame.NORMAL);

Mouse Pointer Problem in Java Swing

I have created the following simple Java Swing program which outputs a 3*3 square in the window every time the user clicks their mouse. The squares remain in the window even if the user clicks more than once. The program compiles and runs just fine, however, when one clicks in the window the square is drawn far below where the mouse pointer is. I've been racking my brain over this one for a while -- what can I change here to get the square to appear exactly with the pointer on each click? Many thanks for any help!
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class ClickCloud extends JComponent {
final ArrayList<Point2D> points = new ArrayList<Point2D>();
public void addPoint(Point2D a) {
points.add(a);
}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
for (int i = 0; i < points.size(); i++) {
Point2D aPoint = points.get(i);
g2.draw(new Rectangle2D.Double(aPoint.getX(), aPoint.getY(), 3, 3));
}
}
public static void main(String[] args) {
final ClickCloud cloud = new ClickCloud();
JFrame aFrame = new JFrame();
class ClickListen implements MouseListener {
#Override
public void mouseClicked(MouseEvent arg0) {
}
#Override
public void mouseEntered(MouseEvent arg0) {
}
#Override
public void mouseExited(MouseEvent arg0) {
}
public void mousePressed(MouseEvent arg0) {
cloud.addPoint(arg0.getPoint());
cloud.repaint();
}
#Override
public void mouseReleased(MouseEvent arg0) {
}
}
aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
aFrame.setSize(500, 500);
aFrame.add(cloud);
aFrame.addMouseListener(new ClickListen());
aFrame.setVisible(true);
}
}
You're adding the MouseListener to the JFrame, but displaying the results in the JComponent and relative to the JComponent. So the location of the Point clicked will be relative to the JFrame's coordinates, but then displayed relative to the JComponent's coordinates which will shift things down by the distance of the title bar. Instead simply add the MouseListener to the same component that is responsible for displaying the results so that the display and clicking coordinates match:
aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
aFrame.setSize(500, 500);
aFrame.add(cloud);
//!! aFrame.addMouseListener(new ClickListen()); // !! Removed
cloud.addMouseListener(new ClickListen()); // !! added
aFrame.setVisible(true);
By the way: Thanks for creating and posting a decent SSCCE as this makes it so much easier to analyse and solve your problem.

Java mouse motion anywhere on screen

I'm sure this is possible but all my searching is coming up blank.
In Java is it possible to register for a mouse motion event outside of a Java app? So if the mouse pointer moves anywhere on the screen I get a call back. An approximation is possible with polling MouseInfo.getPointerInfo but there must be a better way.
Thanks
To explain the use case:
It's just for a pet project but basically firing events when the mouse hits the edge of the screen. I was also thinking that different events could be fired if you try to push past the edge of the screen. And for this I thought a mouse motion listener might be more appropriate.
java.awt.event.MouseMotionListener is only going to give you information about mouse movement inside your application window. For events that occur outside that window, there is no way around MouseInfo.getPointerInfo. However, you could write a (potentially singleton) class that polls the pointer info in regular intervals and allows MouseMotionListeners to be added:
import java.awt.Component;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.HashSet;
import java.util.Set;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
/**
* This class checks the position every #DELAY milliseconds and
* informs all registered MouseMotionListeners about position updates.
*/
public class MouseObserver {
/* the resolution of the mouse motion */
private static final int DELAY = 10;
private Component component;
private Timer timer;
private Set<MouseMotionListener> mouseMotionListeners;
protected MouseObserver(Component component) {
if (component == null) {
throw new IllegalArgumentException("Null component not allowed.");
}
this.component = component;
/* poll mouse coordinates at the given rate */
timer = new Timer(DELAY, new ActionListener() {
private Point lastPoint = MouseInfo.getPointerInfo().getLocation();
/* called every DELAY milliseconds to fetch the
* current mouse coordinates */
public synchronized void actionPerformed(ActionEvent e) {
Point point = MouseInfo.getPointerInfo().getLocation();
if (!point.equals(lastPoint)) {
fireMouseMotionEvent(point);
}
lastPoint = point;
}
});
mouseMotionListeners = new HashSet<MouseMotionListener>();
}
public Component getComponent() {
return component;
}
public void start() {
timer.start();
}
public void stop() {
timer.stop();
}
public void addMouseMotionListener(MouseMotionListener listener) {
synchronized (mouseMotionListeners) {
mouseMotionListeners.add(listener);
}
}
public void removeMouseMotionListener(MouseMotionListener listener) {
synchronized (mouseMotionListeners) {
mouseMotionListeners.remove(listener);
}
}
protected void fireMouseMotionEvent(Point point) {
synchronized (mouseMotionListeners) {
for (final MouseMotionListener listener : mouseMotionListeners) {
final MouseEvent event =
new MouseEvent(component, MouseEvent.MOUSE_MOVED, System.currentTimeMillis(),
0, point.x, point.y, 0, false);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
listener.mouseMoved(event);
}
});
}
}
}
/* Testing the ovserver */
public static void main(String[] args) {
JFrame main = new JFrame("dummy...");
main.setSize(100,100);
main.setVisible(true);
MouseObserver mo = new MouseObserver(main);
mo.addMouseMotionListener(new MouseMotionListener() {
public void mouseMoved(MouseEvent e) {
System.out.println("mouse moved: " + e.getPoint());
}
public void mouseDragged(MouseEvent e) {
System.out.println("mouse dragged: " + e.getPoint());
}
});
mo.start();
}
}
Beware that there are some notable differences from your standard MouseMotionListener though:
You will only receive mouseMoved events, never mouseDragged events. That's because there is no way to receive information about clicks outside the main window.
For similar reasons, the modifiers of each MouseEvent will be always be 0.
The same goes for the values clickCount, popupTrigger, button
You will need to provide a dummy java.awt.Component that will be used as the (fake) source of the MouseEvents - null values are not allowed here.

Categories