用户界面 - 如何避免Java graphics2D组件在最小化或清理Joptionpane时被擦除



>我正在研究一个表示我的拼贴的图形数据结构的java类,现在当我使用graphics2D组件绘制图形时遇到问题。简单地说,我在空间范围内随机地喘着图的节点,就在我按下添加顶点按钮时,所以我必须将我的绘图代码放在添加顶点按钮的操作上,这使我不使用paintComponent()paint()方法,所以当我最小化或只是弹出Joptionepane时, 我绘制的整个图形消失了!

这是我的代码,注意:图形和 StackX 以及队列类的实现是由我在同一包上完成的

package Graphs;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.border.TitledBorder;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JScrollPane;
import DataStrucutres.Trees.BTNode;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Random;
 public class GraphGUI extends JPanel {
   private JPanel panel;
private JTextField vetxTxt;
private JButton vertxBtn;
private JTextField ed1Txt;
private JTextField ed2Txt;
private JButton edBtn;
private JPanel panel_1;
private JButton creatBtn;
private JScrollPane scrollPane;
static int j = 1;
static Graph myGraph;
private JButton btnDfs;
private JButton btnBfs;
boolean check;
int[] X = new int[10];
int[] Y = new int[10];
int w, h;
Graphics2D g;
/**
 * Create the panel.
 * 
 */
public GraphGUI() {
    setBackground(Color.DARK_GRAY);
    setLayout(null);

    panel_1 = new JPanel();
    panel_1.setBorder(new TitledBorder(null, "Controls",
            TitledBorder.LEADING, TitledBorder.TOP, null, new Color(48, 48,
                    48)));
    panel_1.setBounds(12, 117, 248, 188);
    add(panel_1);
    panel_1.setLayout(null);
    vetxTxt = new JTextField();
    vetxTxt.setEnabled(false);
    vetxTxt.setBounds(7, 23, 124, 31);
    panel_1.add(vetxTxt);
    vetxTxt.setColumns(10);
    vertxBtn = new JButton("Add Vertex");
    vertxBtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            try {
                check = true;
                String string = vetxTxt.getText().toUpperCase();
                char r = string.charAt(0);
                myGraph.addVertex(r);
                vetxTxt.setText(null);
                int x = (int) ((Math.random() * (700))  % 349 )+300;
                int y = (int) ((Math.random() * (780))  % 349);
                int width = (int) (Math.random() * (700 / 4));
                int height = (int) (Math.random() * (780 / 4));
                if (x + width > getWidth()) {
                    x = getWidth() - width;
                }
                if (y + height > getHeight()) {
                    y = getHeight() - height;
                }
                Color color = new Color((int) (Math.random() * 255),
                        (int) (Math.random() * 255),
                        (int) (Math.random() * 255));
                Graphics2D g = (Graphics2D) getGraphics();
                g.setColor(color);
                g.drawOval(x, y, 30, 30);
                g.drawString(r + "", x + 10, y + 18);
                X[h] = x;
                Y[w] = y;
                h++;
                w++;
                j++;
            } catch (ArrayIndexOutOfBoundsException e) {
                JOptionPane.showMessageDialog(null,
                        "Graph is full of vertexes", "ERORR",
                        JOptionPane.ERROR_MESSAGE);
                vetxTxt.setText(null);
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, "Enter data pleas!",
                        "ERORR", JOptionPane.ERROR_MESSAGE);
                vetxTxt.setText(null);
            }
        }
    });
    vertxBtn.setEnabled(false);
    vertxBtn.setBounds(141, 24, 100, 29);
    panel_1.add(vertxBtn);
    ed1Txt = new JTextField();
    ed1Txt.setEnabled(false);
    ed1Txt.setBounds(7, 84, 50, 31);
    panel_1.add(ed1Txt);
    ed1Txt.setColumns(10);
    ed2Txt = new JTextField();
    ed2Txt.setEnabled(false);
    ed2Txt.setBounds(81, 84, 50, 31);
    panel_1.add(ed2Txt);
    ed2Txt.setColumns(10);
    edBtn = new JButton("Add Edge");
    edBtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            try {
                int m = Integer.parseInt(ed1Txt.getText());
                int v = Integer.parseInt(ed2Txt.getText());
                ed1Txt.setText(null);
                ed2Txt.setText(null);
                myGraph.addEdge(m, v);
                g.drawLine(X[m] + 8, Y[m] + 8, X[v] + 8, Y[v] + 8);
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null,
                        "Entered edges is out of graph !", "ERORR",
                        JOptionPane.ERROR_MESSAGE);
            }
        }
    });
    edBtn.setEnabled(false);
    edBtn.setBounds(141, 85, 100, 29);
    panel_1.add(edBtn);
    btnBfs = new JButton("BFS");
    btnBfs.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                myGraph.bfs();
                JOptionPane.showMessageDialog(null,
                "BFS is : " + myGraph.getS());
            } catch (Exception e2) {
                JOptionPane.showMessageDialog(null,
                        "Graph is empty of vertexes", "ERORR",
                        JOptionPane.ERROR_MESSAGE);
                vetxTxt.setText(null);
            }
        }
    });
    btnBfs.setEnabled(false);
    btnBfs.setBounds(27, 139, 93, 29);
    panel_1.add(btnBfs);
    btnDfs = new JButton("DFS");
    btnDfs.setEnabled(false);
    btnDfs.setBounds(129, 139, 93, 29);
    panel_1.add(btnDfs);
    btnDfs.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                myGraph.dfs();
                Graphics2D g = (Graphics2D) getGraphics();
                JOptionPane.showMessageDialog(null,
                "DFS is : " + myGraph.getS());
            } catch (Exception e2) {
                JOptionPane.showMessageDialog(null,
                        "Graph is empty of vertexes", "ERORR",
                        JOptionPane.ERROR_MESSAGE);
                vetxTxt.setText(null);
            }
        }
    });
    creatBtn = new JButton("Creat Graph");
    creatBtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            g = (Graphics2D) getGraphics();
            myGraph = new Graph();
            vertxBtn.setEnabled(true);
            vetxTxt.setEnabled(true);
            ed1Txt.setEnabled(true);
            ed2Txt.setEnabled(true);
            edBtn.setEnabled(true);
            btnBfs.setEnabled(true);
            btnDfs.setEnabled(true);
            creatBtn.setVisible(false);
            JOptionPane
                    .showMessageDialog(
                            null,
                            "A graph is a structure consisting of a set of arrays (also called dimensions) and a set of edges . An edge is a pair of vertices . The two vertices are called the edge endpoints.",
                            "Graph", JOptionPane.PLAIN_MESSAGE);
        }
    });
    creatBtn.setBounds(6, 395, 125, 21);
    add(creatBtn);
}

}

因此,当我最小化或只是弹出Joptionepane时,我绘制的整个图形都消失了!

不要使用 getGraphics() 来做自定义绘画。正如你所注意到的,这幅画是暂时的。

所以我必须将我的绘图代码放在添加顶点按钮的操作上,这使我不使用 paintComponent()

否,该操作应调用自定义绘制面板上的方法来添加顶点。因此,您的类需要保留要绘制的顶点列表,然后 paintComponent() 方法中的自定义代码将遍历 List 并绘制所有顶点。

或者,另一种选择是在缓冲图像上绘制动作。然后,您可以在paintComponent()方法中绘制BufferedImage(或者使用ImageIcon将BufferedImage添加到JLabel)。

有关使用 List 或 BufferedImage 执行自定义绘制的工作示例,请参阅自定义绘制方法。

尝试删除该行panel_1.setLayout(空);告诉我你会得到什么解决与否

相关内容

  • 没有找到相关文章