Write a GUI program that draws a rectangle andor an oval as
Write a GUI program that draws a rectangle and/or an oval, as shown in the figures below. The user selects a figure from a radio button and specifies whether it is filled by selecting a check button. A regular button is used to clear all drawings on the canvas. When the Rectangle radio button is clicked, the program checks if the Filled check button is checked to decide whether or not the rectangle should be filled. It then draws a rectangle with top_left corner at (50,50) and bottom_right corner at (250,250). When the oval radio button is clicked, the program checks if the Filled check button is checked to decide whether or not the oval should be filled. It then draws an oval \"bounded by an imaginary rectangle\" with corners at (50,100) and (250, 200). Use red fill color for the rectangle and yellow fill color for the oval. When the Clear button is clicked, all drawing on the canvas will be cleared and all buttons will be unchecked.
Use nested frames to achieve the shown layout where all buttons are added to a pane/frame that is added to the main frame.
from tkinter import *
class Canvas1(Frame):
def __init__(self):
\"\"\"Sets up the window and widgets.\"\"\"
Frame.__init__(self)
self.master.title(\"GUIs drawing geometric shapes\")
self.grid()
#Create a canvas and place in this frame
self.canvas = Canvas(self, width = 300, height = 300, bg = \"white\")
self.canvas.grid(row = 0, column = 0)
#Place buttons in a frame
frame = Frame(self)
frame.grid(row = 1, column = 0)
rectangle = Radiobutton(frame, text = \"Rectangle\", command = self.displayRect)
oval = Radiobutton(frame, text = \"Oval\", command = self.displayOval)
filled = Checkbutton(frame, text = \"Filled\", command = self.displayFilled)
clear = Button(frame, text = \"Clear\", command = self.clearCanvas)
rectangle.grid(row = 0, column = 0)
oval.grid(row = 0, column = 1)
filled.grid(row = 0, column = 2)
clear.grid(row = 0, column = 3)
def displayRect(self):
self.canvas.delete(\"rect\", \"oval\")
self.canvas.create_rectangle(50, 50, 250, 250, tags = \"rect\")
def displayOval(self):
self.canvas.delete(\"rect\", \"oval\")
self.canvas.create_oval(50, 100, 250, 200, tags = \"oval\")
def displayFilled(self):
def clearCanvas(self):
self.canvas.delete(\"rect\", \"oval\", \"filled\")
def main():
Canvas1().mainloop()
main()
Solution
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class DrawShapes {
public static void main(String[] args) {
new DrawShapes();
}
public DrawShapes() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (InstantiationException ex) {
Logger.getLogger(DrawShapes.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(DrawShapes.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedLookAndFeelException ex) {
Logger.getLogger(DrawShapes.class.getName()).log(Level.SEVERE, null, ex);
}
} catch(ClassNotFoundException ex)
{
}
JFrame frame = new JFrame(\"Testing\");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new DrawPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class DrawPane extends JPanel {
public DrawPane() {
setLayout(new BorderLayout());
RenderPane rp = new RenderPane();
add(new ControlsPane(rp), BorderLayout.NORTH);
add(rp);
}
}
public class ControlsPane extends JPanel {
public ControlsPane(RenderPane rp) {
JRadioButton[] btns = new JRadioButton[4];
btns[0] = new JRadioButton(new LineAction(rp));
btns[1] = new JRadioButton(new RectangleAction(rp));
btns[2] = new JRadioButton(new OvalAction(rp));
btns[3] = new JRadioButton(new ClearAction(rp));
ButtonGroup bg = new ButtonGroup();
for (JRadioButton btn : btns) {
bg.add(btn);
add(btn);
}
}
}
public class RenderPane extends JPanel {
private Shape shape;
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
public void setShape(Shape shape) {
this.shape = shape;
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
if (shape != null) {
g2d.setColor(Color.RED);
g2d.draw(shape);
}
g2d.dispose();
}
}
public class LineAction extends AbstractRenderAction {
public LineAction(RenderPane renderPane) {
super(renderPane);
putValue(NAME, \"Line\");
}
@Override
public Shape getShape() {
return new Line2D.Float(0f, 0f, getRenderPane().getWidth(), getRenderPane().getHeight());
}
}
public class RectangleAction extends AbstractRenderAction {
public RectangleAction(RenderPane renderPane) {
super(renderPane);
putValue(NAME, \"Rectangle\");
}
@Override
public Shape getShape() {
return new Rectangle2D.Float(10, 10, getRenderPane().getWidth() - 20, getRenderPane().getHeight() - 20);
}
}
public class OvalAction extends AbstractRenderAction {
public OvalAction(RenderPane renderPane) {
super(renderPane);
putValue(NAME, \"Oval\");
}
@Override
public Shape getShape() {
float radius = Math.min(getRenderPane().getWidth() - 20, getRenderPane().getHeight() - 20);
return new Ellipse2D.Float(10, 10, radius, radius);
}
}
public class ClearAction extends AbstractRenderAction {
public ClearAction(RenderPane renderPane) {
super(renderPane);
putValue(NAME, \"Clear\");
}
@Override
public Shape getShape() {
return null;
}
}
public abstract class AbstractRenderAction extends AbstractAction {
private RenderPane renderPane;
public AbstractRenderAction(RenderPane renderPane) {
this.renderPane = renderPane;
}
public RenderPane getRenderPane() {
return renderPane;
}
public abstract Shape getShape();
@Override
public void actionPerformed(ActionEvent e) {
getRenderPane().setShape(getShape());
}
}
}




