简体   繁体   English

在JPanel内绘画

[英]Painting inside JPanel

Im trying to represent a given math function inside of a JPanel, for now I just have the axis, but the axis isnt painting and I dont know why, by the way if I call new Axis() inside the frame it does paint. 我试图在JPanel内部表示给定的数学函数,现在我只有轴了,但是轴没有绘画,而且我不知道为什么,顺便说一句,如果我在框架内调用new Axis()会绘画。

Main class 主班

public class MyClass{

final static int HEIGHT = 400;
final static int WIDTH = 400;

public static void main(String[] args) {

    JFrame frame = new JFrame();
    frame.setSize(WIDTH,HEIGHT);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setPanel(frame);

    frame.setVisible(true);
}

static void setPanel(JFrame frame) {
    JPanel panel = new JPanel();

    panel.setBackground(Color.GRAY);
    panel.add(new Axis());

    frame.add(panel);
}

}

Axis Class 轴类

@SuppressWarnings("serial")
public class Axis extends JComponent{
static final int LONG_AXIS = 150;

public void paintComponent(Graphics g) {

    int CenterX = getWidth()/2;
    int CenterY = getHeight()/2;

    g.setColor(Color.BLACK);

    //x axis line
    g.drawLine(CenterX - LONG_AXIS, CenterY, CenterX + LONG_AXIS, CenterY);

    //y axis line
    g.drawLine(CenterX, CenterY - LONG_AXIS, CenterX, CenterY + LONG_AXIS);

}


}

Another question: Is it possible to have multiple paint methods? 另一个问题:是否可能有多种绘制方法?

When running, check the size of your Axis object, and you'll likely find that it is [0, 0] or [1, 1] in size since you're adding it to a JPanel that uses FlowLayout by default, and so will not cause Axis to expand, and Axis's default preferred size is [0, 0]. 运行时,检查Axis对象的大小,您可能会发现它的大小为[0,0]或[1,1],因为您默认将其添加到使用FlowLayout的JPanel中,因此不会导致Axis扩展,并且Axis的默认首选大小是[0,0]。

Consider overriding the Axis component's getPreferredSize() method to return a reasonable dimension, or have its containing JPanel use BorderLayout and add it BorderLayout.CENTER, or do both. 考虑重写Axis组件的getPreferredSize()方法以返回合理的尺寸,或者使其包含的JPanel使用BorderLayout并将其添加到BorderLayout.CENTER中,或者同时执行两者。

Also, don't forget to call the super's paintComponent method in your override. 另外,不要忘记在覆盖中调用上级的paintComponent方法。


Regarding, 关于,

Ok, I mean if its a good practice, for example one paint method for the axis, and another paint method to do the draw math function 好的,我的意思是说如果它是一种好的做法,例如,一种用于轴的绘制方法,另一种用于绘制数学函数的绘制方法

Consider creating methods that paintComponent(...) calls for each of these steps. 考虑创建针对每个步骤都需要paintComponent(...)调用的方法。 If complex, you could delegate it to non-component objects that your drawing component holds and that again paintComponent calls. 如果很复杂,则可以将其委派给绘图组件保存的非组件对象,并再次调用paintComponent。

Also, static parts of your image, ie, the x and y axis's are best drawn to a BufferedImage that is then drawn in paintComponent via g.drawImage(....) 另外,图像的静态部分(即x和y轴)最好绘制到BufferedImage上,然后通过g.drawImage(....)在paintComponent中绘制

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM