[英]I need simplest way to draw a line in java in AWT panel
I need the simplest way to draw a line between to coordinates. 我需要最简单的方法在坐标之间画一条线。 Since this drawing line in my code will be repeated more than 200 times in a loop i need the easiest way.
由于我的代码中的这条画线将在循环中重复200多次,因此我需要最简单的方法。 I'm drawing the lines in a AWT panel component.
我正在AWT面板组件中绘制线条。
It's been a long time since I've used java.awt.Panel, but it should be something like: 自从我使用java.awt.Panel已经很长时间了,但是应该是这样的:
class Foo extends Panel {
public void paint(Graphics g) {
super.paint(g);
g.drawLine(x1,y1,x2,y2);
g.drawLine(x3,y3,x4,y4);
//...
}
}
If you want to switch to Swing you would use the JPanel and overwrite the paintComponent()
method. 如果要切换到Swing,则可以使用JPanel并覆盖
paintComponent()
方法。
import java.awt.Graphics;
import javax.swing.JPanel;
public class PanelWithLine extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawLine(x1,y1,x2,y2);
}
}
You can than redraw everything by calling repaint()
on your Jpanel. 您可以通过在Jpanel上调用
repaint()
来重绘所有内容。
You would probably change the coordinates and then call the repaint()
method in your loop. 您可能会更改坐标,然后在循环中调用
repaint()
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.