Currently I'm experincing with things that related to GUI making in Java and I want to try and make GUI that will look as follow :
How do you suggest to do it?
At first I though making class called "My Frame" it'll contain an array that holds the points of each polygon (so each polygon will be a separate frame).
But after rethinking a bit I thought of the purpose of those triangles, I want to make them buttons where each press on one of the button changes the content inside the Octagon, so it's more like the triangles are sort of buttons and the Octagon itself is the content pane, so how do you guys suggest me to achieve this?
Also another related question is that I want to make those triangles move with the Octagon, if the Octagon is being dragged by the mouse, the triangles will move with it as well so should I just parent them to the Octagon frame?
Another thing that I wanted to ask is since I set the frame to be undecorated for the purpose of changing it's shape I eliminated the option to resize the frame as well as I made the iconify and close buttons to disappear, so how can I add those features back, I searched the web but I didn't find any explanation to this.
EDITED DIALOG CLASS :
package rt;
import java.awt.Color;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ContainerListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.event.MouseInputAdapter;
import javax.swing.event.MouseInputListener;
public class MyDialog extends JDialog {
private int pointX;
private int pointY;
JLabel octagonLabel;
JLabel triangleAboutLabel;
MyMouseInputAdapter mmia;
public MyDialog() {
octagonLabel = createOctagonLabel();
triangleAboutLabel = createTriangleAboutLabel();
JTextField textField = createTextField();
setContentPane(octagonLabel);
add(textField);
setUndecorated(true);
setBackground(new Color(0, 0, 0, 0));
pack();
setLocationRelativeTo(null);
mmia = new MyMouseInputAdapter();
addMouseListener(mmia);
addMouseMotionListener(mmia);
setVisible(true);
}
private JTextField createTextField() {
final JTextField field = new JTextField(20);
field.setText("Type \"exit\" to terminate.");
field.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
String text = field.getText();
if ("exit".equalsIgnoreCase(text)) {
System.exit(0);
}
}
});
return field;
}
private JLabel createOctagonLabel() {
Image image = null;
try {
image = ImageIO.read(Class.class.getResourceAsStream("/resources/gui/octagon.png"));
} catch (IOException ex) {
Logger.getLogger(MyDialog.class.getName()).log(Level.SEVERE, null, ex);
}
JLabel label = new JLabel(new ImageIcon(image));
label.setLayout(new GridBagLayout());
return label;
}
private JLabel createTriangleAboutLabel() {
Image image = null;
try {
image = ImageIO.read(Class.class.getResourceAsStream("/resources/gui/triangle_about.png"));
} catch (IOException ex) {
Logger.getLogger(MyDialog.class.getName()).log(Level.SEVERE, null, ex);
}
JLabel label = new JLabel(new ImageIcon(image));
label.setLayout(new GridBagLayout());
label.setLocation(octagonLabel.getLocation().x - 32, octagonLabel.getLocation().y - 32);
return label;
}
private class MyMouseInputAdapter extends MouseInputAdapter {
@Override
public void mouseDragged(MouseEvent e) {
MyDialog.this.setLocation(MyDialog.this.getLocation().x + e.getX() - pointX, MyDialog.this.getLocation().y + e.getY() - pointY);
}
@Override
public void mousePressed(MouseEvent e) {
pointX = e.getX();
pointY = e.getY();
}
}
}
LATEST EDIT : @peeskillet sorry for not being here the last couple of days, family member was in hospital .. I tried to implement what you said I made different mouse listener for the corner triangles, but I came across a problem, the mouseEntered and MouseExited events get called when the mouse position is withing the BORDER of the label (not the polygon) so if I enter the mouse into the borders of the label (but the position of the mouse is still outside of the polygon) and then I decide to move the mouse (while I'm still inside the borders of the label) inside the polygon's borders it won't check if it's inside since mouseEntered have already been called and the check for the point inside of the polygon is taking place in that method (same issue for mouseExited) so I tried instead of overriding the mouseEntered and mouseExited doing my own methods just called them mouseEntered2 and mouseExited2 and I call them every time the mouse moves with mouseMoves event, but after doing it when i move the mouse it doesnt even get called the mouseMoved event and I don't know why.. here's the code :
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Polygon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class DragDialog extends JDialog {
private int pointX;
private int pointY;
public DragDialog() {
JTextField textField = createTextField();
setContentPane(createBackgroundPanel());
setUndecorated(true);
setBackground(new Color(0, 0, 0, 0));
pack();
setLocationRelativeTo(null);
setVisible(true);
}
private JPanel createBackgroundPanel() {
JPanel panel = new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(527, 527);
}
};
panel.setOpaque(false);
panel.setLayout(null);
String[] filePaths = getFilePaths();
Map<String, ImageIcon> imageMap = initImages(filePaths);
Map<String, Rectangle> rectMap = createRectangles();
Map <String, Polygon> polygonMap = createPolygons();
for (Map.Entry<String, Rectangle> entry: rectMap.entrySet()) {
JLabel label = new JLabel();
label.setOpaque(false);
String fullImageKey = entry.getKey() + "-default";
ImageIcon icon = imageMap.get(fullImageKey);
label.setIcon(icon);
label.setBounds(entry.getValue());
if (entry.getKey().equals("northwest") ||
entry.getKey().equals("northeast") ||
entry.getKey().equals("southwest") ||
entry.getKey().equals("southeast")) {
label.addMouseListener(new CornerLabelMouseListener(entry.getKey(), imageMap, polygonMap.get(entry.getKey())));
}
else {
label.addMouseListener(new SideLabelMouseListener(entry.getKey(), imageMap));
}
panel.add(label);
}
JLabel octagon = createOctagonLabel();
octagon.setBounds(85, 85, 357, 357);
panel.add(octagon);
return panel;
}
private Map<String, Rectangle> createRectangles() {
Map<String, Rectangle> rectMap = new HashMap<>();
rectMap.put("north", new Rectangle(191, 0, 145, 73));
rectMap.put("northwest", new Rectangle(74, 74, 103, 103));
rectMap.put("northeast", new Rectangle(348, 74, 103, 103));
rectMap.put("west", new Rectangle(0, 190, 73, 145));
rectMap.put("east", new Rectangle(453, 190, 73, 145));
rectMap.put("south", new Rectangle(190, 453, 145, 73));
rectMap.put("southwest", new Rectangle(74, 348, 103, 103));
rectMap.put("southeast", new Rectangle(349, 349, 103, 103));
return rectMap;
}
private Map <String, Polygon> createPolygons() {
Map <String, Polygon> polygonMap = new HashMap <> ();
int [] x = new int[3];
int [] y = new int [3];
x[0] = 0;
x[1] = 103;
x[2] = 0;
y[0] = 0;
y[1] = 0;
y[2] = 103;
polygonMap.put("northwest", new Polygon(x, y, 3));
x[2] = 103;
polygonMap.put("northeast", new Polygon(x, y, 3));
y[0] = 103;
polygonMap.put("southeast", new Polygon(x, y, 3));
x[1] = 0;
polygonMap.put("southwest", new Polygon(x, y, 3));
return polygonMap;
}
private class SideLabelMouseListener extends MouseAdapter {
private String position;
private Map<String, ImageIcon> imageMap;
public SideLabelMouseListener(String position, Map<String, ImageIcon> imageMap) {
this.imageMap = imageMap;
this.position = position;
}
@Override
public void mousePressed(MouseEvent e) {
System.out.println(position + " pressed");
}
@Override
public void mouseEntered(MouseEvent e) {
JLabel label = (JLabel)e.getSource();
String fullName = position + "-hovered";
ImageIcon icon = imageMap.get(fullName);
label.setIcon(icon);
}
@Override
public void mouseExited(MouseEvent e) {
JLabel label = (JLabel)e.getSource();
String fullName = position + "-default";
ImageIcon icon = imageMap.get(fullName);
label.setIcon(icon);
}
}
private class CornerLabelMouseListener extends MouseAdapter{
private String position;
private Map <String, ImageIcon> imageMap;
private Polygon polygon;
public CornerLabelMouseListener(String position, Map <String, ImageIcon> imageMap, Polygon polygon) {
this.imageMap = imageMap;
this.position = position;
this.polygon= polygon;
}
@Override
public void mousePressed(MouseEvent e) {
if (polygon.contains(e.getPoint())) {
System.out.println(position + " pressed");
}
}
@Override
public void mouseMoved(MouseEvent e) {
System.out.println("moving");
mouseEntered2(e);
mouseExited2(e);
}
//@Override
public void mouseEntered2(MouseEvent e) {
System.out.println("point = " +e.getPoint().toString());
if (polygon.contains(e.getPoint())) {
System.out.println("here");
JLabel label = (JLabel)e.getSource();
String fullName = position + "-hovered";
ImageIcon icon = imageMap.get(fullName);
label.setIcon(icon);
}
}
//@Override
public void mouseExited2(MouseEvent e) {
if (!polygon.contains(e.getPoint())) {
JLabel label = (JLabel)e.getSource();
String fullName = position + "-default";
ImageIcon icon = imageMap.get(fullName);
label.setIcon(icon);
}
}
}
private String[] getFilePaths() {
String[] paths = { "north-default.png", "north-hovered.png",
"northwest-default.png", "northwest-hovered.png",
"northeast-default.png", "northeast-hovered.png",
"west-default.png", "west-hovered.png", "east-default.png",
"east-hovered.png", "south-default.png", "south-hovered.png",
"southwest-default.png", "southwest-hovered.png",
"southeast-default.png", "southeast-hovered.png"
};
return paths;
}
private Map<String, ImageIcon> initImages(String[] paths) {
Map<String, ImageIcon> map = new HashMap<>();
for (String path: paths) {
ImageIcon icon = null;
try {
System.out.println(path);
icon = new ImageIcon(ImageIO.read(getClass().getResource("/octagonframe/" + path)));
} catch (IOException e) {
e.printStackTrace();
}
String prefix = path.split("\\.")[0];
map.put(prefix, icon);
}
return map;
}
private JTextField createTextField() {
final JTextField field = new JTextField(20);
field.setText("Type \"exit\" to terminate.");
field.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = field.getText();
if ("exit".equalsIgnoreCase(text)) {
System.exit(0);
}
}
});
return field;
}
private JLabel createOctagonLabel() {
Image image = null;
try {
image = ImageIO.read(getClass().getResource("/octagonframe/octagon.png"));
} catch (IOException ex) {
Logger.getLogger(DragDialog.class.getName()).log(Level.SEVERE,
null, ex);
}
JLabel label = new JLabel(new ImageIcon(image));
label.setLayout(new GridBagLayout());
label.add(createTextField());
label.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
DragDialog.this.setLocation(
DragDialog.this.getLocation().x + e.getX() - pointX,
DragDialog.this.getLocation().y + e.getY() - pointY);
}
});
label.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
pointX = e.getX();
pointY = e.getY();
}
});
return label;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new DragDialog();
}
});
}