My code so far is as follows. I am meant to clear the things inside the base traingle when i click on reset. But it clears the whole drawing panel. so i dont know how to get this work. :
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Panel;
import java.awt.GridBagLayout;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import javax.swing.JButton;
public class assignmentDraft_1 extends JFrame {
private static final long serialVersionUID = 1L;
private static final int TEXTFIELD_WIDTH = 35;
private static final int TEXTFIELD_HEIGHT = 20;
private static final int BASEFLAG_WIDTH = 240;
private static final int BASEFLAG_HEIGHT = 120;
private JPanel jContentPane = null; // @jve:decl-index=0:visual-constraint="39,10"
private JLabel jLabel_SelectShape = null;
private JLabel jLabel_Bar = null;
private JLabel jLabel_Circle = null;
private JRadioButton jRadioButton_Bar = null;
private JRadioButton jRadioButton_Circle = null;
private ButtonGroup elementGroup = new ButtonGroup(); // @jve:decl-index=0:
private ButtonGroup orientationGroup = new ButtonGroup ();// @jve:decl-index=0:
private ButtonGroup colourGroup = new ButtonGroup ();
private JLabel jLabel_BarThickness = null;
private JTextField jTextField_Bar_Thickness = null;
private JLabel jLabel_distanceLeft = null;
private JTextField jTextField_Distance = null;
private JLabel jLabel_HeightRight = null;
private JLabel jLabel_WidthRight = null;
private JTextField jTextField_HeightRight = null;
private JTextField jTextField_WidthRight = null;
private JLabel jLabel_XdistanceRight = null;
private JLabel jLabel_YdistanceRight = null;
private JTextField jTextField_XdistanceRight = null;
private JTextField jTextField_YdistanceRight = null;
int iX, iY, iWidth, iHeight, iHeightRectangle, iDistanceRectangle;
private JLabel jLabel_Orientation = null;
private JRadioButton jRadioButton_Horizontal = null;
private JRadioButton jRadioButton_Vertical = null;
private JLabel jLabel_Horizontal = null;
private JLabel jLabel_Vertical = null;
private myJPanel jPanel_DrawingArea = null;
//Declare a BufferedImage and its corresponding Graphics2D context object
private BufferedImage img;
private Graphics2D g2dImg; // @jve:decl-index=0:
private JButton jButton_Draw = null;
private JButton jButton_Reset = null;
private JButton jButton_Quit = null;
private JLabel jLabel_Color = null;
private JRadioButton jRadioButton_Red = null;
private JRadioButton jRadioButton_Green = null;
private JRadioButton jRadioButton_Blue = null;
private JLabel jLabel_Red = null;
private JLabel jLabel_Green = null;
private JLabel jLabel_Blue = null;
private Color drawingColour;
/**
* This method initializes jRadioButton_Bar
*
* @return javax.swing.JRadioButton
*/
private JRadioButton getJRadioButton_Bar() {
if (jRadioButton_Bar == null) {
jRadioButton_Bar = new JRadioButton();
jRadioButton_Bar.setBounds(new Rectangle(14, 51, 21, 21));
jRadioButton_Bar.setSelected(true);
}
return jRadioButton_Bar;
}
/**
* This method initializes jRadioButton_Circle
*
* @return javax.swing.JRadioButton
*/
private JRadioButton getJRadioButton_Circle() {
if (jRadioButton_Circle == null) {
jRadioButton_Circle = new JRadioButton();
jRadioButton_Circle.setBounds(new Rectangle(107, 46, 21, 21));
}
return jRadioButton_Circle;
}
/**
* This method initializes jTextField_Bar_Thickness
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField_Bar_Thickness() {
if (jTextField_Bar_Thickness == null) {
jTextField_Bar_Thickness = new JTextField();
jTextField_Bar_Thickness.setBounds(new Rectangle(101, 80, 35, 20));
jTextField_Bar_Thickness.setText("40");
}
return jTextField_Bar_Thickness;
}
/**
* This method initializes jTextField_Distance
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField_Distance() {
if (jTextField_Distance == null) {
jTextField_Distance = new JTextField();
jTextField_Distance.setBounds(new Rectangle(99, 111, 35, 20));
jTextField_Distance.setText("40");
}
return jTextField_Distance;
}
/**
* This method initializes jTextField_HeightRight
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField_HeightRight() {
if (jTextField_HeightRight == null) {
jTextField_HeightRight = new JTextField();
jTextField_HeightRight.setBounds(new Rectangle(234, 78, 35, 20));
jTextField_HeightRight.setText("50");
jTextField_HeightRight.setText(Integer.toString(BA SEFLAG_WIDTH/3));
}
return jTextField_HeightRight;
}
/**
* This method initializes jTextField_WidthRight
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField_WidthRight() {
if (jTextField_WidthRight == null) {
jTextField_WidthRight = new JTextField();
jTextField_WidthRight.setBounds(new Rectangle(235, 108, 35, 20));
//jTextField_WidthRight.setText("100");
jTextField_WidthRight.setText(Integer.toString(BAS EFLAG_HEIGHT/3));
}
return jTextField_WidthRight;
}
/**
* This method initializes jTextField_XdistanceRight
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField_XdistanceRight() {
if (jTextField_XdistanceRight == null) {
jTextField_XdistanceRight = new JTextField();
jTextField_XdistanceRight.setBounds(new Rectangle(236, 138, TEXTFIELD_WIDTH, TEXTFIELD_HEIGHT));
jTextField_XdistanceRight.setText("10");
}
return jTextField_XdistanceRight;
}
/**
* This method initializes jTextField_YdistanceRight
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField_YdistanceRight() {
if (jTextField_YdistanceRight == null) {
jTextField_YdistanceRight = new JTextField();
jTextField_YdistanceRight.setBounds(new Rectangle(237, 166, 35, 20));
jTextField_YdistanceRight.setText("20");
}
return jTextField_YdistanceRight;
}
/**
* This method initializes jRadioButton_Horizontal
*
* @return javax.swing.JRadioButton
*/
private JRadioButton getJRadioButton_Horizontal() {
if (jRadioButton_Horizontal == null) {
jRadioButton_Horizontal = new JRadioButton();
jRadioButton_Horizontal.setBounds(new Rectangle(20, 221, 21, 21));
jRadioButton_Horizontal.setSelected(true);
jRadioButton_Horizontal.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
jLabel_distanceLeft.setText("Y Distance");
jLabel_BarThickness.setText("Height");
jTextField_Bar_Thickness.setText(Integer.toString( BASEFLAG_HEIGHT/3));
jTextField_Distance.setText(Integer.toString(BASEF LAG_HEIGHT/3));
}
});
}
return jRadioButton_Horizontal;
}
/**
* This method initializes jRadioButton_Vertical
*
* @return javax.swing.JRadioButton
*/
private JRadioButton getJRadioButton_Vertical() {
if (jRadioButton_Vertical == null) {
jRadioButton_Vertical = new JRadioButton();
jRadioButton_Vertical.setBounds(new Rectangle(15, 244, 21, 21));
jRadioButton_Vertical.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
jLabel_distanceLeft.setText("X Distance");
jLabel_BarThickness.setText("Width");
jTextField_Bar_Thickness.setText(Integer.toString( BASEFLAG_WIDTH/3));
jTextField_Distance.setText(Integer.toString(BASEF LAG_WIDTH/3));
}
});
}
return jRadioButton_Vertical;
}
/**
* This method initializes jPanel_DrawingArea
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel_DrawingArea() {
if (jPanel_DrawingArea == null) {
jPanel_DrawingArea = new myJPanel();
jPanel_DrawingArea.setLayout(new GridBagLayout());
jPanel_DrawingArea.setBounds(new Rectangle(345, 29, 300, 200));
jPanel_DrawingArea.setBackground(Color.white);
jPanel_DrawingArea.setEnabled(false);
//Instantiate the BufferedImage object and give it the same width
// and height as that of the drawing area JPanel
//img = new BufferedImage(jPanel_DrawingArea.getWidth(),
//jPanel_DrawingArea.getHeight(),
// BufferedImage.TYPE_INT_RGB);
//Get its graphics context. A graphics context of a particular object allows us to draw on it.
//g2dImg = (Graphics2D)img.getGraphics();
//g2dImg.setPaint(Color.WHITE);
//g2dImg.fill(new Rectangle2D.Double(0, 0, img.getWidth(), img.getHeight()));
}
return jPanel_DrawingArea;
}
/**
* This method initializes jButton_Draw
*
* @return javax.swing.JButton
*/
private JButton getJButton_Draw() {
if (jButton_Draw == null) {
jButton_Draw = new JButton();
jButton_Draw.setBounds(new Rectangle(255, 255, 75, 31));
jButton_Draw.setText("Draw");
jButton_Draw.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
// Get values from the text fields
iX = Integer.parseInt(jTextField_XdistanceRight.getText ());
iY = Integer.parseInt(jTextField_YdistanceRight.getText ());
iWidth = Integer.parseInt(jTextField_WidthRight.getText());
iHeight = Integer.parseInt(jTextField_HeightRight.getText()) ;
iHeightRectangle = Integer.parseInt(jTextField_Bar_Thickness.getText( ));
iDistanceRectangle = Integer.parseInt(jTextField_Distance.getText());
jPanel_DrawingArea.repaint();
}
});
}
return jButton_Draw;
}
/**
* This method initializes jButton_Reset
*
* @return javax.swing.JButton
*/
private JButton getJButton_Reset() {
if (jButton_Reset == null) {
jButton_Reset = new JButton();
jButton_Reset.setBounds(new Rectangle(370, 249, 91, 32));
jButton_Reset.setText("Reset");
jButton_Reset.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
jPanel_DrawingArea.clear(jPanel_DrawingArea.getGra phics());
}
});
}
return jButton_Reset;
}
/**
* This method initializes jButton_Quit
*
* @return javax.swing.JButton
*/
private JButton getJButton_Quit() {
if (jButton_Quit == null) {
jButton_Quit = new JButton();
jButton_Quit.setBounds(new Rectangle(492, 257, 84, 33));
jButton_Quit.setText("Quit");
jButton_Quit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.exit(0);
}
});
}
return jButton_Quit;
}
/**
* This method initializes jRadioButton_Red
*
* @return javax.swing.JRadioButton
*/
private JRadioButton getJRadioButton_Red() {
if (jRadioButton_Red == null) {
jRadioButton_Red = new JRadioButton();
jRadioButton_Red.setBounds(new Rectangle(19, 303, 21, 21));
}
return jRadioButton_Red;
}
/**
* This method initializes jRadioButton_Green
*
* @return javax.swing.JRadioButton
*/
private JRadioButton getJRadioButton_Green() {
if (jRadioButton_Green == null) {
jRadioButton_Green = new JRadioButton();
jRadioButton_Green.setBounds(new Rectangle(15, 324, 21, 21));
jRadioButton_Green.setSelected(true);
}
return jRadioButton_Green;
}
/**
* This method initializes jRadioButton_Blue
*
* @return javax.swing.JRadioButton
*/
private JRadioButton getJRadioButton_Blue() {
if (jRadioButton_Blue == null) {
jRadioButton_Blue = new JRadioButton();
jRadioButton_Blue.setBounds(new Rectangle(13, 352, 21, 21));
}
return jRadioButton_Blue;
}
/**
* @param args
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
assignmentDraft_1 thisClass = new assignmentDraft_1();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_ CLOSE);
thisClass.setVisible(true);
}
});
}
/**
* This is the default constructor
*/
public assignmentDraft_1() {
super(); // Call to the parent class JFrame to starta window.
initialize(); // Call to initialise
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(672, 416);
this.setContentPane(getJContentPane());
this.setTitle("Java Flags A - u3036830");
//initialize button groups
elementGroup.add(jRadioButton_Bar);
elementGroup.add(jRadioButton_Circle);
orientationGroup.add(jRadioButton_Horizontal);
orientationGroup.add(jRadioButton_Vertical);
colourGroup.add(jRadioButton_Red);
colourGroup.add(jRadioButton_Green);
colourGroup.add(jRadioButton_Blue);
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel_Blue = new JLabel();
jLabel_Blue.setBounds(new Rectangle(42, 356, 37, 16));
jLabel_Blue.setText("Blue");
jLabel_Green = new JLabel();
jLabel_Green.setBounds(new Rectangle(41, 330, 40, 18));
jLabel_Green.setText("Green");
jLabel_Red = new JLabel();
jLabel_Red.setBounds(new Rectangle(42, 307, 28, 17));
jLabel_Red.setText("Red");
jLabel_Color = new JLabel();
jLabel_Color.setBounds(new Rectangle(15, 272, 47, 24));
jLabel_Color.setText("Colour");
jLabel_Vertical = new JLabel();
jLabel_Vertical.setBounds(new Rectangle(46, 245, 52, 16));
jLabel_Vertical.setText("Vertical");
jLabel_Horizontal = new JLabel();
jLabel_Horizontal.setBounds(new Rectangle(46, 224, 65, 16));
jLabel_Horizontal.setText("Horizontal");
jLabel_Orientation = new JLabel();
jLabel_Orientation.setBounds(new Rectangle(19, 187, 68, 21));
jLabel_Orientation.setText("Orientation");
jLabel_YdistanceRight = new JLabel();
jLabel_YdistanceRight.setBounds(new Rectangle(180, 163, 47, 26));
jLabel_YdistanceRight.setText("Y Distance");
jLabel_XdistanceRight = new JLabel();
jLabel_XdistanceRight.setBounds(new Rectangle(178, 136, 45, 21));
jLabel_XdistanceRight.setText("X Distance");
jLabel_WidthRight = new JLabel();
jLabel_WidthRight.setBounds(new Rectangle(180, 112, 44, 19));
jLabel_WidthRight.setText("Width");
jLabel_HeightRight = new JLabel();
jLabel_HeightRight.setBounds(new Rectangle(180, 76, 48, 26));
jLabel_HeightRight.setText("Height");
jLabel_distanceLeft = new JLabel();
jLabel_distanceLeft.setBounds(new Rectangle(18, 110, 70, 25));
jLabel_distanceLeft.setText("Y Distance" +
"");
jLabel_BarThickness = new JLabel();
jLabel_BarThickness.setBounds(new Rectangle(17, 78, 50, 24));
jLabel_BarThickness.setText("Height");
jLabel_Circle = new JLabel();
jLabel_Circle.setBounds(new Rectangle(136, 46, 74, 23));
jLabel_Circle.setText("Add a Circle");
jLabel_Bar = new JLabel();
jLabel_Bar.setBounds(new Rectangle(38, 51, 60, 19));
jLabel_Bar.setText("Add a Bar");
jLabel_SelectShape = new JLabel();
jLabel_SelectShape.setBounds(new Rectangle(12, 18, 274, 22));
jLabel_SelectShape.setText("Select the Shape you want to add to the Flag");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.setSize(new Dimension(636, 397));
jContentPane.add(jLabel_SelectShape, null);
jContentPane.add(jLabel_Bar, null);
jContentPane.add(jLabel_Circle, null);
jContentPane.add(getJRadioButton_Bar(), null);
jContentPane.add(getJRadioButton_Circle(), null);
jContentPane.add(jLabel_BarThickness, null);
jContentPane.add(getJTextField_Bar_Thickness(), null);
jContentPane.add(jLabel_distanceLeft, null);
jContentPane.add(getJTextField_Distance(), null);
jContentPane.add(jLabel_HeightRight, null);
jContentPane.add(jLabel_WidthRight, null);
jContentPane.add(getJTextField_HeightRight(), null);
jContentPane.add(getJTextField_WidthRight(), null);
jContentPane.add(jLabel_XdistanceRight, null);
jContentPane.add(jLabel_YdistanceRight, null);
jContentPane.add(getJTextField_XdistanceRight(), null);
jContentPane.add(getJTextField_YdistanceRight(), null);
jContentPane.add(jLabel_Orientation, null);
jContentPane.add(getJRadioButton_Horizontal(), null);
jContentPane.add(getJRadioButton_Vertical(), null);
jContentPane.add(jLabel_Horizontal, null);
jContentPane.add(jLabel_Vertical, null);
jContentPane.add(getJPanel_DrawingArea(), null);
jContentPane.add(getJButton_Draw(), null);
jContentPane.add(getJButton_Reset(), null);
jContentPane.add(getJButton_Quit(), null);
jContentPane.add(jLabel_Color, null);
jContentPane.add(getJRadioButton_Red(), null);
jContentPane.add(getJRadioButton_Green(), null);
jContentPane.add(getJRadioButton_Blue(), null);
jContentPane.add(jLabel_Red, null);
jContentPane.add(jLabel_Green, null);
jContentPane.add(jLabel_Blue, null);
}
return jContentPane;
}
class myJPanel extends JPanel
{
// Declare and instantiate an ellipse and rectangle object of
// dimension zero. We will later just change their dimension
// and location for efficiency.
private Ellipse2D.Double ellipse;
private Rectangle2D.Double rectangleHeight;
private Rectangle2D.Double rectangleWidth;
private Rectangle2D.Double baserectangle;
/**
* Override paintComnponent method with our draw commands
*
*/
public void paintComponent(Graphics g)
{
//Must be called to draw the JPanel control.
// As a side effect, it also clears it.
//super.paintComponent(g);
// Change the dimension and location of the ellipse and rectangle
// obtained from the user interface.
ellipse = new Ellipse2D.Double(iX, iY, iWidth, iHeight);
rectangleHeight = new Rectangle2D.Double(11, iDistanceRectangle, 239, iHeightRectangle);
rectangleWidth = new Rectangle2D.Double(iDistanceRectangle,11,iHeightRe ctangle,119);
Graphics2D g2d = (Graphics2D)g;
baserectangle = new Rectangle2D.Double(10, 10, BASEFLAG_WIDTH, BASEFLAG_HEIGHT);
g2d.draw(baserectangle);
if (jRadioButton_Red.isSelected())
{ drawingColour = Color.red;
g2d.setPaint(Color.red);
}
else if (jRadioButton_Green.isSelected())
{ drawingColour = Color.green;
g2d.setPaint(Color.green);
}
else if (jRadioButton_Blue.isSelected())
{ drawingColour = Color.blue;
g2d.setPaint(Color.blue);
}
//if (jRadioButton_Bar.isSelected())
//{
//if (jRadioButton_Horizontal.isSelected())
//g2d.fill(rectangleHeight);
//else if (jRadioButton_Vertical.isSelected())
//g2d.fill(rectangleWidth);
//} else if (jRadioButton_Circle.isSelected())
//g2d.fill(ellipse);
if (jRadioButton_Circle.isSelected())
g2d.fill(ellipse);
else if (jRadioButton_Bar.isSelected()&& jRadioButton_Horizontal.isSelected())
g2d.fill(rectangleHeight);
else if (jRadioButton_Bar.isSelected()&& jRadioButton_Vertical.isSelected())
g2d.fill(rectangleWidth);
else if (jButton_Reset.isSelected())
g2d.draw(baserectangle);
//if (jRadioButton_Bar.isSelected())
//g2d.draw(baserectangle);
//else if (jRadioButton_Circle.isSelected())
//g2d.fill(ellipse);
}
protected void clear(Graphics g) {
super.paintComponent(g);
// Also clear the BufferedImage object by drawing a white coloured filled rectangle all over.
//g2d.setPaint(Color.WHITE);
//g2d.fill(new Rectangle2D.Double(0, 0, img.getWidth(), img.getHeight()));
}
}
} // @jve:decl-index=0:visual-constraint="74,-4"