Do I need to use repaint?

I designed a Combobox, when I click one item, the code will plot the same polygon with a different color. The plot part is a separated method, do I need to use a repaint() method?Thanks
[199 byte] By [ardmorea] at [2007-11-27 10:06:24]
# 1

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class ChangingColors extends JPanel {

int[] xpoints = { 200, 250, 150 };

int[] ypoints = { 100, 250, 250 };

Color color;

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

g2.setPaint(color);

Polygon polygon = new Polygon(xpoints, ypoints, 3);

g2.draw(polygon);

}

private JPanel getColorControl() {

final Color[] colors = {

Color.red, Color.green.darker(), Color.blue, Color.orange

};

color = colors[0];

String[] items = { "red", "green", "blue", "orange" };

final JComboBox combo = new JComboBox(items);

combo.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

int index = ((JComboBox)e.getSource()).getSelectedIndex();

color = colors[index];

repaint();

}

});

JPanel panel = new JPanel();

panel.add(combo);

return panel;

}

public static void main(String[] args) {

ChangingColors test = new ChangingColors();

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(test.getColorControl(), "First");

f.getContentPane().add(test);

f.setSize(400,400);

f.setLocation(200,200);

f.setVisible(true);

}

}

crwooda at 2007-7-13 0:42:40 > top of Java-index,Security,Cryptography...