Question
Guys I need help. This is the code I made for my project. This is only the main i have 6 other java classes but
Guys I need help. This is the code I made for my project. This is only the main i have 6 other java classes but I need help on this part. I will post an image of the output. It does not show anything. I have no idea how to make it work. This is java swing. Thanks.
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
public class Drawing extends javax.swing.JPanel{
private String shape;
private Dimension dimens;
private Rectangular rectangular;
private Oval oval;
String shapetoDraw1;
private Graphics g ;
@Override
public void paintComponent (Graphics g){
super.paint(g);
if (shapetoDraw1=="Rectangle"){
rectangular.draw(g);
}
else{
oval.draw(g);
}
}
public Dimension getPreferredSize (){
dimens.height=200;
dimens.width=200;
return dimens;
}
public void drawShape(String shapetoDraw, String shapeColor, String fillType){
Rectangle rect=new Rectangle();
rect.x=10;
rect.y=10;
rect.height=50;
rect.width=50;
shapetoDraw1=shapetoDraw;
if (shapetoDraw=="Rectangle"){
rectangular= new Rectangular(rect,shapeColor,fillType);
}
else{
oval= new Oval(rect,shapeColor,fillType);
}
repaint();
}
}
------------------------------------------
public class OutsideBounds extends Exception{
public static void throwOutsideBounds(Exception e) throws Exception{
throw e;
}
}
-------------------------------------------
import java.awt.Rectangle;
import java.awt.Graphics;
public abstract class Shape extends Rectangle{
public String color;
public boolean isHollow;
public static int noOfShapes=0;
public Shape( Rectangle rect, String shapeColor, String fillType){
noOfShapes++;
}
public void setColor(String shapeColor){
this.color=shapeColor;
}
public void setSolid(boolean isShapeHollow){
this.isHollow=isShapeHollow;
}
public static int getNoOfShapes(){
return noOfShapes;
}
public abstract void draw(Graphics graphics);
}
-------------------------------------------------
import java.awt.Graphics;
import java.awt.Rectangle;
public class Rectangular extends Shape{
Rectangle rectangle;
public Rectangular( Rectangle rect,String shapeColor, String fillType){
super(rect,shapeColor,fillType);
rectangle=rect;
}
public void draw(Graphics graphics){
graphics.drawRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
}
}
----------------------------------------
import java.awt.Graphics;
import java.awt.Rectangle;
public class Oval extends Shape{
Rectangle ova;
public Oval( Rectangle rect,String shapeColor, String fillType){
super(rect,shapeColor,fillType);
ova=rect;
}
public void draw(Graphics graphics){
graphics.drawRect(ova.x, ova.y, ova.width, ova.height);
}
}
----------------------------------------
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Project3 extends JFrame{
private JLabel shapeType, fillType, color, Width, Height, xCoordinate, yCoordinate;
private JTextField xCoor, yCoor, jWidth, jHeight;
private JPanel left,right,down;
private String shapetoDraw, shapeColor, filltype;
private Rectangular rectangular;
private Oval oval;
private Drawing drawing= new Drawing();
Project3(){
setTitle("Let's Draw Shapes!");
setLayout(null);
setSize(600,500);
left = new JPanel(new GridLayout(2,2,10,10));
right = new JPanel();
right.setBorder(BorderFactory.createTitledBorder("Shape Drawing"));
down = new JPanel(new GridLayout(2,2,10,10));
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
shapeType = new JLabel("Shape Type");
fillType = new JLabel("Fill Type");
color = new JLabel("Color");
Width = new JLabel("Width");
Height = new JLabel("Height");
xCoordinate = new JLabel("x coordinate");
yCoordinate = new JLabel("y coordinate");
xCoor = new JTextField(10);
yCoor = new JTextField(10);
jWidth = new JTextField(10);
jHeight = new JTextField(10);
left.add(shapeType);
JComboBox shapeCombo = new JComboBox();
shapeCombo.addItem("Rectangle");
shapeCombo.addItem("Oval");
left.add(color);
JComboBox colorCombo = new JComboBox();
colorCombo.addItem("Black");
colorCombo.addItem("Red");
colorCombo.addItem("Orange");
colorCombo.addItem("Yellow");
colorCombo.addItem("Green");
colorCombo.addItem("Blue");
colorCombo.addItem("Magenta");
left.add(fillType);
JComboBox filltypeCombo = new JComboBox();
filltypeCombo.addItem("Hollow");
filltypeCombo.addItem("Solid");
left.add(Width);
left.add(jWidth);
left.add(Height);
left.add(jHeight);
left.add(xCoordinate);
left.add(xCoor);
left.add(yCoordinate);
left.add(yCoor);
JButton drawButton = new JButton("Draw");
drawButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
shapetoDraw=shapeCombo.getSelectedItem().toString();
shapeColor=colorCombo.getSelectedItem().toString();
filltype=filltypeCombo.getSelectedItem().toString();
drawing.drawShape(shapetoDraw, shapeColor, filltype);
}
});
down.add(drawButton);
}
public static void main(String[] args) {
Project3 mainFrame = new Project3();
mainFrame.setVisible(true);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void paint(Graphics g) {
super.paint(g);
}
}
The required outcome is supposed to look like this:
I want to at least see something with my code but nothing is showing.
2 Let's Draw Shapes! - 0 X Geometric Drawing - 0 X Shape Drawing Shape Type Oval Solid Fill Type Color Red Width 100 Height x coordinate y coordinate Draw 2 Let's Draw Shapes! - 0 X Geometric Drawing - 0 X Shape Drawing Shape Type Oval Solid Fill Type Color Red Width 100 Height x coordinate y coordinate DrawStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started