Working dynamic GUI layout

This commit is contained in:
2025-01-05 19:06:41 +03:00
parent abbc64eead
commit a929f86461
4 changed files with 42 additions and 87 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 327 KiB

View File

@ -39,17 +39,10 @@ public abstract class Editable extends JPanel implements MouseListener, MouseMot
logger.setLevel(Level.ALL); logger.setLevel(Level.ALL);
} }
//Абстрактные методы
public abstract void drawing(Graphics2D g); public abstract void drawing(Graphics2D g);
public abstract void saveDataInEntity(); public abstract void saveDataInEntity();
//методы родительского класса Editable
//get,set для name
public String getName() { public String getName() {
return selectedEntityName; return selectedEntityName;
} }
@ -58,7 +51,6 @@ public abstract class Editable extends JPanel implements MouseListener, MouseMot
this.selectedEntityName = name; this.selectedEntityName = name;
} }
//заполнить текущую сущность по имени.
public void setEntityByName(String name) throws Exception { public void setEntityByName(String name) throws Exception {
entity = Project.getInstance().getEntityByName(name); entity = Project.getInstance().getEntityByName(name);
} }

View File

@ -1,15 +1,14 @@
package gui; package gui;
import java.awt.BorderLayout;
import java.awt.Component; import java.awt.Component;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Font; import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image; import java.awt.Image;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -36,23 +35,23 @@ import listeners.RemoveListElementEntityListener;
import model.Drawbox; import model.Drawbox;
import model.Entity; import model.Entity;
import model.Hitbox; import model.Hitbox;
import model.Point;
import repository.Project; import repository.Project;
public class ListGUI extends JPanel { public class ListGUI extends JPanel {
private Map<String, Icon> iconMap = new HashMap<>(); private Map<String, Icon> iconMap = new HashMap<>();
JButton addListElementEntity; JButton addListElementEntity;
JButton removeListElementEntity; JButton removeListElementEntity;
JButton addPicEntity;
JList list; JList list;
JScrollPane scroll; JScrollPane scroll;
ActionListener removeEntity; ActionListener removeEntity;
ActionListener addEntity; ActionListener addEntity;
DefaultListModel<String> testModel; DefaultListModel<String> testModel;
JPanel gridButtonBar = new JPanel(new GridLayout(1, 2, 5, 0));
private static Logger logger = Logger.getLogger("gui.ListGUI"); private static Logger logger = Logger.getLogger("gui.ListGUI");
public ListGUI() { public ListGUI() {
setLayout(new BorderLayout());
setPreferredSize(new Dimension(223,638));
try { try {
Project.getInstance().load(); Project.getInstance().load();
} catch (SAXException | IOException | ParserConfigurationException e) { } catch (SAXException | IOException | ParserConfigurationException e) {
@ -60,25 +59,22 @@ public class ListGUI extends JPanel {
// в идеале должны появляться разные окошки, которые обрисуют в чём проблема и предложения что делать // в идеале должны появляться разные окошки, которые обрисуют в чём проблема и предложения что делать
JOptionPane.showMessageDialog(this, "Parser exception, cause: "+e); JOptionPane.showMessageDialog(this, "Parser exception, cause: "+e);
} }
String[] nameList = createNameList(); String[] nameList = createNameList();
testModel = new DefaultListModel<>(); testModel = new DefaultListModel<>();
list = new JList(testModel); list = new JList(testModel);
list.setCellRenderer(new ListEntityRenderer()); list.setCellRenderer(new ListEntityRenderer());
addEntity = new CreateFrameAddElementListener(this); addEntity = new CreateFrameAddElementListener(this);
removeEntity = new RemoveListElementEntityListener(this); removeEntity = new RemoveListElementEntityListener(this);
scroll = new JScrollPane(list); scroll = new JScrollPane(list);
scroll.setSize(new Dimension(223, 638));
scroll.setLocation(5, 5); scroll.setLocation(5, 5);
this.add(scroll); add(scroll,BorderLayout.CENTER);
list.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION); list.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
addListElementEntity = createButton(5,645, addEntity,"res/addbutton.png"); addListElementEntity = createButton(addEntity,"res/addbutton.png");
removeListElementEntity = createButton(117,645,removeEntity,"res/deletebutton.png"); removeListElementEntity = createButton(removeEntity,"res/deletebutton.png");
add(gridButtonBar,BorderLayout.SOUTH);
/// ЧТОБЫ ОТКЛЮЧИТЬ ИЗБЫТОЧНЫЙ ВЫВОД В КОНСОЛЬ - НУЖНО СМЕНИТЬ УРОВЕНЬ ЛОГГИРОВАНИЯ В СТРОКЕ НИЖЕ /// ЧТОБЫ ОТКЛЮЧИТЬ ИЗБЫТОЧНЫЙ ВЫВОД В КОНСОЛЬ - НУЖНО СМЕНИТЬ УРОВЕНЬ ЛОГГИРОВАНИЯ В СТРОКЕ НИЖЕ
logger.setLevel(Level.FINEST); logger.setLevel(Level.FINEST);
@ -98,7 +94,6 @@ public class ListGUI extends JPanel {
Hitbox hitbox = new Hitbox(); Hitbox hitbox = new Hitbox();
Drawbox drawbox = new Drawbox(); Drawbox drawbox = new Drawbox();
// а тут уже создание новой сущности
Entity e = new Entity(name, hitbox, drawbox); Entity e = new Entity(name, hitbox, drawbox);
hitbox.setOwnerEntity(e); hitbox.setOwnerEntity(e);
e.setType(solid); e.setType(solid);
@ -187,14 +182,13 @@ public class ListGUI extends JPanel {
} }
} }
private JButton createButton(int width,int height,ActionListener listener,String pathImage) { private JButton createButton(ActionListener listener,String pathImage) {
JButton button = new JButton(new ImageIcon(pathImage)); JButton button = new JButton(new ImageIcon(pathImage));
button.setSize(110, 46); button.setPreferredSize(new Dimension(100, 50));
button.setLocation(width, height);
button.addActionListener(listener); button.addActionListener(listener);
button.setContentAreaFilled(false); button.setContentAreaFilled(false);
button.setFocusPainted(false); button.setFocusPainted(false);
this.add(button); gridButtonBar.add(button);
return button; return button;
} }

View File

@ -1,119 +1,88 @@
package gui; package gui;
import java.awt.BorderLayout;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Point; import java.awt.GridLayout;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.Box; import javax.swing.BorderFactory;
import javax.swing.ImageIcon; import javax.swing.ImageIcon;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JLabel; import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane; import javax.swing.JTabbedPane;
import listeners.OpenXMLFileButtonListener; import listeners.OpenXMLFileButtonListener;
import repository.Project; import repository.Project;
public class MainGUI extends JFrame{ public class MainGUI extends JFrame{
JPanel gridButtonBar = new JPanel(new GridLayout(1, 3, 5, 0));
JPanel TopButtonBar = new JPanel(new FlowLayout(FlowLayout.LEFT));
ListGUI list; ListGUI list;
ActionListener OpenXMLFileButtonListener; ActionListener OpenXMLFileButtonListener;
JButton openXMLJButton; JButton openXMLJButton;
JButton saveXMLJButton; JButton saveXMLJButton;
JButton clearLinesJButton; JButton clearLinesJButton;
public static JTabbedPane editorPane; public static JTabbedPane editorPane;
public static JTabbedPane hitdrawPane;
List <Editable> listEditorPanel = new ArrayList<Editable>();
DrawboxEditor drawBoxPanel; DrawboxEditor drawBoxPanel;
HitboxCircleEditor hitboxCirclePanel;
HitboxRectangleEditor hitboxRectanglePanel; HitboxRectangleEditor hitboxRectanglePanel;
HitboxPoligonEditor hitboxPoligonPanel;
public MainGUI() { public MainGUI() {
setTitle("Hitbox/Drawbox Editor"); setTitle("Hitbox/Drawbox Editor");
setLayout(null); setSize(1000,650);
setSize(1200,780);
setLocationRelativeTo(null);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setMinimumSize(new Dimension(800,600));
list = new ListGUI(); list = new ListGUI();
this.add(list); add(list,BorderLayout.WEST);
list.setLayout(null);
list.setSize(230, 691);
list.setLocation(0, 47);
list.setVisible(true); list.setVisible(true);
drawBoxPanel = new DrawboxEditor(list); drawBoxPanel = new DrawboxEditor(list);
hitboxCirclePanel = new HitboxCircleEditor(list);
hitboxRectanglePanel = new HitboxRectangleEditor(list); hitboxRectanglePanel = new HitboxRectangleEditor(list);
hitboxPoligonPanel = new HitboxPoligonEditor(list);
listEditorPanel.add(drawBoxPanel);
listEditorPanel.add(hitboxCirclePanel);
listEditorPanel.add(hitboxRectanglePanel);
listEditorPanel.add(hitboxPoligonPanel);
list.registerJListListener(drawBoxPanel); list.registerJListListener(drawBoxPanel);
list.registerJListListener(hitboxCirclePanel);
list.registerJListListener(hitboxPoligonPanel);
list.registerJListListener(hitboxRectanglePanel); list.registerJListListener(hitboxRectanglePanel);
OpenXMLFileButtonListener = new OpenXMLFileButtonListener(list); OpenXMLFileButtonListener = new OpenXMLFileButtonListener(list);
openXMLJButton = createButton("XML",5,5, OpenXMLFileButtonListener,"res/xml.png"); openXMLJButton = createButton("XML", OpenXMLFileButtonListener,"res/xml.png");
saveXMLJButton = createButton("Save",80,5,(e)-> Project.getInstance().writeXML(),"res/download.png"); saveXMLJButton = createButton("Save",(e)-> Project.getInstance().writeXML(),"res/download.png");
clearLinesJButton = createButton("Clear lines",155,5,null,"res/destroy.png"); clearLinesJButton = createButton("Clear lines",null,"res/destroy.png");
gridButtonBar.add(openXMLJButton);
gridButtonBar.add(saveXMLJButton);
//gridButtonBar.add(clearLinesJButton);
TopButtonBar.add(gridButtonBar);
TopButtonBar.setBorder(BorderFactory.createLoweredBevelBorder());
add(TopButtonBar,BorderLayout.NORTH);
clearLinesJButton.addActionListener(drawBoxPanel); clearLinesJButton.addActionListener(drawBoxPanel);
clearLinesJButton.addActionListener(hitboxCirclePanel);
clearLinesJButton.addActionListener(hitboxPoligonPanel);
clearLinesJButton.addActionListener(hitboxRectanglePanel); clearLinesJButton.addActionListener(hitboxRectanglePanel);
repaint();
this.add(editorPane = createPane(editorPane, 230, 50)); editorPane = new JTabbedPane();
editorPane.setVisible(true);
add(editorPane,BorderLayout.CENTER);
editorPane.addTab("Hitbox", hitdrawPane = createPane(hitdrawPane,10, 10)); editorPane.addTab("Hitbox", hitboxRectanglePanel);
editorPane.addTab("Drawbox", drawBoxPanel); editorPane.addTab("Drawbox", drawBoxPanel);
hitdrawPane.addTab(null, new ImageIcon("res/square.png"),hitboxRectanglePanel, null); repaint();
hitdrawPane.addTab(null, new ImageIcon("res/circle.png"),hitboxCirclePanel, null);
hitdrawPane.addTab(null, new ImageIcon("res/formless.png"),hitboxPoligonPanel, null);
hitdrawPane.setTabPlacement(JTabbedPane.LEFT);
//Оформление подписок к издателям.
drawBoxPanel.subscribe(Project.getInstance()); // подписка: Project получит данные ввиде обьекта Event, содержащий аднные drawbox при отрисовке последней точки из 4-х. drawBoxPanel.subscribe(Project.getInstance()); // подписка: Project получит данные ввиде обьекта Event, содержащий аднные drawbox при отрисовке последней точки из 4-х.
} }
@Override @Override
public void paint(Graphics g){ public void paint(Graphics g){
super.paint(g); super.paint(g);
g.drawLine(0, 72, 1200, 72);
}
public JTabbedPane createPane(JTabbedPane pane, int x, int y) {
pane = new JTabbedPane();
pane.setSize(new Dimension(950, 688));
pane.setLocation(new Point(x, y));
pane.setVisible(true);
return pane;
} }
private JButton createButton(String text,int width,int height,ActionListener listener,String pathImage) { private JButton createButton(String text,ActionListener listener,String pathImage) {
JButton button = new JButton(new ImageIcon(pathImage)); JButton button = new JButton(new ImageIcon(pathImage));
button.setSize(68, 34);
button.setLocation(width, height);
button.addActionListener(listener); button.addActionListener(listener);
button.setContentAreaFilled(false); button.setContentAreaFilled(false);
button.setFocusPainted(false); button.setFocusPainted(false);
this.add(button); button.setPreferredSize(new Dimension(72, 38));
return button; return button;
} }
} }