Java Swing MouseListener issue

I'll have to segment the code in comments because it's too large for Discord. I have a frame, a container, and two components in the container. I made the components draggable with
final Point start = new Point();
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
System.out.println("click");
start.setLocation(e.getPoint());
bringToFront();

switch (e.getButton()) {
case MouseEvent.BUTTON2:
getParent().remove(ContainerItemStack.this);
break;
case MouseEvent.BUTTON3:
getParent().split(ContainerItemStack.this);
break;
}
}
});
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
System.out.println("drag");
Point c = e.getLocationOnScreen();
Point p = getParent().getLocationOnScreen();
setLocation(c.x - p.x - start.x, c.y - p.y - start.y);
bringToFront();
}
});
final Point start = new Point();
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
System.out.println("click");
start.setLocation(e.getPoint());
bringToFront();

switch (e.getButton()) {
case MouseEvent.BUTTON2:
getParent().remove(ContainerItemStack.this);
break;
case MouseEvent.BUTTON3:
getParent().split(ContainerItemStack.this);
break;
}
}
});
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
System.out.println("drag");
Point c = e.getLocationOnScreen();
Point p = getParent().getLocationOnScreen();
setLocation(c.x - p.x - start.x, c.y - p.y - start.y);
bringToFront();
}
});
It was working a few minutes ago, but now the mouse listeners aren't being triggered. Full code in comments
5 Replies
JavaBot
JavaBot9mo ago
This post has been reserved for your question.
Hey @The Typhothanian! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
The Typhothanian
The TyphothanianOP9mo ago
package net.typho.pnegative;

import net.typho.pnegative.ui.ContainerItemStack;
import net.typho.pnegative.ui.ItemContainer;
import net.typho.pnegative.world.ItemStack;
import net.typho.pnegative.world.ItemType;
import net.typho.utils.resources.TFiles;

import javax.swing.*;
import java.awt.*;

public class Frame extends JFrame {
Frame() {
super("Project Negative");

Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
setSize(screen.width / 2, screen.height / 2);
setMinimumSize(new Dimension(400, 300));
setLocationRelativeTo(null);

setLayout(new BorderLayout());

ItemContainer container = new ItemContainer();
container.add(new ContainerItemStack(new ItemStack(new ItemType("stone", "blocks/stone/0.png"), 73)));
container.add(new ContainerItemStack(50, 50, new ItemStack(new ItemType("grass", "blocks/grass/side.png"), 54)));
add(container, BorderLayout.CENTER);

setIconImage(TFiles.getImage("logo/icon.png"));

setDefaultCloseOperation(EXIT_ON_CLOSE);

setVisible(true);
}
}
package net.typho.pnegative;

import net.typho.pnegative.ui.ContainerItemStack;
import net.typho.pnegative.ui.ItemContainer;
import net.typho.pnegative.world.ItemStack;
import net.typho.pnegative.world.ItemType;
import net.typho.utils.resources.TFiles;

import javax.swing.*;
import java.awt.*;

public class Frame extends JFrame {
Frame() {
super("Project Negative");

Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
setSize(screen.width / 2, screen.height / 2);
setMinimumSize(new Dimension(400, 300));
setLocationRelativeTo(null);

setLayout(new BorderLayout());

ItemContainer container = new ItemContainer();
container.add(new ContainerItemStack(new ItemStack(new ItemType("stone", "blocks/stone/0.png"), 73)));
container.add(new ContainerItemStack(50, 50, new ItemStack(new ItemType("grass", "blocks/grass/side.png"), 54)));
add(container, BorderLayout.CENTER);

setIconImage(TFiles.getImage("logo/icon.png"));

setDefaultCloseOperation(EXIT_ON_CLOSE);

setVisible(true);
}
}
package net.typho.pnegative.ui;

import net.typho.pnegative.world.ItemStack;

import javax.swing.*;
import java.awt.*;
import java.util.HashSet;
import java.util.Set;

public class ItemContainer extends JComponent {
private Set<ContainerItemStack> items = new HashSet<>();
public int capacity = 4096;

public ItemContainer(Set<ContainerItemStack> items, int capacity) {
this.items = items;
this.capacity = capacity;
}

public ItemContainer(int capacity) {
this.capacity = capacity;
}

public ItemContainer(Set<ContainerItemStack> items) {
this.items = items;
}

public ItemContainer() {
}

{
setLayout(null);
}

public boolean add(ContainerItemStack item) {
if (remainingSpace() >= item.stack.amount) {
items.add(item);
add((Component) item);
repaint();
return true;
}

return false;
}

public void remove(ContainerItemStack item) {
items.remove(item);
remove((Component) item);
repaint();
}
...
package net.typho.pnegative.ui;

import net.typho.pnegative.world.ItemStack;

import javax.swing.*;
import java.awt.*;
import java.util.HashSet;
import java.util.Set;

public class ItemContainer extends JComponent {
private Set<ContainerItemStack> items = new HashSet<>();
public int capacity = 4096;

public ItemContainer(Set<ContainerItemStack> items, int capacity) {
this.items = items;
this.capacity = capacity;
}

public ItemContainer(int capacity) {
this.capacity = capacity;
}

public ItemContainer(Set<ContainerItemStack> items) {
this.items = items;
}

public ItemContainer() {
}

{
setLayout(null);
}

public boolean add(ContainerItemStack item) {
if (remainingSpace() >= item.stack.amount) {
items.add(item);
add((Component) item);
repaint();
return true;
}

return false;
}

public void remove(ContainerItemStack item) {
items.remove(item);
remove((Component) item);
repaint();
}
...
...
public void split(ContainerItemStack item) {
if (items.contains(item)) {
double x = Math.random();
double y = Math.random();

if (x < 0.75 && x > 0.5) {
x = 0.75;
}

if (x > 0.25 && x < 0.5) {
x = 0.25;
}

if (y < 0.75 && y > 0.5) {
y = 0.75;
}

if (y > 0.25 && y < 0.5) {
y = 0.25;
}

add(new ContainerItemStack((int) (item.getX() + x * item.getWidth() * 2 - item.getWidth()), (int) (item.getY() + y * item.getHeight() * 2 - item.getHeight()), new ItemStack(item.stack.type, item.stack.amount / 2)));
item.stack.amount = item.stack.amount - item.stack.amount / 2;
repaint();
}
}

public int spaceUsed() {
return items.stream().mapToInt(item -> item.stack.amount).sum();
}

public int remainingSpace() {
return capacity - spaceUsed();
}

@Override
public Dimension getPreferredSize() {
return getSize();
}

@Override
protected void paintComponent(Graphics g) {
g.setColor(Color.GRAY);
g.fillRect(0, 0, getWidth(), getHeight());
}
}
...
public void split(ContainerItemStack item) {
if (items.contains(item)) {
double x = Math.random();
double y = Math.random();

if (x < 0.75 && x > 0.5) {
x = 0.75;
}

if (x > 0.25 && x < 0.5) {
x = 0.25;
}

if (y < 0.75 && y > 0.5) {
y = 0.75;
}

if (y > 0.25 && y < 0.5) {
y = 0.25;
}

add(new ContainerItemStack((int) (item.getX() + x * item.getWidth() * 2 - item.getWidth()), (int) (item.getY() + y * item.getHeight() * 2 - item.getHeight()), new ItemStack(item.stack.type, item.stack.amount / 2)));
item.stack.amount = item.stack.amount - item.stack.amount / 2;
repaint();
}
}

public int spaceUsed() {
return items.stream().mapToInt(item -> item.stack.amount).sum();
}

public int remainingSpace() {
return capacity - spaceUsed();
}

@Override
public Dimension getPreferredSize() {
return getSize();
}

@Override
protected void paintComponent(Graphics g) {
g.setColor(Color.GRAY);
g.fillRect(0, 0, getWidth(), getHeight());
}
}
package net.typho.pnegative.ui;

import net.typho.pnegative.world.ItemStack;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

public class ContainerItemStack extends JComponent {
public ItemStack stack;

public ContainerItemStack(int x, int y, ItemStack stack) {
this.stack = stack;
setBounds(x, y, 32, 32);
}

public ContainerItemStack(ItemStack stack) {
this(0, 0, stack);
}

{
final Point start = new Point();
setFocusable(true);
System.out.println("init");
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
System.out.println("click");
start.setLocation(e.getPoint());
bringToFront();

switch (e.getButton()) {
case MouseEvent.BUTTON2:
getParent().remove(ContainerItemStack.this);
break;
case MouseEvent.BUTTON3:
getParent().split(ContainerItemStack.this);
break;
}
}
});
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
System.out.println("drag");
Point c = e.getLocationOnScreen();
Point p = getParent().getLocationOnScreen();
setLocation(c.x - p.x - start.x, c.y - p.y - start.y);
bringToFront();
}
});
}
...
package net.typho.pnegative.ui;

import net.typho.pnegative.world.ItemStack;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

public class ContainerItemStack extends JComponent {
public ItemStack stack;

public ContainerItemStack(int x, int y, ItemStack stack) {
this.stack = stack;
setBounds(x, y, 32, 32);
}

public ContainerItemStack(ItemStack stack) {
this(0, 0, stack);
}

{
final Point start = new Point();
setFocusable(true);
System.out.println("init");
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
System.out.println("click");
start.setLocation(e.getPoint());
bringToFront();

switch (e.getButton()) {
case MouseEvent.BUTTON2:
getParent().remove(ContainerItemStack.this);
break;
case MouseEvent.BUTTON3:
getParent().split(ContainerItemStack.this);
break;
}
}
});
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
System.out.println("drag");
Point c = e.getLocationOnScreen();
Point p = getParent().getLocationOnScreen();
setLocation(c.x - p.x - start.x, c.y - p.y - start.y);
bringToFront();
}
});
}
...
@Override
public void addNotify() {
if (!(super.getParent() instanceof ItemContainer)) {
throw new IllegalStateException("Cannot add a ContainerItemStack to a " + getParent().getClass());
}
requestFocus();
}

@Override
public void removeNotify() {
getParent().remove(this);
}

@Override
public ItemContainer getParent() {
return (ItemContainer) super.getParent();
}

@Override
public Dimension getPreferredSize() {
return getSize();
}

@Override
protected void paintComponent(Graphics g) {
System.out.println("paint");
g.drawImage(stack.type.texture, 0, 0, getWidth(), getHeight(), this);

if (stack.amount != 1) {
String text = String.valueOf(stack.amount);
g.setColor(Color.BLACK);
g.setFont(new Font("Times New Roman", Font.BOLD, Math.min(getWidth(), getHeight()) / 4 * 3));

FontMetrics metrics = g.getFontMetrics();
g.drawString(text, (getWidth() - metrics.stringWidth(text)) / 2 + 2, (getHeight() - metrics.getHeight()) / 2 + metrics.getAscent() + 2);

g.setColor(Color.WHITE);

g.drawString(text, (getWidth() - metrics.stringWidth(text)) / 2, (getHeight() - metrics.getHeight()) / 2 + metrics.getAscent());
}
}

public void bringToFront() {
getParent().setComponentZOrder(this, 0);
}
}
@Override
public void addNotify() {
if (!(super.getParent() instanceof ItemContainer)) {
throw new IllegalStateException("Cannot add a ContainerItemStack to a " + getParent().getClass());
}
requestFocus();
}

@Override
public void removeNotify() {
getParent().remove(this);
}

@Override
public ItemContainer getParent() {
return (ItemContainer) super.getParent();
}

@Override
public Dimension getPreferredSize() {
return getSize();
}

@Override
protected void paintComponent(Graphics g) {
System.out.println("paint");
g.drawImage(stack.type.texture, 0, 0, getWidth(), getHeight(), this);

if (stack.amount != 1) {
String text = String.valueOf(stack.amount);
g.setColor(Color.BLACK);
g.setFont(new Font("Times New Roman", Font.BOLD, Math.min(getWidth(), getHeight()) / 4 * 3));

FontMetrics metrics = g.getFontMetrics();
g.drawString(text, (getWidth() - metrics.stringWidth(text)) / 2 + 2, (getHeight() - metrics.getHeight()) / 2 + metrics.getAscent() + 2);

g.setColor(Color.WHITE);

g.drawString(text, (getWidth() - metrics.stringWidth(text)) / 2, (getHeight() - metrics.getHeight()) / 2 + metrics.getAscent());
}
}

public void bringToFront() {
getParent().setComponentZOrder(this, 0);
}
}
Help pls
JavaBot
JavaBot9mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
The Typhothanian
The TyphothanianOP9mo ago
issue was the add notify for some reason maybe getParent in that method is broken
JavaBot
JavaBot9mo ago
Post Closed
This post has been closed by <@801145088830210129>.
Want results from more Discord servers?
Add your server