【发布时间】:2018-10-18 21:07:07
【问题描述】:
所以在过去的几天里,我试图实现一个更简单的绘图仪版本。 我遇到的一个大问题是重绘时出现的错误。 基本上,我在一个类中设计了我的程序,该类负责在单击另一个类中的 JButton 后绘制整个坐标系和给定函数。另一个类包含按下的 JButton。按下 JButton 后,它会调用坐标系类中的一个函数来重新绘制图片。这两个类都在扩展 JPanel。
错误是,当我在按下按钮时进行重新绘制时,按钮是在坐标系上绘制的,而不是在原来的位置上,换句话说,即使我没有改变任何东西,在另一个 JPanel 上关于展示位置和东西。这两个类都被添加到使用 GridLayout 的 JFrame 中。
谁能告诉我为什么 super.paintComponent(g);解决了那个bug?
编辑:添加代码
窗口类
public class main {
public static void main(String[] args) throws SemanticFailureException {
int x = 800;
int y = 600;
JFrame frame = new JFrame();
frame.setLayout(new GridLayout());
frame.setTitle("Function plotter");
frame.setSize(2*x, 2*y);
Surface test = new Surface(x, y, 10);
CommandDraw test1 = new CommandDraw(x/2,y,test);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.add(test);
frame.add(test1);
frame.setVisible(true);
}
}
坐标系类:(为简单起见将坐标系绘制为矩形,仅绘制矩形仍然存在错误)
public class Surface extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
boolean drawFunct;
public Surface(int x1, int y1, int coordLength) {
setSize(x1,y1);
drawFunct = false;
}
public void paintComponent(Graphics g) {
super.paintComponent(g); // without this the jbutton occures on the left
// create Graphics object to get more functions
Graphics2D g2 = (Graphics2D) g;
// draw Plotter
drawFunction(g2);
if (drawFunct)
g2.drawLine(0, 0, 80, 80);
}
public void drawFunction(Graphics2D g) {
g.drawRect(40, 40, 30, 30);
}
public void redraw() {
drawFunct = true;
repaint();
}
}
带有 JButton 的类:
public class CommandDraw extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
JButton makeDraw;
JTextField inputPoly;
Surface surf;
public CommandDraw(int x, int y, Surface surf) {
this.surf = surf;
setSize(x,y);
setLayout(new FlowLayout());
makeDraw = new JButton("draw Function");
makeDraw.setBackground(Color.LIGHT_GRAY);
makeDraw.setFocusable(false);
makeDraw.addActionListener( new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
surf.redraw();
}
});
add(makeDraw);
inputPoly = new JTextField("Input polynomial");
inputPoly.setHorizontalAlignment(JTextField.CENTER);
add(inputPoly);
}
}
【问题讨论】:
-
如果没有看到你的代码,很难理解你到底做错了什么。您的图形故障可能有多种原因。在挥杆中进行自定义绘画时,人为错误的机会很多。
-
@DudeDoesThings 不,不是他的情况。这种“行为”完全是意料之中的。
标签: java