I need to create a program to draw shapes(user chooses with radio button), and whether or not the shape is filled(user chooses with check box). This is the code I have so far:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SelectShape extends JFrame implements ItemListener{
private JRadioButton line = new JRadioButton("Line");
private JRadioButton rect = new JRadioButton("Rectangle");
private JRadioButton oval = new JRadioButton("Oval");
private JCheckBox fill = new JCheckBox("Filled");
private FigurePanel fp;
public static void main(String[] args) {
SelectShape frame = new SelectShape();
frame.setTitle("Select Shape");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setSize(400,400);
frame.setVisible(true);
}
public SelectShape() {
JPanel p1 = new JPanel();
p1.add(fp);
fp.setBackground(Color.WHITE);
p1.setSize(200,400);
JPanel p2 = new JPanel();
p2.setLayout(new FlowLayout());
p2.add(line);
p2.add(rect);
p2.add(oval);
p2.add(fill);
add(p2, "South");
line.addItemListener(this);
rect.addItemListener(this);
oval.addItemListener(this);
fill.addItemListener(this);
}
public void ItemStateChanged(ItemEvent e) {
if(rect.isSelected()) {
FigurePanel.dRect();
repaint();
}
else if(oval.isSelected()) {
FigurePanel.dOval();
repaint();
}
else if(line.isSelected()) {
FigurePanel.dLine();
repaint();
}
if(fill.isSelected()) {
FigurePanel.fill();
repaint();
}
else {
FigurePanel.erase();
repaint();
}
}
}
class FigurePanel extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
public void dLine(Graphics g) {
g.drawLine(10,10,160,10);
}
public void dRect(Graphics g) {
g.drawRect(10,10,150,50);
}
public void dOval(Graphics g) {
g.drawOval(10,10,150,50);
}
public void fill(Graphics g) {
g.setColor(Color.GREEN);
if(rect.isSelected()) {
g.fillRect(10,10,150,50);
}
else if(oval.isSelected()) {
g.fillOval(10,10,150,50);
}
}
public void erase(Graphics g) {
g.setColor(Color.WHITE);
if(rect.isSelected()) {
g.fillRect(10,10,150,50);
}
else if(oval.isSelected()) {
g.fillOval(10,10,150,50);
}
}
}
}
The errors I am getting are illegal start of expression and identifier expected. If I should approach this another way, please tell.
Well, the following is definitely not valid Java code:
The same applies to the following dRect, etc.
I'm not sure exactly what you're trying to achieve with that code. If it is to define a method called dLine, you would do the following instead:
I also noticed the following code, which is not currently causing you problems, but it will:
This is not capitalized properly, so it will not compile, and you're also not listening to any events, so it will never get called.
There are various other errors in the code, but this should get you started.
I think you need to go back to basics...
This won't work...
Component#setBackground
doesn't take aString
as a parameter, it takes aColor
All your
addItemListener
calls arn't going to work, because you've not implement aItemListener
I'm not sure what it is you hope to achieve by doing this...
But it won't work. @Override is used to indicate that a method was overridden by an ancestor, you are simply calling the method of
FigurePanel
Java, like C and C++ is case sensitive;
There is no such class
itemEvent
...it'sItemEvent
There is no such class
graphics
, it'sGraphics
And I'm not even going to try and guess what it is you were hoping to achieve with the following...
Java doesn't support "inline methods" (or what ever you want to call them) and no, making them methods would also not achieve what you are trying to do...
In fact the one thing you did very well, was to override
paintComponent
and callsuper.paintComponent
...well done :D !Updated
I would encourage you to have a read through...
Updated with possible running example