2013-04-14 40 views
5

Do sau:Vẽ một đường đậm với DrawLine() khi mở rộng Canvas

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


    }  
    ... 
} 

Khi tôi vẽ theo dòng của currLine tôi có được điều này:

enter image description here

Như bạn có thể thấy, Tôi đã thực hiện 3 cuộc gọi đến drawline(), để làm cho nó đậm hơn, nhưng nó vẫn không hoàn toàn như tôi muốn.

Làm cách nào để vẽ một đường đậm?

+0

Bạn có thể sử dụng fillPolygon – Justin

+0

Xem phương pháp fillPolygon: [Graphics] (http://docs.oracle.com/javase/6 /docs/api/java/awt/Graphics.html) – Justin

Trả lời

6

Graphics2D#setStroke điều khiển phong cách của dòng đó được sơn. BasicStroke là cài đặt mặc định là Stroke và có một số tham số, cái mà bạn quan tâm nhất là chiều rộng.

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

    } 

} 

Có một cái nhìn tại Stroking and filling Graphics Primitives để biết thêm chi tiết

+0

Được đánh giá cao. – ron

Các vấn đề liên quan