PaintComponent 方法未在面板上显示任何内容
我已经尝试调试这个几个小时了。该程序应该是一个绘制坐标图的绘图器,但我无法显示任何内容,甚至是随机行,但如果我在那里放置一条打印语句,它就可以工作。这是paintComponent方法的问题。当我在 g.drawLine 之前输出 print 语句时,它会打印,但即使我放置坐标为 (1,3)、(2,4) 的随机线,它也不会绘制任何线条。
import java.awt.*;
import java.util.*;
import javax.swing.*;
public abstract class XYGrapher
{
abstract public Coordinate xyStart();
abstract public double xRange();
abstract public double yRange();
abstract public Coordinate getPoint(int pointNum);
public class Paint extends JPanel
{
public void paintGraph(Graphics g, int xPixel1, int yPixel1, int xPixel2, int yPixel2)
{
super.paintComponent(g);
g.setColor(Color.black);
g.drawLine(xPixel1, yPixel1, xPixel2, yPixel2);
}
public void paintXAxis(Graphics g, int xPixel, int pixelsWide, int pixelsHigh)
{
super.paintComponent(g);
g.setColor(Color.green);
g.drawLine(xPixel, 0, xPixel, pixelsHigh);
}
public void paintYAxis(Graphics g, int yPixel, int pixelsWide, int pixelsHigh)
{
super.paintComponent(g);
g.setColor(Color.green);
g.drawLine(0, yPixel, pixelsWide, yPixel);
}
}
public void drawGraph(int xPixelStart, int yPixelStart, int pixelsWide, int pixelsHigh)
{
JFrame frame = new JFrame();
Paint panel = new Paint();
panel.setPreferredSize(new Dimension(pixelsWide, pixelsHigh));
panel.setMinimumSize(new Dimension(pixelsWide, pixelsHigh));
panel.setMaximumSize(new Dimension(pixelsWide, pixelsHigh));
frame.setLocation(frame.getToolkit().getScreenSize().width / 2 - pixelsWide / 2, frame.getToolkit().getScreenSize().height / 2 - pixelsHigh / 2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(panel);
frame.pack();
frame.setVisible(true);
double xRange = xRange();
double yRange = yRange();
Coordinate xyStart = xyStart();
int xPixel = xPixelStart - (int) (xyStart.getX() * (pixelsWide / xRange));
int yPixel = yPixelStart + (int) ((xyStart.getY() + yRange) * (pixelsHigh / yRange));
System.out.println(xPixel + " " + yPixel);
if(yPixel > 0 && (yPixel < pixelsHigh))
{
System.out.println("y");
panel.paintYAxis(panel.getGraphics(), yPixel, pixelsWide, pixelsHigh);
}
if(xPixel > 0 && (xPixel < pixelsHigh))
{
System.out.println("x");
panel.paintXAxis(panel.getGraphics(), xPixel, pixelsWide, pixelsHigh);
}
for(int i = 0; i>=0; i++)
{
Coordinate point1 = getPoint(i);
Coordinate point2 = getPoint(i+1);
if(point2 == null)
{
break;
}
else
{
if(point1.drawFrom() && point2.drawTo())
{
int xPixel1 = (int) (xPixelStart + (point1.getX() - xyStart.getX()) * (pixelsWide / xRange));
int yPixel1 = (int) (yPixelStart + (xyStart.getY() + yRange-point1.getY()) * (pixelsHigh / yRange));
int xPixel2 = (int) (xPixelStart + (point2.getX() - xyStart.getX()) * (pixelsWide / xRange));
int yPixel2 = (int) (yPixelStart + (xyStart.getY() + yRange - point2.getY()) * (pixelsHigh / yRange));
panel.paintGraph(panel.getGraphics(), xPixel1, yPixel1, xPixel2, yPixel2);
}
}
}
frame.pack();
}
}
这就是我测试的方式,它应该是一个正方形,但什么也没有显示。
public class GrapherTester extends XYGrapher
{
public Coordinate xyStart()
{
return new Coordinate(-2,2);
}
public double xRange()
{
return 4;
}
public double yRange()
{
return 4;
}
public Coordinate getPoint(int pointNum)
{
switch(pointNum)
{
case 0: return new Coordinate(-1,-1);
case 1: return new Coordinate(1,-1);
case 2: return new Coordinate(1,1);
case 3: return new Coordinate(-1,1);
case 4: return new Coordinate(-1,-1);
}
return null;
}
public static void main(String[] args)
{
new GrapherTester().drawGraph(100, 100, 500, 500);
}
}
协调课程,以便如果你们中有人想跑步并尝试一下。这就是您所需要的。
public class Coordinate
{
float x;
float y;
boolean drawTo;
boolean drawFrom;
Coordinate(double x, double y)
{
this.x = (float) x;
this.y = (float) y;
drawFrom = true;
drawTo = true;
}
Coordinate(double x, double y, boolean drawFrom, boolean drawTo)
{
this.x = (float) x;
this.y = (float) y;
this.drawFrom = drawFrom;
this.drawTo = drawTo;
}
public double getX()
{
return x;
}
public double getY()
{
return y;
}
public boolean drawTo()
{
return drawTo;
}
public boolean drawFrom()
{
return drawFrom;
}
}
I have been trying to debug this for hours. The program is supposed to be a grapher that graphs coordinates, but i cannot get anything to display not even a random line, but if i put a print statement there it works. It is a problem with the paintComponent Method. When I out print statement before g.drawLine then it prints, but it doesn't draw any lines even if i put a random line with coordinates (1,3), (2,4).
import java.awt.*;
import java.util.*;
import javax.swing.*;
public abstract class XYGrapher
{
abstract public Coordinate xyStart();
abstract public double xRange();
abstract public double yRange();
abstract public Coordinate getPoint(int pointNum);
public class Paint extends JPanel
{
public void paintGraph(Graphics g, int xPixel1, int yPixel1, int xPixel2, int yPixel2)
{
super.paintComponent(g);
g.setColor(Color.black);
g.drawLine(xPixel1, yPixel1, xPixel2, yPixel2);
}
public void paintXAxis(Graphics g, int xPixel, int pixelsWide, int pixelsHigh)
{
super.paintComponent(g);
g.setColor(Color.green);
g.drawLine(xPixel, 0, xPixel, pixelsHigh);
}
public void paintYAxis(Graphics g, int yPixel, int pixelsWide, int pixelsHigh)
{
super.paintComponent(g);
g.setColor(Color.green);
g.drawLine(0, yPixel, pixelsWide, yPixel);
}
}
public void drawGraph(int xPixelStart, int yPixelStart, int pixelsWide, int pixelsHigh)
{
JFrame frame = new JFrame();
Paint panel = new Paint();
panel.setPreferredSize(new Dimension(pixelsWide, pixelsHigh));
panel.setMinimumSize(new Dimension(pixelsWide, pixelsHigh));
panel.setMaximumSize(new Dimension(pixelsWide, pixelsHigh));
frame.setLocation(frame.getToolkit().getScreenSize().width / 2 - pixelsWide / 2, frame.getToolkit().getScreenSize().height / 2 - pixelsHigh / 2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(panel);
frame.pack();
frame.setVisible(true);
double xRange = xRange();
double yRange = yRange();
Coordinate xyStart = xyStart();
int xPixel = xPixelStart - (int) (xyStart.getX() * (pixelsWide / xRange));
int yPixel = yPixelStart + (int) ((xyStart.getY() + yRange) * (pixelsHigh / yRange));
System.out.println(xPixel + " " + yPixel);
if(yPixel > 0 && (yPixel < pixelsHigh))
{
System.out.println("y");
panel.paintYAxis(panel.getGraphics(), yPixel, pixelsWide, pixelsHigh);
}
if(xPixel > 0 && (xPixel < pixelsHigh))
{
System.out.println("x");
panel.paintXAxis(panel.getGraphics(), xPixel, pixelsWide, pixelsHigh);
}
for(int i = 0; i>=0; i++)
{
Coordinate point1 = getPoint(i);
Coordinate point2 = getPoint(i+1);
if(point2 == null)
{
break;
}
else
{
if(point1.drawFrom() && point2.drawTo())
{
int xPixel1 = (int) (xPixelStart + (point1.getX() - xyStart.getX()) * (pixelsWide / xRange));
int yPixel1 = (int) (yPixelStart + (xyStart.getY() + yRange-point1.getY()) * (pixelsHigh / yRange));
int xPixel2 = (int) (xPixelStart + (point2.getX() - xyStart.getX()) * (pixelsWide / xRange));
int yPixel2 = (int) (yPixelStart + (xyStart.getY() + yRange - point2.getY()) * (pixelsHigh / yRange));
panel.paintGraph(panel.getGraphics(), xPixel1, yPixel1, xPixel2, yPixel2);
}
}
}
frame.pack();
}
}
This is how i am testing it is supposed to be a square, but nothing shows up.
public class GrapherTester extends XYGrapher
{
public Coordinate xyStart()
{
return new Coordinate(-2,2);
}
public double xRange()
{
return 4;
}
public double yRange()
{
return 4;
}
public Coordinate getPoint(int pointNum)
{
switch(pointNum)
{
case 0: return new Coordinate(-1,-1);
case 1: return new Coordinate(1,-1);
case 2: return new Coordinate(1,1);
case 3: return new Coordinate(-1,1);
case 4: return new Coordinate(-1,-1);
}
return null;
}
public static void main(String[] args)
{
new GrapherTester().drawGraph(100, 100, 500, 500);
}
}
Coordinate class so if any of you want to run and try it out. That is all you would need.
public class Coordinate
{
float x;
float y;
boolean drawTo;
boolean drawFrom;
Coordinate(double x, double y)
{
this.x = (float) x;
this.y = (float) y;
drawFrom = true;
drawTo = true;
}
Coordinate(double x, double y, boolean drawFrom, boolean drawTo)
{
this.x = (float) x;
this.y = (float) y;
this.drawFrom = drawFrom;
this.drawTo = drawTo;
}
public double getX()
{
return x;
}
public double getY()
{
return y;
}
public boolean drawTo()
{
return drawTo;
}
public boolean drawFrom()
{
return drawFrom;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
方法paintGraph(...)、paintXAxis(...) 和paintYAxis(...) 不应调用paintComponent()。
相反,代码应该是相反的。也就是说,您应该重写 PaintComponent() 方法,该方法反过来将调用这 3 个方法。此外,传递给每个方法的参数应该是 Paint 类的属性。因此,您可能还需要添加一些 setter 方法来为所有属性赋值。
另外,不要忘记重写 Paint 类的 getPreferredSize() 方法,以便布局管理器可以正确布局组件。
我建议您首先阅读 Swing 教程中关于 自定义绘画 获取更详细的解释和工作示例。
The methods paintGraph(...), paintXAxis(...) and paintYAxis(...) should not invoke paintComponent().
Instead the code should be the other way around. That is you should override the paintComponent() method which in turn will invoke those 3 methods. Also the parameters you pass to each of the methods should be properties of the Paint class. So you may also need to add some setter method to assign values to all the properties.
Also, don't forget to override the getPreferredSize() method of the Paint class so the component can be layed out properly by layout managers.
I suggest you start by reading the section from the Swing tutorial on Custom Painting for a more detailed explanation and working examples.
该类不会覆盖
paintComponent(Graphics)
。所显示的代码还有其他一些奇怪的方面,首先是……
用任何人类语言来说,“屏幕中心”意味着什么?如果是这样,可以使用更简单的方法来完成:
但更好的是,我们现在拥有..
请参阅 这里举一个简单的例子。
The class does not override
paintComponent(Graphics)
.There are some other odd aspects to the code shown, starting with..
What does that translate to in any human language, the 'center of the screen'? If so, that can be accomplished simpler using:
But better still, we now have..
See here for a quick example.