gpt4 book ai didi

java - 在面板上绘图,但将其添加到另一个框架时,没有任何绘制

转载 作者:行者123 更新时间:2023-11-30 08:08:53 25 4
gpt4 key购买 nike

我正在开发一个项目,我工作的总体思路是在一个独立的项目中完成它的每个部分,然后在完成后通过库将其添加到主项目中。

我的问题是 - 我所做的一部分是在面板上进行绘画。当我添加一个将其连接到主项目的图层 Pane 时,实际上没有发生任何绘图。

这是我的项目示例代码:

在代码示例 1 中,有 JLayeredPane,其中包含我用于执行绘图的面板。

在代码示例 2 中,有一个按钮。它的actionPerformed 是将JLayeredPane 添加到面板中。但问题是添加JLayeredPane后绘图没有出现。

代码示例 1:

public class GraphGui extends javax.swing.JFrame {

/**
* Creates new form GraphGui
*/
adjacencyMatrix m = new adjacencyMatrix();
Dfs df = new Dfs();
int[] x = new int[df.MAX_VERTS];
int[] y = new int[df.MAX_VERTS];
public Graphics2D d;
public Graphics2D doo;
int i = 0;


public GraphGui() {
setlookAndFeel();
initComponents();
setLocationRelativeTo(null);
DFS.setVisible(true);
adjMatrics.setVisible(false);

//display is the panel that draw over
d = (Graphics2D) display.getGraphics();
doo = (Graphics2D) jPanel2.getGraphics();


}

//crating an initialization of components are done automatically

public void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

df.dfs();
doo.setFont(new Font("TimesRoman", Font.BOLD, 19));
doo.drawString("visits: " + df.out, 5, 20);
df.out = "";
}

public void AddE1ActionPerformed(java.awt.event.ActionEvent evt) {
String j = start1.getText();
int k = Integer.parseInt(j) - 1;
String c = end1.getText();
int v = Integer.parseInt(c) - 1;

df.addEdge(k, v);
d.setFont(new Font("TimesRoman", Font.BOLD, 17));
d.drawLine(x[k] + 30, y[k] + 20, x[v], y[v] + 19);
}

public void AddV1ActionPerformed(java.awt.event.ActionEvent evt) {
String f = ver1.getText();
String toUpperCase = f.toUpperCase();
char r = toUpperCase.charAt(0);
df.addVertex(r);
int radius = 30;
int R = (int) (Math.random() * 256);
int G = (int) (Math.random() * 256);
int B = (int) (Math.random() * 256);
x[i] = R % 320;
y[i] = B % 167;

d.setColor(new Color(R, G, B));
d.setFont(new Font("TimesRoman", Font.BOLD, 15));
d.drawOval(x[i], y[i], radius, radius);
d.fillOval(x[i], y[i], radius, radius);
d.setColor(Color.BLACK);
d.drawString(r + "", x[i] + 10, y[i] + 20);
d.drawOval(0, 0, radius, radius);
i++;
}

此链接显示了代码示例 1 应该执行的操作:

https://pbs.twimg.com/media/CG8INXZXAAEqthh.png:large

代码示例 2

{
private void graphBTActionPerformed(java.awt.event.ActionEvent evt) {
GraphGui gr=new GraphGui();

jPanel2.removeAll();

jPanel2.add(gr.DFS);

MainLayer.setVisible(false);
Displaylayer.setVisible(true);
}

}

下面的链接是我添加面板后得到的 -什么也画不出来。

https://pbs.twimg.com/media/CG8IR7rWoAA3qKG.png:large

最佳答案

这里有 2 个单独的问题。

(1) 最简单的答案是 - 您需要从操作方法中调用“getGraphics”。不是来自构造函数。例如

public void jButton1ActionPerformed(java.awt.event.ActionEvent evt)  {                                         
Graphics2D doo = (Graphics2D) jPanel2.getGraphics();
...
doo.setFont(...);
doo.drawString(...);
}

(2) 这将产生可见的绘图,但是每当 java 决定重新绘制时它们就会消失 - 例如如果你最小化框架。这可以通过备注中提到的paintComponent()来解决。基本思想是你的组件(例如jPanel2)将保存它需要绘制的所有内容的数据结构 - 字符串、边、顶点等。在paintComponent中你可以绘制它们。在 actionPerformed() 中,您更改数据结构并调用“重绘”。这种方法的草图:

class MyPanel extends JPanel{
private String text;
private Point[] vertextes;
public void addVertext(..)
public void paintComponent(Graphics g){
... use g to drawString, drawOval... according to 'text' and 'vertexes'
}
}
// Then in your JFrame:
private MyPanel p;
...
actionPerfomred(...){
p.addVertext(..)
p.repaint();
}

关于java - 在面板上绘图,但将其添加到另一个框架时,没有任何绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30700006/

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