/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 *//*
 * DDALine.java
 *
 * Created on 2009-5-7, 14:58:09
 */
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
/**
 *
 * @author Administrator
 */
public class DDALine extends javax.swing.JFrame {    /** Creates new form DDALine */
    public DDALine() {        initComponents();
        
    }    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("画线--DDA(Created by 200640345 罗会明)");        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("图像"));        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 568, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 528, Short.MAX_VALUE)
        );        jLabel1.setText("jLabel1");        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 580, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(11, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );        pack();
    }// </editor-fold>    public void paint() {
        Graphics g = jPanel1.getGraphics();
        Graphics2D g2 = (Graphics2D)g;        width = jPanel1.getSize().width;
        height = jPanel1.getSize().height;
        System.out.println(width + "  " + height);
        Point origin = new Point(width / 2, height / 2);
        //g2.drawLine(0, height / 2 , width, height / 2);
        //g2.drawLine(width / 2, 0, width / 2, height);
        g2.fillOval(33, 33, 5, 5);
        g2.drawString("Test", 20, 20);    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                DDALine line = new DDALine();
                javax.swing.JOptionPane jop = new javax.swing.JOptionPane();
                line.setVisible(true);
                line.paint();
            }
        });
    }    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration    private int width;
    private int height;}

解决方案 »

  1.   

    不要自己写paint()方法,要重载父类的,然后画图:
        public void paint(Graphics gg) { 
            super.paint(gg);
            Graphics g = jPanel1.getGraphics(); 
            Graphics2D g2 = (Graphics2D)g;         width = jPanel1.getSize().width; 
            height = jPanel1.getSize().height; 
            System.out.println(width + "  " + height); 
            Point origin = new Point(width / 2, height / 2); 
            //g2.drawLine(0, height / 2 , width, height / 2); 
            //g2.drawLine(width / 2, 0, width / 2, height); 
            g2.fillOval(33, 33, 5, 5); 
            g2.drawString("Test", 20, 20);     }