Hello, I am sophomore Computer Science student and I am stuck on my project.
My problem is very basic.
Heres the project question:
Create an application that enables to user to paint a picture. The user should be able to choose the shape to draw, the color in which the shape should appear and whether the shape should be filled with color. Use the graphical user interface components we discussed in this chapter, such as JComboBoxes, JRadioButtons and JCheckBoxes, to allow the user to select various options. The program should provide a JButton object that allows the user to erase the window.
now, I've created the framework of the program. That is, how the program looks and stuff. But there is one annying problem!
How do I paint?! As soon as I put the paint method inside my program, it completely overwrites the GUI I've created. Copy-paste the following code in your compiler (and read the comments towards the end):
public static void main (String args[]) { Project app = new Project(); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
// Initiating values Container c; JPanel p = new JPanel(); JRadioButton oval=new JRadioButton("Oval"); JRadioButton line=new JRadioButton("Line"); JRadioButton rect = new JRadioButton("Rect"); ButtonGroup b = new ButtonGroup(); JCheckBox filled = new JCheckBox("Filled"); JButton clear = new JButton("Clear"); String colorNames[] = {"Red","Green","Blue","Pink","Magenta","Orange"}; Color colors[] = {Color.red,Color.green,Color.blue,Color.pink,Color.magenta,Color.orange}; JList colorList = new JList(colorNames);
//Adding listeners for the Radio Buttons oval.addItemListener(new RadioButtonListener()); line.addItemListener(new RadioButtonListener()); rect.addItemListener(new RadioButtonListener());
show(); }
private class MyMouseHandler extends MouseAdapter { public void mouseClicked (MouseEvent e) { repaint(); // Calling the paint method
}
}
private class MyMouseMotionHandler extends MouseMotionAdapter {
}
private class RadioButtonListener implements ItemListener { public void itemStateChanged(ItemEvent e) {
} }
//public void paint (Graphics g) { // Run the program without the paint method, and then run it with the paint method. You'll see what I mean. // g.drawString("Hello",160,160); //}