Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

I need help with this project. I am having trouble making the graphics part of the code to work. The program should draw an oval

I need help with this project. I am having trouble making the graphics part of the code to work. The program should draw an oval or rectangle depending on the options that you give it. I have finished the GUI but i cannot make the graphics part work. I will post it the codes where the program runs but does not "Draw". I would like to ask for help on how to complete my program. thank you.This is in swing.

//Shape.java

import java.awt.Rectangle; import java.awt.Graphics; public abstract class Shape extends Rectangle{

public String color; public boolean solidOrHollow; public static int numOfShapes=0; public Shape( Rectangle rect, String shapeColor, String fillType){ numOfShapes++; } public void setColor(String shapeColor){ this.color=shapeColor; } public boolean getSolid(){ return this.solidOrHollow; } public void setSolid(boolean solidOrHollow){ this.solidOrHollow=solidOrHollow; } public static int getNoOfShapes(){ return numOfShapes; } public abstract void draw(Graphics graphics); }

------------------------------------------------------------------

//OutsideBounds.java

public class OutsideBounds extends Exception{ public static void throwOutsideBounds(Exception e) throws Exception{ throw e; } }

------------------------------------------------------------------

//Rectangular.java

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; } @Override public void draw(Graphics graphics){ graphics.drawRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height); } }

-------------------------------------------------------------------

Oval.java

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; } @Override public void draw(Graphics graphics){ graphics.drawRect(ova.x, ova.y, ova.width, ova.height); } }

-------------------------------------------------------------------

//Drawing.java

import java.awt.Dimension; import java.awt.Graphics; import java.awt.Rectangle; import javax.swing.JPanel;

public class Drawing extends JPanel{ private String shape; private Dimension dmn; private Rectangular rectangular; private Oval oval; String currentShape; private Graphics g ;

public void paintComponent (Graphics g){ super.paintComponent(g); if (currentShape=="Rectangle"){ rectangular.draw(g); } else{ oval.draw(g); } }

@Override public Dimension getPreferredSize (){ dmn.height=200; dmn.width=200; return dmn; }

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; currentShape=shapetoDraw; if (shapetoDraw=="Rectangle"){ rectangular= new Rectangular(rect,shapeColor,fillType); } else{ oval= new Oval(rect,shapeColor,fillType); } repaint(); } }

-------------------------------------------------------------------------

//Project3.java

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(700,450); setLocationRelativeTo (null); left = new JPanel(new GridLayout(7,2,5,5)); right = new JPanel(null); 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); JComboBox shapeCombo = new JComboBox(); shapeCombo.addItem("Rectangle"); shapeCombo.addItem("Oval"); 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"); JComboBox filltypeCombo = new JComboBox(); filltypeCombo.addItem("Hollow"); filltypeCombo.addItem("Solid"); left.add(shapeType); left.add(shapeCombo); left.add(color); left.add(colorCombo); left.add(fillType); left.add(filltypeCombo); 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);

left.setBounds(10,30,300,300); right.setBounds(350,30,300,300); down.setBounds(280,350,80,50); add(left, BorderLayout.LINE_START); add(right, BorderLayout.LINE_END); add(down, BorderLayout.PAGE_END);

}

public static void main(String[] args) { Project3 mainFrame = new Project3(); mainFrame.setVisible(true); mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

image text in transcribed

This is the current output.

Let's Draw Shapes! - O X Shape Drawing Shape Type Rectangle Color Black Fill Type Hollow Width Height x coordinate y coordinate Draw Let's Draw Shapes! - O X Shape Drawing Shape Type Rectangle Color Black Fill Type Hollow Width Height x coordinate y coordinate Draw

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions