gpt4 book ai didi

Java图形缓慢渲染的一个draw方法。如何修复它?

转载 作者:行者123 更新时间:2023-12-01 14:47:16 26 4
gpt4 key购买 nike

为什么下面的代码在圆圈前绘制了相当于连续执行 x=+10 语句的总和的大空格?

class Panel extends JPanel {
private int x=10;
public void paintComponent( Graphics g ) {
super.paintComponent( g );
g.setColor( Color.MAGENTA );
for (int i=1; i<=8; i++) {
g.drawOval( x, 10, 50, 50 );
x+=10;
}
}
}

public class Circles156 {
public static void main(String[] args) {
JFrame frame = new JFrame( "Drawing lines, rectangles and ovals" );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
Panel Panel = new Panel();
Panel.setBackground( Color.WHITE );
frame.add( Panel ); // add panel to frame
frame.setSize( 800, 300 ); // set frame size
frame.setVisible( true ); // display frame
}
}

最佳答案

将x放入paintComponent()方法中。每次调用时,x 都会将“初始值”增加 80。

关于Java图形缓慢渲染的一个draw方法。如何修复它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15299659/

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