Java drawing on JPanel which on a JFrame [closed]

♀尐吖头ヾ 提交于 2019-12-02 10:01:06

All the JComponents ( of which JPanel inherits from ) have a paintComponent(Graphics g ) method that you can override.

Basically... oh.. well, I think this would be more appropiate:

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

Naive sample:

Source code:

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

Check out the Java tutorials page. Start with the 2D Graphics tutorial.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!