gpt4 book ai didi

java - 扩展 Canvas 时使用 drawLine() 绘制一条粗线

转载 作者:搜寻专家 更新时间:2023-10-31 19:56:19 24 4
gpt4 key购买 nike

鉴于以下情况:

public class NavigationCanvas extends Canvas implements MouseListener,MouseMotionListener,KeyListener  {

public void paint(Graphics g)
{

// some code
// more
// ...

g.setColor(Color.black);

// drawing each Line
for (int i=0; i<length; i++)
{
Line2D currLine = m_lines.get(i);

g.drawLine((int)currLine.getX1(),(int)currLine.getY1(),
(int)currLine.getX2(),(int)currLine.getY2());
g.drawLine((int)currLine.getX1()+1,(int)currLine.getY1()+1
,(int)currLine.getX2()+1,(int)currLine.getY2()+1);
g.drawLine((int)currLine.getX1()+2,(int)currLine.getY1()+2
,(int)currLine.getX2()+2,(int)currLine.getY2()+2);
}


}
...
}

当我绘制 currLine 的线条时,我得到了这个:

enter image description here

如您所见,我对 drawline() 进行了 3 次调用,以使其更加粗体,但仍然不够如我所愿。

如何画一条粗线?

最佳答案

Graphics2D#setStroke 控制绘制的线条样式。 BasicStrokeStroke 的默认实现,有许多参数,您最感兴趣的是宽度。

enter image description here

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestStroke {

public static void main(String[] args) {
new TestStroke();
}

public TestStroke() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();

int width = getWidth();
int height = getHeight();

int xDif = width / 4;
int yDif = height / 4;

g2d.setStroke(new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2d.drawLine(xDif, yDif, width - xDif, yDif);
g2d.setStroke(new BasicStroke(2, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2d.drawLine(width - xDif, yDif, width - xDif, height - yDif);
g2d.setStroke(new BasicStroke(3, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2d.drawLine(width - xDif, height - yDif, xDif, height - yDif);
g2d.setStroke(new BasicStroke(4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2d.drawLine(xDif, height - yDif, xDif, yDif);

g2d.dispose();
}

}

}

看看Stroking and filling Graphics Primitives了解更多详情

关于java - 扩展 Canvas 时使用 drawLine() 绘制一条粗线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15996036/

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