簡體   English   中英

嘗試使用循環繪制多朵花

[英]Trying to draw multiple flowers using a loop

這是我必須為編程課做的一個項目...而且我在這方面做得很不好,所以我希望這里有人可以幫助我或至少指向正確的方向:)

該項目的要求是(從荷蘭語翻譯):

  1. 1個固定的繪制對象-花盆。

  2. 1個對象,根據文本框中給出的“總計”重復多次。 將這些花種在花盆中。(一朵花至少有莖和一朵花)

  3. 至少要輸入1個文本框(類型為int)的文本框。該總數代表必須繪制的花朵總數。 循環執行此操作。

  4. 為花的顏色創建一個文本框,然后添加紅色,黃色,橙色和其他一些顏色。 每次修改顏色,花朵都會改變顏色。列表框會更好。

  5. 制作一個按鈕,使花蕾的大小加倍,另一個按鈕使花朵恢復其初始大小。

  6. 至少可以指定對象大小的一個或多個文本框以及總計大小。對象之間的距離取決於給定的大小。 這些對象可以是任何東西。 噴壺,水,迷你農場......讓您的幻想成真。

  7. 向每個繪制的對象添加注釋。 您可以在此說出正在繪制的內容。

  8. 還要利用圖像。

到目前為止,我已經設法畫了一朵花,而且我的循環可以或多或少地工作了……但是位置變得一團糟。 我正在使用Netbeans。 這是我面板中的代碼:

    package Versie4;
import java.awt.Graphics;
import java.awt.Color;

public class PanelFlowers extends javax.swing.JPanel {

    private int amount;

    public PanelFlowers() {
        initComponents();
        repaint();
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);

        int teller;

        g.setColor(Color.RED);  //flowerpot
        g.fillRect(300, 350, 500, 100);

        for (teller=1; teller <= amount ;teller++) { 

            //Flower 1
        g.setColor(Color.GREEN);  //stem
        g.fillRect(320 * teller, 250, 10, 100);


        g.setColor(Color.PINK); //petals
        g.fillOval(304 * teller, 190, 40, 40);
        g.fillOval(330 * teller, 210, 40, 40);
        g.fillOval(320 * teller, 240, 40, 40);
        g.fillOval(290 * teller, 240, 40, 40);
        g.fillOval(280 * teller, 210, 40, 40);

        g.setColor(Color.YELLOW);  //pistil
        g.fillOval(312 * teller, 225, 25, 25);




        }


    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        lblamount = new javax.swing.JLabel();
        txtamount = new javax.swing.JTextField();
        lblcolor = new javax.swing.JLabel();
        txtcolor = new javax.swing.JTextField();
        btngrow = new javax.swing.JButton();
        btnreset = new javax.swing.JButton();

        lblamount.setText("Amount: ");

        txtamount.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtamountActionPerformed(evt);
            }
        });

        lblcolor.setText("Color: ");

        btngrow.setText("Grow!");

        btnreset.setText("Reset Size");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblamount)
                .addGap(18, 18, 18)
                .addComponent(txtamount, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(41, 41, 41)
                .addComponent(lblcolor)
                .addGap(18, 18, 18)
                .addComponent(txtcolor, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(60, 60, 60)
                .addComponent(btngrow)
                .addGap(18, 18, 18)
                .addComponent(btnreset)
                .addContainerGap(230, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblamount)
                    .addComponent(txtamount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblcolor)
                    .addComponent(txtcolor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btngrow)
                    .addComponent(btnreset))
                .addContainerGap(412, Short.MAX_VALUE))
        );
    }// </editor-fold>                        

    private void txtamountActionPerformed(java.awt.event.ActionEvent evt) {                                          
amount = Integer.parseInt(txtamount.getText());
repaint();
    }                                         

    // Variables declaration - do not modify                     
    private javax.swing.JButton btngrow;
    private javax.swing.JButton btnreset;
    private javax.swing.JLabel lblamount;
    private javax.swing.JLabel lblcolor;
    private javax.swing.JTextField txtamount;
    private javax.swing.JTextField txtcolor;
    // End of variables declaration                   
}

這是框架:

package Versie4;


public class FrameFlowers extends javax.swing.JFrame {


    public FrameFlowers() {
        initComponents();
        setSize(900, 600);
        setContentPane(new PanelFlowers());
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrameFlowers.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrameFlowers.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrameFlowers.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrameFlowers.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FrameFlowers().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    // End of variables declaration                   
}
  1. 當您嘗試水平移動花的位置時,您試圖進行乘法。 您的想法是最正確的。 但是將相同的整數乘以不同的petals將不會產生相同的移位量。 而是讓我們使用變量delta x ,該變量將使花朵(包括花瓣)與我們定義的delta x發生位移。 但是,我們將把teller乘以該delta ,然后將其添加到橢圓的x坐標中,以確保所有花瓣的x坐標都相對均勻地移動。

     public void paintComponent(Graphics g){ super.paintComponent(g); int teller; g.setColor(Color.RED); //flowerpot g.fillRect(300, 350, 500, 100); int x = 1; for (teller=1; teller <= amount ;teller++) { //Flower 1 g.setColor(Color.GREEN); //stem g.fillRect(320 + x, 250, 10, 100); g.setColor(Color.PINK); //petals g.fillOval(304 + x, 190, 40, 40); g.fillOval(330 + x, 210, 40, 40); g.fillOval(320 + x, 240, 40, 40); g.fillOval(290 + x, 240, 40, 40); g.fillOval(280 + x, 210, 40, 40); g.setColor(Color.YELLOW); //pistil g.fillOval(312 + x, 225, 25, 25); x = teller * 80; //<---- here we are multiplying with the teller a fixed amount } } 

    在這里,與您一起調整上面的代碼,並大飽眼福。

  2. 嘗試學習布局管理器 ,而不要依賴NetBean的免費設計支持。 開發了非常好的布局來幫助我們進行工作。 最后,它們會讓您開心。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM