练习paintComponent方法 [英] practicing paintComponent method
问题描述
我想练习paintComponent方法.我的程序由不同文件中的两个类(test 和paintClass)组成.我想通过使用paintComponent方法垂直和水平绘制线条将区域划分为25个正方形.我的构造函数现在只有paintComponent.我知道它不是很有效,但我将来会添加按钮、标签和其他东西,所以我需要像这样使用这个程序.当我运行程序时,我得到一个空指针异常错误.你能帮忙吗?
I want to practice paintComponent method. My program is consist of two classes(test and paintClass) in different files. I want to divide the area into 25 squares by drawing lines vertically and horizontally by using paintComponent method. My constructor only has paintComponent now. I know it is not very efficient but I will add buttons, labels and other things in the future so I need to use the program like this. When I run the program I get a nullpointerexception error.Can you help?
我将页面更改为 g 并在代码中添加了一些行.还是一样的错误.
EDIT : I changed the page into g and added some lines into code. Still has the same error.
import javax.swing.*;
import java.awt.*;
public class test
{
public static void main(String[] args)
{
JFrame frame = new JFrame("buttons");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
paintClass paint = new paintClass();//This line has error.
paint.repaint();//ADDED
frame.add(paint);
frame.pack();
frame.setVisible(true);
}
}
import javax.swing.*;
import java.awt.*;
public class paintClass extends JPanel
{
private Graphics g;
private int interval,side,number;
public paintClass()
{
this.repaint();//ADDED
paintComponent(g);//This line has error.
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);//ADDED and has an error.
this.repaint();//ADDED
side = 250;
number = 5;
interval = side / number;
g.drawRect(0,0, side, side);
for(int i = 0; i <= number - 1; i++)
{
for(int j = 0; j <= number - 1; j++)
{
g.drawLine(i * interval, 0, i * interval, side);
}
g.drawLine(0, i * interval, side, i * interval);
}
}
}
推荐答案
您正在使用尚未实例化的 Graphics 对象在paintClass 的构造函数中调用paintComponent.对于简单的绘图,您不需要直接调用此方法 - 它会在必要时由 EDT(使用适当的 Graphics 对象)调用(换句话说,删除该行并且不要保留对 Graphics 的引用目的).
You are calling paintComponent in the paintClass's constructor with a Graphics object that has not been instantiated. For simple drawing, you shouldn't need to call this method directly - it will be called by the EDT (with the appropriate Graphics object) when necessary (in other words, remove that line and don't hold onto a reference to the Graphics object).
//constructor of Class - note Classnames should start with uppercase
public paintClass(){
//no need to call paintComponent or repaint here
}
您可以通过调用 JComponent(JPanel 的父类)定义的 repaint() 方法来请求重绘.您还应该调用父方法
You can request a repaint by calling the repaint() method defined by JComponent (the parent class of JPanel). You should also call the parent method
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
//do your custom drawing here, and never call repaint from this method
}
如果您还没有,我建议您学习有关自定义绘画的 Oracle 教程:http://docs.oracle.com/javase/tutorial/uiswing/painting/
I would recommend, if you have not yet, is to study the Oracle tutorials about Custom painting: http://docs.oracle.com/javase/tutorial/uiswing/painting/
这篇关于练习paintComponent方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!