2013-10-14 10:09:49Morris

[JAVA] 繪圖選單




繪圖選單可能有數個欄位,而所有欄位中,頂多有一個功能被選擇。
由於目前欄位只有一組,因此可能還看不太出來。

設計的思路為 :

MainFrame 下套用 MainPanel 整個,之所以多使用 JPanel class 是因為從旗下 add() 的 component 可以用 getParent() 得到, JFrame class 是沒有辦法調用到。

而 MainPanel 又分成數個區域,如圖所示,左區為工具列,右區為繪圖區 PaintPanel。

PaintPanel 利用 getParent() 呼叫 MainPanel 的 function 得到左區工具列哪一個功能被選擇。

到時候有多個工具列時,才方便修改。

由於選單至多一個被選擇,採用 JList 形式去修改,要將選擇事件稍微修改一下達到美觀。
從讀圖檔的方式改變,以及選擇框架的取消。當然也可以使用 JMenuItem 套用 ButtonGroup 之類的方式。不敢怎樣,到時候去修改 function 即可。

需要整包 project 留言。

package package1;

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

public class MainFrame extends JFrame {
    private static int WIDTH = 1000, HEIGHT = 500;
    private DefaultListModel listModel;
    private JList sampleJList;

    MainFrame() {
        this.setTitle("ButtonGroupObject");
        this.setSize(WIDTH, HEIGHT);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.add(new MainPanel());
        this.setVisible(true);
    }

    public static void main(String[] args) {
        new MainFrame();
    }
}

package package1;

import java.awt.*;

import javax.swing.*;
public class MainPanel extends JPanel {
    private static int WIDTH = 1000, HEIGHT = 500;
    private DefaultListModel listModel;
    private JList sampleJList;
    MainPanel() {
        this.setLayout(new GridBagLayout());
        listModel = new DefaultListModel();
        listModel.addElement("Select");
        listModel.addElement("Class");
        listModel.addElement("Association");
        sampleJList = new JList(listModel);
        sampleJList.setCellRenderer(new MRenderer());
        sampleJList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        sampleJList.setSelectionBackground(null);
        sampleJList.setBackground(new Color(240, 240, 240));
        Font displayFont = new Font("Serif", Font.BOLD, 35);
        sampleJList.setFont(displayFont);
        sampleJList.setSelectedIndex(0);
        PaintPanel samplePanel = new PaintPanel();
        GridBagConstraints GBC = new GridBagConstraints();
        GBC.insets = new Insets(10, 10, 10, 10);
        GBC.fill = GridBagConstraints.HORIZONTAL;
        GBC.anchor = GridBagConstraints.NORTHEAST;
        GBC.gridx = 0;
        GBC.gridy = 0;
        GBC.gridwidth = 1;
        GBC.gridheight = 3;
        GBC.weightx = 30;
        GBC.weighty = 30;
        this.add(sampleJList, GBC);
        GBC = new GridBagConstraints();
        GBC.insets = new Insets(10, 10, 10, 10);
        GBC.fill = GridBagConstraints.BOTH;
        GBC.anchor = GridBagConstraints.NORTHEAST;
        GBC.gridx = 1;
        GBC.gridy = 1;
        GBC.gridwidth = 3;
        GBC.gridheight = 3;
        GBC.weightx = 300;
        GBC.weighty = 150;
        this.add(samplePanel, GBC);
    }
    public String getToolsSelected() {
        int idx = sampleJList.getSelectedIndex();
        return listModel.get(idx).toString();
    }
}

package package1;
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class MRenderer extends DefaultListCellRenderer {
    private Hashtable iconTable = new Hashtable();
    public Component getListCellRendererComponent(JList list,
                                                    Object value,
                                                    int index,
                                                    boolean isSelected,
                                                    boolean hasFocus) {
        JLabel label =
                  (JLabel)super.getListCellRendererComponent(list,
                                                             value.toString(),// display non-text.
                                                             index,
                                                             isSelected,
                                                             false);
        ImageIcon icon = (ImageIcon)iconTable.get(value.toString()+isSelected);
        if(icon == null) {
            String fileName = value.toString();
            if(isSelected == true)
                fileName += "1.png";
            else
                fileName += "0.png";
            icon = new ImageIcon(fileName);
            iconTable.put(value.toString()+isSelected, icon);
        }
        label.setIcon(icon);
        return label;
    }
}

package package1;

import java.awt.*;

import javax.swing.*;

public class PaintPanel extends JPanel {
    PaintPanel() {
        this.setBackground(new Color(40, 40, 40));
        ShowListener listener = new ShowListener();
        this.addMouseListener(listener);
        this.addMouseMotionListener(listener);
    }
    public String getToolsSelected() {
        Component j1 = this.getParent();
        if(j1 instanceof MainPanel)
            return ((MainPanel) j1).getToolsSelected();
        else
            return null;
    }
}

package package1;

import java.awt.Component;
import java.awt.Point;
import java.awt.event.MouseEvent;

import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.event.MouseInputListener;

public class ShowListener implements MouseInputListener{
    public void mouseMoved(MouseEvent e) {
    }

    public void mouseDragged(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
        Component j1 = (Component) e.getSource();//jl = PaintPanel
        JOptionPane.showMessageDialog(j1, ((PaintPanel)j1).getToolsSelected());
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
    }
}