这是我必须为我的编程课做的一个项目...而且我正在努力学习它,所以我希望有人可以帮助我,或者至少指出我正确的方向:)
该项目的要求是(翻译自荷兰语):
1个固定的绘制对象 - 花盆。
根据文本框中给出的“总计”重复多次的对象。在花盆中种植这些花。(一朵花至少有茎和花)
最少1个文本框,其中输入总数(类型为int)。此总数代表必须绘制的花的总数。通过循环执行此操作。
为花的颜色制作一个文本框,并添加红色,黄色,橙色和其他一些颜色。随着颜色的每次修改,花朵都会变色。列表框会更好。
按一下按钮,花蕾的大小翻倍,另一个按钮将花朵恢复到初始大小。
最少一个或多个文本框,其中可以给出对象的大小以及总数。对象之间的距离取决于给定的大小。这些对象可以是任何ex。喷壶,一杯水,迷你农民...让你的幻想工作。
为每个绘制的对象添加注释。有了这个,你就可以说出究竟是什么了。
还可以使用图像。
到目前为止,我已经设法绘制了一朵花,并且我已经让我的循环工作或多或少......但是这些位置搞得一团糟。我正在使用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
}
答案 0 :(得分: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
}
}
在这里,用你的一个调整上面的代码,然后大饱眼福。
尝试learn layout managers,而不是依赖于NetBean的免费设计支持。有很好的布局可以帮助我们合作。他们最终会让你开心。