简体   繁体   English

在扩展Canvas时使用drawLine()绘制粗线

[英]Drawing a bold line with drawLine() when extending Canvas

Given the following : 鉴于以下内容:

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);
        }


    }       
    ...
}

When I draw the lines of currLine I get this : 当我画出currLine我得到了这个:

在此输入图像描述

As you can see , I made 3 calls to drawline() , to make it more bold ,but it still doesn't quite as I wanted . 正如你所看到的,我对drawline()进行了3次调用,使其更加大胆,但它仍然不像我想要的那样。

How can I draw one bold line ? 我怎样画一条粗线?

Graphics2D#setStroke controls the style of line that is painted. Graphics2D#setStroke控制绘制的线条样式。 BasicStroke is the default implementation of Stroke and has a number of parameters, the one you're most interested in is the width. BasicStrokeStroke的默认实现,有许多参数,你最感兴趣的是宽度。

在此输入图像描述

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();
        }

    }

}

Have a look at Stroking and filling Graphics Primitives for more details 有关更多详细信息,请查看Stroking和填充Graphics Primitives

Use the setStroke() method located in the Graphics library: http://docs.oracle.com/javase/1.5.0/docs/api/java/awt/Graphics2D.html 使用位于Graphics库中的setStroke()方法: httpsetStroke()

setStroke(Stroke s)
    Sets the Stroke for the Graphics2D context.

It takes a Stroke object, http://docs.oracle.com/javase/1.5.0/docs/api/java/awt/Stroke.html 它需要一个Stroke对象, http://docs.oracle.com/javase/1.5.0/docs/api/java/awt/Stroke.html

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

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