Java绘图在Jarame上的JPanel上

时间:2010-09-09 00:58:19

标签: java swing jpanel custom-painting

您好我有一个JFrame,并且它上面有两个JPanel。我的目的是利用JPanels。任何人都可以分享任何Java代码吗?

2 个答案:

答案 0 :(得分:5)

所有JComponents(JPanel继承自的)都有一个paintComponent(Graphics g )方法可以覆盖。

基本上......哦..好吧,我认为这会更合适:

http://java.sun.com/developer/technicalArticles/GUI/java2d/java2dpart1.html

天真的样本:

sample

源代码:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.geom.*;
    public class X  {
        public static void main( String [] args ) {
             JFrame frame = new JFrame();
             frame.add( new JPanel() {
                 public void paintComponent( Graphics g ) {
                    super.paintComponent(g);
                    Graphics2D g2 = (Graphics2D)g;

                    Line2D line = new Line2D.Double(10, 10, 40, 40);
                    g2.setColor(Color.blue);
                    g2.setStroke(new BasicStroke(10));
                    g2.draw(line);
                 }
            });
            frame.setVisible( true );
        }
    }

答案 1 :(得分:2)

查看Java tutorials页面。从2D图形教程开始。