gpt4 book ai didi

java - 如何在下次绘图之前清除 jPanel

转载 作者:行者123 更新时间:2023-12-02 10:56:11 30 4
gpt4 key购买 nike

我在网上搜索过,据说有很多方法可以在开始新绘图之前清除 jPanel,但是,在尝试了所有方法之后,我仍然没有成功 - 结果是面板根本不清除,然后下一个绘图只是与初始绘图重叠,否则我的 paintComponent() 根本不会执行。

Image of problem (Overlapping)

从 GUI 执行绘图的代码:

private void BTCGraphActionPerformed(java.awt.event.ActionEvent evt) {                                         

Graphics2D gfx = (Graphics2D) jPanel2.getGraphics();
WebScraper WSBTC = new WebScraper();
float[] HBTC = WSBTC.HScrapeBTC();

Graph graphExecute = new Graph();

graphExecute.CurrentValues(HBTC);
graphExecute.IndexLarge(HBTC);
graphExecute.IndexSmall(HBTC);
graphExecute.Plotter(HBTC);
graphExecute.paintComponent(gfx);
}

private void LTCGraphActionPerformed(java.awt.event.ActionEvent evt) {

Graphics2D gfx = (Graphics2D) jPanel2.getGraphics();
WebScraper WSLTC = new WebScraper();
float[] HLTC = WSLTC.HScrapeLTC();
Graph graphExecute = new Graph();

graphExecute.CurrentValues(HLTC);
graphExecute.IndexLarge(HLTC);
graphExecute.IndexSmall(HLTC);
graphExecute.Plotter(HLTC);
graphExecute.paintComponent(gfx);
}

private void ETHGraphActionPerformed(java.awt.event.ActionEvent evt) {

Graphics2D gfx = (Graphics2D) jPanel2.getGraphics();
WebScraper WSETH = new WebScraper();
float[] HETH = WSETH.HScrapeETH();
Graph graphExecute = new Graph();

graphExecute.CurrentValues(HETH);
graphExecute.IndexLarge(HETH);
graphExecute.IndexSmall(HETH);
graphExecute.Plotter(HETH);
graphExecute.paintComponent(gfx);
}

paintComponent() 的代码:

protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
super.paintComponent(g2);
int DotSize = 10;

int width = g2.getFontMetrics().stringWidth("Today");
int middle = width / 2;

g2.setColor(Color.BLACK);
/* g2.drawLine(10, 10, 10, 410); //Frame Boundaries
g2.drawLine(410, 10, 10, 10); //Frame Boundaries
g2.drawLine(410, 10, 410, 410); //Frame Boundaries
g2.drawLine(410, 410, 10, 410); //Frame Boundaries */

//Axis
g2.drawLine(30, 30, 30, 370);
g2.drawLine(370, 370, 30, 370);

//Points & Connections
PlotPoints(g2, 98, DistanceDay1, DotSize);
g2.drawLine(98, DistanceDay1, 166, DistanceDay2);
PlotPoints(g2, 166, DistanceDay2, DotSize);
g2.drawLine(166, DistanceDay2, 234, DistanceDay3);
PlotPoints(g2, 234, DistanceDay3, DotSize);
g2.drawLine(234, DistanceDay3, 302, DistanceDay4);
PlotPoints(g2, 302, DistanceDay4, DotSize);
g2.drawLine(302, DistanceDay4, 370, DistanceDay5);
PlotPoints(g2, 370, DistanceDay5, DotSize);

//Labels
g2.drawString("Today", 370 - middle, 390);
/* g2.drawString("Test", 98 - middle, 40);
g2.drawString("Test", 146, 25); */

}

我应该调用什么方法以及我必须将其放置在哪里,以便每次按下按钮绘制新图形时,它都会在绘制之前清除面板

最佳答案

I've searched up online and there are supposedly many ways to clear a jPanel before starting a new drawing however

是的,不要做你正在做的事情。永远不需要手动调用 paintComponent ,并且永远不应该调用 getGraphics ,除了能够返回 null 之外,这不是自定义的方式应该进行绘画。

绘制应该只绘制组件的当前状态。因此,如果您想“清除”面板,请清除它用来决定绘制内容和绘制方式的状态。

您可以通过在要更新的组件上调用 repaint 来触发执行新绘制 channel 的请求。作为绘制过程的一部分,组件的Graphics上下文将被“准备”,通常是通过在其上绘制组件的背景颜色。

首先查看 Performing Custom PaintingPainting in AWT and Swing

已更新...

一个可运行的示例将使这变得更容易。让我们从...开始...

private void LTCGraphActionPerformed(java.awt.event.ActionEvent evt) {                                         

Graphics2D gfx = (Graphics2D) jPanel2.getGraphics();
WebScraper WSLTC = new WebScraper();
float[] HLTC = WSLTC.HScrapeLTC();
Graph graphExecute = new Graph();

graphExecute.CurrentValues(HLTC);
graphExecute.IndexLarge(HLTC);
graphExecute.IndexSmall(HLTC);
graphExecute.Plotter(HLTC);
graphExecute.paintComponent(gfx);
}

Graphics2D gfx = (Graphics2D) jPanel2.getGraphics(); 是一个坏主意。 getGraphics 可以返回 null 并且仅提供组件的快照。在下一个绘制周期,它将被更新。

graphExecute.paintComponent(gfx); 是一个坏主意,没有任何理由你应该直接调用任何 paint 方法。绘制系统会告诉您何时需要绘制组件以及要使用的上下文。

然后...

Graph graphExecute = new Graph();

graphExecute.CurrentValues(HLTC);
graphExecute.IndexLarge(HLTC);
graphExecute.IndexSmall(HLTC);
graphExecute.Plotter(HLTC);
graphExecute.paintComponent(gfx);

...等等,什么!?您创建,我假设是一个组件Graph,您为其添加一些信息,然后手动绘制它......

好吧,假设它是某种 JComponent,为什么不将其添加到当前 UI

jPanel2.removeAll();
jPanel2.add(graphExecute);
jPanel2.invalidate();
jPanel2.repaint();

如果Graph不是某种JComponent,那么我们就会遇到一个更大的问题,因为您需要将它传递给可以绘制它的东西。 .也许类似...

public class RenderPane extends JPanel {

private Graph graph;

public RenderPane() {
}

public void setGraph(Graph graph) {
this.graph = graph;
repaint();
}

public Graph getGraph() {
return graph;
}

@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}

protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graph grap = getGraph();
if (grap != null) {
grap.paintComponent(g);
}
}

}

那么你也许可以做一些类似的事情......

private void LTCGraphActionPerformed(java.awt.event.ActionEvent evt) {                                         

jPanel2.removeAll();
WebScraper WSLTC = new WebScraper();
float[] HLTC = WSLTC.HScrapeLTC();
Graph graphExecute = new Graph();

graphExecute.CurrentValues(HLTC);
graphExecute.IndexLarge(HLTC);
graphExecute.IndexSmall(HLTC);
graphExecute.Plotter(HLTC);

RenderPane renderer = new RenderPane();
renderer.setGraph(graphExecute.Plotter);
jPanel.add(renderer);
}

关于java - 如何在下次绘图之前清除 jPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51719336/

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