如何避免 Java graphics2D 组件在最小化或弹出 Joptionpane 时擦除?
how to avoid Java graphics2D components to erase when minimizing or pooping up of Joptionpane?
我正在为我的拼贴画 java class 表示 GRAPH 数据结构,现在我在使用 graphics2D
组件绘制图形时遇到问题。
简单地说,当我按下添加顶点按钮时,我只是在一个 spacifiec 范围内随机调整图形的节点,所以我必须将我的绘图代码放在添加顶点按钮的操作上,这使我不使用 paintComponent()
或paint()
方法,所以当我最小化或只是弹出 Joptionepane 时,我绘制的整个图形都消失了!!
这是我的代码,注意:Graph 和 StackX 以及 Queue classes 实现是我在同一个包上实现的
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);
}
}
so when i minimize or just the Joptionepane popped up , the entire graphics that i had drawn gone !
不要使用 getGraphics() 进行自定义绘画。如您所见,这幅画是临时的。
so i have to put my drawing code on the action of add vertex button , that makes me not using paintComponent()
不,该操作应调用自定义绘画面板上的方法来添加顶点。因此,您的 class 需要保留要绘制的顶点列表,然后 paintComponent()
方法中的自定义代码将遍历列表并绘制所有顶点。
或者,另一种选择是在 BufferedImage 上绘制动作。然后,您可以在 paintComponent() 方法中绘制 BufferedImage(或者使用 ImageIcon 将 BufferedImage 添加到 JLabel)。
有关使用 List 或 BufferedImage 进行自定义绘画的工作示例,请参阅 Custom Painting Approaches。
尝试删除该行
panel_1.setLayout(空);
告诉我你会得到什么
解决与否
我正在为我的拼贴画 java class 表示 GRAPH 数据结构,现在我在使用 graphics2D
组件绘制图形时遇到问题。
简单地说,当我按下添加顶点按钮时,我只是在一个 spacifiec 范围内随机调整图形的节点,所以我必须将我的绘图代码放在添加顶点按钮的操作上,这使我不使用 paintComponent()
或paint()
方法,所以当我最小化或只是弹出 Joptionepane 时,我绘制的整个图形都消失了!!
这是我的代码,注意:Graph 和 StackX 以及 Queue classes 实现是我在同一个包上实现的
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);
}
}
so when i minimize or just the Joptionepane popped up , the entire graphics that i had drawn gone !
不要使用 getGraphics() 进行自定义绘画。如您所见,这幅画是临时的。
so i have to put my drawing code on the action of add vertex button , that makes me not using paintComponent()
不,该操作应调用自定义绘画面板上的方法来添加顶点。因此,您的 class 需要保留要绘制的顶点列表,然后 paintComponent()
方法中的自定义代码将遍历列表并绘制所有顶点。
或者,另一种选择是在 BufferedImage 上绘制动作。然后,您可以在 paintComponent() 方法中绘制 BufferedImage(或者使用 ImageIcon 将 BufferedImage 添加到 JLabel)。
有关使用 List 或 BufferedImage 进行自定义绘画的工作示例,请参阅 Custom Painting Approaches。
尝试删除该行 panel_1.setLayout(空); 告诉我你会得到什么 解决与否