gpt4 book ai didi

java - 如何避免 Java Graphics2D 组件在最小化或弹出 Joptionpane 时被删除?

转载 作者:行者123 更新时间:2023-12-01 11:25:39 24 4
gpt4 key购买 nike

我正在开发一个代表我的拼贴的 GRAPH 数据结构的 Java 类,现在当我使用 graphics2D 组件绘制图形时遇到问题。只是当我按下“添加顶点”按钮时,我在一个空间范围内随机地绘制图形的节点,所以我必须将绘图代码放在“添加顶点”按钮的操作上,这使我不使用 paintComponent() paint() 方法,因此当我最小化或仅弹出 Joptionepane 时,我绘制的整个图形都会消失!

这是我的代码,注意:Graph、StackX 和 Queue 类实现是我在同一个包上实现的

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()

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

或者,另一个选项是在 BufferedImage 上绘制 Action 。然后,您可以在paintComponent() 方法中绘制BufferedImage(或者仅使用ImageIcon 将BufferedImage 添加到JLabel)。

参见Custom Painting Approaches有关使用 List 或 BufferedImage 进行自定义绘画的工作示例。

关于java - 如何避免 Java Graphics2D 组件在最小化或弹出 Joptionpane 时被删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30823572/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com