gpt4 book ai didi

java - PaintComponents() 从未被调用

转载 作者:行者123 更新时间:2023-12-02 06:25:53 27 4
gpt4 key购买 nike

这应该是一个抽象类,可以扩展以绘制连接 JPanel 上某些坐标点的线。

但是,永远不会调用专门的 PaintComponents() 方法。我已经看到其他答案并尝试使用他们的解决方案,例如检查我是否添加了 JPanel 或者是否有布局。然而,PaintComponent() 仍然没有被调用,屏幕上也没有显示任何内容。

import java.awt.*;
import java.util.*;
import javax.swing.*;

public abstract class XYGrapher
{
private ArrayList<Coordinate> coordinates;
int xStart;
int yStart;
int width;
int height;

public class GraphPanel extends JPanel
{
@Override
public void paintComponents(Graphics g)
{
super.paintComponents(g);
g.setColor(Color.green);
g.drawLine(xStart, yStart + height / 2, xStart + width, yStart + height / 2);
g.drawLine(xStart + width / 2, yStart, xStart + width / 2, yStart + height);

g.setColor(Color.black);
for(int i = 0; i < coordinates.size() - 1; i++)
{
Coordinate prev = coordinates.get(i);
Coordinate next = coordinates.get(i + 1);

if(prev.drawFrom() && next.drawTo())
{
g.drawLine((int) prev.getX(), (int) prev.getY(), (int) next.getX(), (int) next.getY());
}
}
}
}

public abstract Coordinate xyStart();

public abstract double xRange();

public abstract double yRange();

public abstract Coordinate getPoint(int pointNum);

public void drawGraph(int xPixelStart, int yPixelStart, int pixelsWide, int pixelsHigh)
{
int i = 0;

xStart = xPixelStart;
yStart = yPixelStart;
width = pixelsWide;
height = pixelsHigh;

double X0 = xyStart().getX();
double Y0 = xyStart().getX();

ArrayList<Coordinate> points = new ArrayList(0);
ArrayList<Coordinate> mapped = new ArrayList(0);

while(getPoint(i) != null)
{
points.add(getPoint(i));
i++;
}

for(int j = 0; j < points.size(); j++)
{
double X1 = points.get(j).getX();
double Y1 = points.get(j).getY();

boolean df = points.get(j).drawFrom();
boolean dt = points.get(j).drawTo();

int X = (int) (xPixelStart + (X1 - X0) * (pixelsWide / xRange()));
int Y = (int) (yPixelStart + (Y0 + yRange() -Y1) * (pixelsHigh / yRange()));
mapped.add(new Coordinate(X, Y, df, dt));
}

coordinates = mapped;

for(int k = 0; k < coordinates.size(); k++)
{
Coordinate test = coordinates.get(k);
Coordinate or = points.get(k);
System.out.println(test.getX() + " " + test.getY());
System.out.println(or.getX() + " " + or.getY());
}

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(0,1)

JPanel graph = new GraphPanel();
graph.setLayout(new GridLayout(0, 1));

frame.setContentPane(graph);
frame.pack();
frame.setVisible(true);
}
}

最佳答案

我认为“paintComponenents()”方法应该是“paintComponent()”。

关于java - PaintComponents() 从未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20535982/

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