2016年2月22日 星期一

JAVA GUI ShowLines.java (JAVA8 無效)

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

public class ShowLines extends JPanel
{
public void painComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
//根據 MyPanel 的寬與高來調整線條長短
double Width =10; //getSize().width/6-20;
double Height =10; //getSize().height-30;
//畫 CAP_BUTT , JOIN_BEVEL線
g2.setStroke(new BasicStroke(15,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL));
g2.draw(new Line2D.Double(10,10+Height,10+Width,10));
g2.draw(new Line2D.Double(10+Width,10,10+2*Width,10+Height));

//畫 CAP_ROUND 、 JOIN_MITER線
g2.setStroke(new BasicStroke(15,BasicStroke.CAP_ROUND,BasicStroke.JOIN_MITER));
g2.draw(new Line2D.Double(30+2*Width,10+Height,30+3*Width,10));
g2.draw(new Line2D.Double(30+3*Width,10,30+4*Width,10+Height));

//畫CAP_SQUARE 、 JOIN_ROUND線
g2.setStroke(new BasicStroke(15,BasicStroke.CAP_SQUARE,BasicStroke.JOIN_ROUND));
g2.draw(new Line2D.Double(50+4*Width,10+Height,50+5*Width,10));
g2.draw(new Line2D.Double(50+5*Width,10,50+6*Width,10+Height));
}
public static void main(String[] args)
{
JFrame f = new JFrame("不同樣式的線斷");
f.getContentPane().add(new ShowLines());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(720,160);
f.setVisible(true);
}
}

沒有留言: