Java - 方法repaint()不调用paintComponent()方法

时间:2013-11-27 22:15:39

标签: java swing components paint repaint

大家好!

我有一个奇怪的问题,关注所有这些油漆“东西”。 我正在创建一个可以绘制高速公路曲线(http://en.wikipedia.org/wiki/Dragon_curve)的小型SWING应用程序。 我的项目中有3个课程:

  • DragonData - 有关计算和存储龙点的所有内容,
  • GUI - 用户界面窗口,
  • Point - 指定单点的类,

现在好了:我有一个JPanel,我想要绘制点和一个点击的按钮开始计算点,并绘制龙。

我的问题是:为什么它不起作用?我为这个JPanel创建了覆盖方法paintComponent,并在'buttonDrawActionPerformed'方法中调用它。我做错了什么?

我的项目有一个链接:http://www73.zippyshare.com/v/9630962/file.html


代码

Point.java

public class Point {

    private double x;
    private double y;

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }
}

DragonData.java

import java.util.ArrayList;

public class DragonData {

    private static final int RATE = 500;
    private static ArrayList<Point> dragonPoints = null;

    public static void generatePoints(double x, double y, int iter, 
            int shiftX, int shiftY){ // ### TYP double

        dragonPoints = new ArrayList<Point>();

        for(int i = 0 ; i < iter ; i++){

            int option = (int)(Math.random() * 2);

            switch(option){

                case 0:
                    x = -0.4 * x - 1;
                    y = -0.4 * y + 0.1; //### !!! ### - aktualizujemy wartosc zmiennej
                    break; 

                case 1:
                    x = 0.76 * x - 0.4 * y;
                    y = 0.4 * x + 0.76 * y;
                    break;
            }

            dragonPoints.add(new Point(x, y));
        };

        calibrateCoordinates();
        filterPoints(shiftX, shiftY);
    }

    private static void calibrateCoordinates(){

        int pointsCount = dragonPoints.size();

        for(int i = 0 ; i < pointsCount ; i++){

            double scaledX = dragonPoints.get(i).getX() * RATE;
            double scaledY = dragonPoints.get(i).getY() * RATE;

            dragonPoints.get(i).setX(scaledX);
            dragonPoints.get(i).setY(scaledY);
        }
    }

    private static void filterPoints(int shiftX, int shiftY){

        int pointsCount = dragonPoints.size();
        ArrayList<Point> choosen = new ArrayList<Point>();

        for(int i = 0 ; i < pointsCount ; i++){

            double x = dragonPoints.get(i).getX() + shiftX;
            double y = dragonPoints.get(i).getY() + shiftY;

            if((x > 0 && x < 800) && (y > 0 && y < 600))
                choosen.add(new Point(x,y));
        }

        dragonPoints.clear();
        dragonPoints.addAll(choosen);

        System.out.println("New points count: " + dragonPoints.size());

    }

    public static ArrayList<Point> getDragonPoints() {
        return dragonPoints;
    }

}

GUI.java

import java.awt.Graphics;
import java.util.ArrayList;

public class GUI extends javax.swing.JFrame {

    /**
     * Creates new form GUI
     */
    public GUI() {
        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">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        buttonDraw = new javax.swing.JButton();
        fieldX = new javax.swing.JTextField();
        fieldY = new javax.swing.JTextField();
        fieldIter = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        panelCanvas = new javax.swing.JPanel(){

            @Override
            protected void paintComponent(Graphics g) {

                super.paintComponent(g);

                ArrayList<Point> dragonPoints = DragonData.getDragonPoints();
                int newPointsCount = dragonPoints.size();

                System.out.println("REPAINT BEFORE");

                for(int i = 0 ; i < newPointsCount; i++){
                    int pointX = (int) dragonPoints.get(i).getX() + 100;
                    int pointY = (int) dragonPoints.get(i).getY();

                    g.drawRect(pointX, pointY, 1, 1);
                }
            }
        };
        panelCanvas = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(800, 600));
        setResizable(false);

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));

        buttonDraw.setText("Draw Dragon");
        buttonDraw.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonDrawActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setText("X:");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel2.setText("Y:");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel3.setText("Loops (>=1000)");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(fieldX, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(fieldY, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(fieldIter, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 60, Short.MAX_VALUE)
                .addComponent(buttonDraw, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(fieldX, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(fieldY, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(fieldIter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel1)
                        .addComponent(jLabel2)
                        .addComponent(jLabel3))
                    .addComponent(buttonDraw))
                .addGap(49, 49, 49))
        );

        panelCanvas.setBackground(new java.awt.Color(255, 255, 255));
        panelCanvas.setMinimumSize(new java.awt.Dimension(800, 600));
        panelCanvas.setPreferredSize(new java.awt.Dimension(800, 600));
        panelCanvas.setRequestFocusEnabled(false);

        jLabel4.setText("Author Label");

        javax.swing.GroupLayout panelCanvasLayout = new javax.swing.GroupLayout(panelCanvas);
        panelCanvas.setLayout(panelCanvasLayout);
        panelCanvasLayout.setHorizontalGroup(
            panelCanvasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelCanvasLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel4)
                .addContainerGap())
        );
        panelCanvasLayout.setVerticalGroup(
            panelCanvasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelCanvasLayout.createSequentialGroup()
                .addContainerGap(575, Short.MAX_VALUE)
                .addComponent(jLabel4)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(panelCanvas, javax.swing.GroupLayout.DEFAULT_SIZE, 817, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelCanvas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonDrawActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonDrawActionPerformed

        double x = Double.valueOf(this.fieldX.getText());
        double y = Double.valueOf(this.fieldY.getText());
        int iter = Integer.valueOf(this.fieldIter.getText());

        int middleX = this.panelCanvas.getSize().width / 2;
        int middleY = this.panelCanvas.getSize().height / 2;

        DragonData.generatePoints(x, y, iter, middleX, middleY);

        this.panelCanvas.repaint();
    }//GEN-LAST:event_buttonDrawActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        GUI gui = new GUI();
        gui.setLocationRelativeTo(null);
        gui.setVisible(true);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonDraw;
    private javax.swing.JTextField fieldIter;
    private javax.swing.JTextField fieldX;
    private javax.swing.JTextField fieldY;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel panelCanvas;
    // End of variables declaration//GEN-END:variables

}

1 个答案:

答案 0 :(得分:1)

首先在方法GUI的{​​{1}}课程中,您可以使用覆盖的initComponents()paintComponent(...)创建面板,然后在下一行中使用公共{{{ 1}}。

请删除下一行method panelCanvas = new javax.swing.JPanel() {...},一切正常。