Hello experts!

I've made a GUI that calculates the surface area and volume of several shapes based on User input of discreet information such as length, width, depth, etc.

[IMG]http://i246.photobucket.com/albums/gg88/mustardy85/ScreenShotGUI.png[/IMG]
_______________________________________________________________________________________________________
How do I get java to draw the shapes of images in the BLUE rectangle to the right of the pane. So, if the user clicks on "Cube" it would get a jpeg/png/tiff image from say photobucket or another online server, and draw it on there?

Here is the code;

//GUI_4 - by Danish Khan
//======================================================
//Create a program to calculate area/volume of 2D and 3D 
//version of a shape (e.g. triangle and cone, circle and 
//sphere, etc).  There should be 2 buttons at the top 
//which indicate which version it is. 
//Save as “GUI_4.java”, and drop off.
//======================================================

//QUESTION 20 GRAPHIC USER INTERFACE - by Danish Khan
import java.awt.*; 
import java.applet.*; 
import java.awt.event.*;
import java.net.*;


//GRAPHIC USER INTERFACE START - CREATING ALL COMPONENTS
//==================================================================
public class GUI_4 extends Applet implements ActionListener{ 

  //Type, Name, new =, Type, (Parameters)   
  Button area = new Button("Calculate Area");
  Button volume = new Button("Calculate Volume");
  Button okButton2 = new Button("Refresh");
  //Shape Buttons
  Button rectangle = new Button("Square");
  Button triangle = new Button("Triangle");
  Button circle = new Button("Circle");
  Button prism = new Button("Prism");
  Button cone = new Button("Cone");
  Button sphere = new Button("Sphere");
  //Interaction Panes
  TextField x = new TextField("X");
  TextField y = new TextField("Y");
  TextField z = new TextField("Z");
  //Answer Pannel String
  String answerString = new String("The Answer is...");
  String shapeType = "Selected Shape Type";
  String areaOrVolume = "Selected Area or Selected Volume";
  String communication = "Information Required: Select a Shape";
  //Custom Colours
  Color custom;
  Color one = new Color(48,94,132);
  Color two = new Color(101,137,167);
  //g.fillRect(5,117,380,26);
  int a_x = 5;
  int a_y = 117;
  int a_w = 380;
  int a_h = 26;
  //Small Banners Above Calculate Area & Calculate Volume
  int cA_y = 38; 
  int cA_h = 2; 
  int cV_y = 38; 
  int cV_h = 2;
  //Manipulated Doubles & Strings used for calculation
  Double _X, _Y, _Z, answer;

  //PUBLIC VOID INITIALIZE - ADDING ALL COMPONENTS
  //==================================================================
  public void init(){
    //SetLayout(null)
    setLayout(null);
    //Setbounds (x,y,width,height)
    area.setBounds(10,40,185,20);
    volume.setBounds(205,40,185,20);
    okButton2.setBounds(10,65,380,20);
    
    //Shape Buttons
    rectangle.setBounds(10,120,90,20);
    triangle.setBounds(105,120,90,20);
    circle.setBounds(200,120,90,20);
    prism.setBounds(10,145,90,20);
    cone.setBounds(105,145,90,20);
    sphere.setBounds(200,145,90,20);
    //Information Input fields
    x.setBounds(25,225,90,20);
    y.setBounds(120,225,90,20);
    z.setBounds(215,225,90,20);
    
    //adding to GUI
    add(area);
    add(volume);
    add(okButton2); 
    add(rectangle);
    add(triangle);
    add(circle);
    add(prism);
    add(cone);
    add(sphere);
    add(x);
    add(y);
    add(z);
    // Attach actions to the components
    area.addActionListener(this);
    volume.addActionListener(this);
    okButton2.addActionListener(this);
    rectangle.addActionListener(this);
    triangle.addActionListener(this);
    circle.addActionListener(this);
    prism.addActionListener(this);
    cone.addActionListener(this);
    sphere.addActionListener(this);
    }//end void init
  
  
  //PUBLIC VOID PAINT - ALL GRAPHICS COMPONENTS ARE FOUND HERE
  //==================================================================
  public void paint(Graphics g){ 
    
    g.setColor(Color.black);
    //Draws a Standard Rectangle
    g.drawRect(5,5,390,290);
    g.drawString("Java Graphic User Interface - GUI_4 by Danish Khan",15,30);
    g.drawString(shapeType + " -> " + areaOrVolume,15,107);
    g.drawString(communication,15,190);

    //Colored Ontop of Area & Volume Buttons
    //area.setBounds(10,40,185,20);
    //volume.setBounds(205,40,185,20);
    g.setColor(one);
    g.fillRect(10,cA_y,185,cA_h);
    g.setColor(two);
    g.fillRect(205,cV_y,185,cV_h);
               
    //Manipulated Animation Draws
    g.setColor(custom);
    g.fillRect(300,117,85,83);
    g.fillRect(a_x,a_y,a_w,a_h);
    
    //Answer Pane
    g.setColor(Color.black);
    g.fillRect(15,200,370,80);
    g.setColor(Color.white);
    g.drawString(answerString,25,265);
    g.drawString("X-Value",25,220);
    g.drawString("Y-Value",120,220);
    g.drawString("Z-Value",215,220);

  }//end void Paint


  //PUBLIC VOID ACTIONPERFORMED - SPECIFY ACTION ACTIVATION HERE
  //==================================================================
    public void actionPerformed(ActionEvent evt){
      
      //=====[Area]=====\\
      if (evt.getSource() == area){
        //Graphical SetUps
        areaOrVolume = "Calculate Area";
        custom = new Color(48,94,132);
        cA_y = 36; cA_h = 4;
        cV_y = 38; cV_h = 2;
        
        //Shape Select Check
        if(shapeType.equals("Selected Shape Type")){
        answerString = "Please select a type of Shape!";}
        
        //Area Calculation for 2D Shapes
        else if(shapeType.equals("Rectangle")){
        _X = Double.parseDouble(x.getText());
        _Y = Double.parseDouble(y.getText());
        answer = _X * _Y;
        answerString = "Area of Rectangle: " + answer + "cm(squared)";}
        else if(shapeType.equals("Triangle")){
        _X = Double.parseDouble(x.getText());
        _Y = Double.parseDouble(y.getText());
        answer = (_X * _Y)/2;
        answerString = "Area of Triangle: " + answer + "cm(squared)";}
        else if(shapeType.equals("Circle")){
        _X = Double.parseDouble(x.getText());
        answer = Math.PI * Math.pow(_X,2);
        answerString = "Area of Circle: " + answer + "cm(squared)";}
        
        //Area Calculation for 3D Shapes
        else if(shapeType.equals("Prism")){
        _X = Double.parseDouble(x.getText());
        _Y = Double.parseDouble(y.getText());
        _Z = Double.parseDouble(z.getText());
        answer = 2*(_X*_Y + _X*_Z + _Y*_Z);
        answerString = "Surface Area of Prism: " + answer + "cm(squared)";}
        else if(shapeType.equals("Cone")){
        _X = Double.parseDouble(x.getText());
        _Y = Double.parseDouble(y.getText());
        _Z = Double.parseDouble(z.getText());
        answer = Math.PI*Math.pow(_X,2) + Math.PI*_Y*_Z;
        answerString = "Surface Area of Cone: " + answer + "cm(squared)";}
        else if(shapeType.equals("Sphere")){
        _X = Double.parseDouble(x.getText());
        answer = 4*Math.PI*Math.pow(_X,2);
        answerString = "Surface Area of Sphere: " + answer + "cm(squared)";}
        //Repaints at the very end
        repaint();}
        
      //=====[Volume]=====\\
      else if(evt.getSource() == volume){
        //Graphical SetUps
        areaOrVolume = "Calculate Volume";
        custom = new Color(101,137,167);
        cA_y = 38; cA_h = 2;
        cV_y = 36; cV_h = 4;
        
        //Volume Calculation for 2D Shapes
        if(shapeType.equals("Selected Shape Type")){
        answerString = "Please select a type of Shape!";}
        else if(shapeType.equals("Square")||
           shapeType.equals("Triangle")||
           shapeType.equals("Circle")){
           answerString = "Volume of a 2D Shape Cannot be Calculated";}
           
        else if(shapeType.equals("Prism")){
        _X = Double.parseDouble(x.getText());
        _Y = Double.parseDouble(y.getText());
        _Z = Double.parseDouble(z.getText());
        answer = _X*_Y*_Z;
        answerString = "Volume of Prism: " + answer + "cm(cubed)";}
        else if(shapeType.equals("Cone")){
        _X = Double.parseDouble(x.getText());
        _Y = Double.parseDouble(y.getText());
        answer = (Math.PI*Math.pow(_X,2)*_Y)/3;
        answerString = "Volume of Cone: " + answer + "cm(cubed)";}
        else if(shapeType.equals("Sphere")){
        _X = Double.parseDouble(x.getText());
        answer = (4*Math.PI*Math.pow(_X,2))/3;
        answerString = "Volume of Sphere: " + answer + "cm(cubed)";}
        repaint();}
      
      //2D-Shapes
      else if(evt.getSource() == rectangle){
      shapeType = "Rectangle";
      a_x = 5; a_y = 117; a_w = 380; a_h = 26;
      communication = "Enter(cm): X=Length, Y=Width";
      repaint();}
      else if(evt.getSource() == triangle){
      shapeType = "Triangle";
      a_x = 5; a_y = 117; a_w = 380; a_h = 26;
      communication = "Enter(cm): X=Height, Y=BaseWidth";
      repaint();}
      else if(evt.getSource() == circle){
      shapeType = "Circle";
      a_x = 5; a_y = 117; a_w = 380; a_h = 26;
      communication = "Enter(cm): X=Radius";
      repaint();}
      
      //3D-Shapes
      else if(evt.getSource() == prism){
      shapeType = "Prism";
      a_x = 5; a_y = 143; a_w = 380; a_h = 25;
      communication = "Enter(cm): X=Length, Y=Width, Z=Depth";
      repaint();}
      else if(evt.getSource() == cone){
      shapeType = "Cone";
      a_x = 5; a_y = 143; a_w = 380; a_h = 25;
      communication = "Enter(cm): X=Radius, Y=Height, Z=SideLength";
      repaint();}
      else if(evt.getSource() == sphere){
      shapeType = "Sphere";
      a_x = 5; a_y = 143; a_w = 380; a_h = 25;
      communication = "Enter(cm): X=Radius";
      repaint();}
      
      else if(evt.getSource() == okButton2){
      answerString = new String("The Answer is...");
      shapeType = "Selected Shape Type";
      areaOrVolume = "Selected Area or Selected Volume";
      communication = "Information Required: Select a Shape";
      x.setText(""); y.setText(""); z.setText("");
      a_x = 5; a_y = 117; a_w = 380; a_h = 26;
      cA_y = 38; cA_h = 2; cV_y = 38; cV_h = 2;
      
      repaint();}
      
    
  }//actionPerform end
}//end GUI

Thanks in Advance.

Recommended Answers

All 3 Replies

Are there two parts to your question?
1)draw the shapes of images in the BLUE rectangle to the right of the pane.
2)get a jpeg/png/tiff image from say photobucket or another online server, and draw it on there. There could be a problem here. Unsigned Applets are not allowed to go to any site except the one they were loaded from.

A comment: It's a very small area to draw in.

There is only one part to the question; How can I draw an Image in the blue rectangle to the right of the pane?

Alright, if it can't use another website, how can I get it to render it from my hard drive just for test purposes?

Look at the ImageIO class for methods to read an image file.
In an applet you'll want to use getResourceAsStream to get an inputstream for the image in a jar file and pass that to ImageIO

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.