Reputation: 247
I want only the petals of my flowers to change color. I've gotten it to work with a textfield but I've decided a combobox would be better(so my lecturer knows which colors he can choose). I saw a few examples where they would make the selection in the combobox show up in a textfield but I'm not sure how to do that with my flowers.
So in short, how do I get the combobox to change the color of my flower petals and would an array be better(and why/not)?
Picture - http://s903.photobucket.com/user/Nicole_Pretorius/media/Flowers_zps936f6f40.jpg.html
Code for Frame:
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
}
Code for Panel:
package Versie4;
import java.awt.Color;
import java.awt.Graphics;
public class PanelFlowers extends javax.swing.JPanel {
private int amount;
private String color = "";
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);
int x = 1;
for (teller=1; teller <= amount ;teller++) {
//Flower 1
g.setColor(Color.GREEN); //stem
g.fillRect(320 + x, 250, 10, 100);
switch (color) { //Colours of petals
case "red":
g.setColor(Color.red);
break;
case "blue":
g.setColor(Color.blue);
break;
case "yellow":
g.setColor(Color.yellow);
break;
case "orange":
g.setColor(Color.orange);
break;
case "pink":
g.setColor(Color.PINK);
break;
case "purple":
g.setColor(new Color(102, 0, 204));
break;
}
g.fillOval(304 + x, 190, 40, 40); //petals
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;
}
}
@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();
colorCombo = new javax.swing.JComboBox();
lblamount.setText("Amount: ");
txtamount.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtamountActionPerformed(evt);
}
});
lblcolor.setText("Color: ");
txtcolor.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtcolorActionPerformed(evt);
}
});
btngrow.setText("Grow!");
btnreset.setText("Reset Size");
colorCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "red", "blue", "yellow", "orange", "pink", "purple" }));
colorCombo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
colorComboActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(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))
.addGroup(layout.createSequentialGroup()
.addGap(211, 211, 211)
.addComponent(colorCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.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))
.addGap(42, 42, 42)
.addComponent(colorCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(350, Short.MAX_VALUE))
);
}// </editor-fold>
private void txtamountActionPerformed(java.awt.event.ActionEvent evt) {
amount = Integer.parseInt(txtamount.getText());
color = this.txtcolor.getText();
repaint();
}
private void txtcolorActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void colorComboActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
// Variables declaration - do not modify
private javax.swing.JButton btngrow;
private javax.swing.JButton btnreset;
private javax.swing.JComboBox colorCombo;
private javax.swing.JLabel lblamount;
private javax.swing.JLabel lblcolor;
private javax.swing.JTextField txtamount;
private javax.swing.JTextField txtcolor;
// End of variables declaration
}
Upvotes: 0
Views: 332
Reputation: 15408
So in short, how do I get the combobox to change the color of my flower petals and would an array be better(and why/not)?
Yes, obviously. JComboBox
is a user input controlling component. It lets user to select item among various other items with same type but different taste. Array is not a component. It is just a inner data structure which we can use to maintain(add, save, remove) the item, nothing more. Well JComboBox has model known as DefaultComboBoxModel
which internally contain such data structure and contain elements and manage them for us.
However, the way you are using JComboBox
will get you into trouble, because if you select an item from the set of items, you will need to use if-else
or switch-case
check for finding the specific element. Apparently which is what you are doing now. As a solution which is simple(but rather junky):
public class PanelFlowers extends javax.swing.JPanel {
private int amount;
// private String color = "";
Color chosenColor;
public PanelFlowers() {
initComponents();
// repaint(); // <--- no need to call repaint in constructor
}
private void initComponents() {
// your other code------------
DefaultComboBoxModel model = new DefaultComboBoxModel();
model.addElement(new Color(152, 52, 152));
model.addElement(new Color(152, 12, 52));
model.addElement(new Color(152, 142, 120));
colorCombo.setModel(model);
colorCombo.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
JComboBox comb = (JComboBox)e.getSource();
chosenColor = (Color) comb.getSelectedItem();
repaint();
}
});
I have called the above solution junky. If you run the program by adopting the above approach you can actually directly use chosenColor
instance to draw inside paintComponent
function. Whenever you choose a Color from colorCombo
you will see that now the program responds with the selected color item. But the item will appear in comboBox as: java.awt.Color[r=xxx,g=xxx,b=xxx]
which is what probably you won't like.
How about learning about Color Chooser and make use of it.
Upvotes: 1