niceman
niceman

Reputation: 2673

drawing on applet without extending Applet class

import java.util.InputMismatchException;    
import java.util.Scanner;
class Rectangle extends Shape
{
private double width,height;

Rectangle(double w,double h){
    super();
    width=w;
    height=h;
}
@Override
public void draw() {

}
}
class MyClass
{
    public static void main(String[] args)
    {
        try
        {        
            Scanner c=new Scanner(System.in); 
            String st="";
            System.out.println("enter the number of Shapes you want to add(0 will exit)");
            byte s=c.nextByte();
            if(s==0)
                return;
            Shape[] shapes=new Shape[s];
            for(byte j=0;j<s;j++)
            {                                      
               System.out.println("what kind of shapes do you want , r for Rectangle , c for Circle");                    
               st=c.next();
               if("c".equals(st))
               {
                   System.out.println("enter the radius and then the Name please");
                   shapes[j]=new Circle(c.nextDouble());
                   shapes[j].setName(c.next());    
                   break;
               }
               else if("r".equals(st))
               {
                   System.out.println("enter the width and height and then the Name please");
                   shapes[j]=new Rectangle(c.nextDouble(),c.nextDouble());
                   shapes[j].setName(c.next());
                   break;
               }
               else
               {
                   System.out.println("c or r please");
                   j--;
               }                   
            }  
            DrawShapes(shapes);                             
        }
        catch(InputMismatchException i)
        {
            System.out.println("a decimal number was expected , but text was found , so we will start from begining");
        }
        catch(Throwable e)
        {
            System.out.println(e.getMessage());
        }    
    }
    public static void DrawShapes(Shape[] shapes)
    {
       for(int i=0;i<shapes.length;i++)
         shapes[i].draw();
    }
}

explanation:

the shape class is an abstract class which contains getPerimeter , getArea, and draw abstract methods, The draw method of the Rectangle class should draw the rectangle in a window.

Goal

draw the rectangle on a separate window while keeping my application a console one for the other methods.

Upvotes: 0

Views: 379

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

Suggestions:

  • Use the Swing GUI library, not AWT.
  • You will want to draw in a class that extends JPanel
  • Override the JPanel's protected void paintComponent(Graphics g) method, and do your drawing inside of this method.
  • Be sure to call the super's method: super.paintComponent(g) inside of your override.
  • Then display the JPanel inside of whatever top-level Swing window that you desire, but it a JApplet, JFrame, JDialog, or inside of another JPanel if this is required. You can add this drawing JPanel to your applet in the JApplet's init() method simply via add(myDrawingJPanel);

Regarding:

is it possible to use applet without extending Applet class?

No I don't think so. If you want to create a GUI and display it inside of a JApplet or Applet, then you must override the class, and in particular override its init() method, and add your GUI to the applet there.

For more specific help, consider providing more information and code with your question.


Edit
Regarding your edit:

this is a console application and I want to keep it like that, but I also want to draw the rectangle when calling draw method, the shape class isn't needed, it's just an abstract class and doesn't have implemented methods. in summery I want to use an applet to draw the rectangle while keeping my application a console one(except drawing).any help ?

Again, if you want to draw something and display it, you need a GUI and so might as well create and display a stand-alone GUI such as a JFrame or JDialog or at the minimum a JPanel in a JOptionPane. Again, you cannot and should not use an applet in any of this. Period. My previous recommendations still stand.

Upvotes: 2

Related Questions