java - 在 Applet 上绘图而不扩展 Applet 类

标签 java applet

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();
    }
}

说明:

shape 类是一个抽象类,它包含 getPerimeter 、 getArea 和 draw 抽象方法, Rectangle 类的 draw 方法应该在窗口中绘制矩形。

目标

在单独的窗口上绘制矩形,同时使我的应用程序成为其他方法的控制台。

最佳答案

建议:

  • 使用 Swing GUI 库,而不是 AWT。
  • 您需要在扩展 JPanel 的类中进行绘制
  • 重写 JPanel 的 protected void PaintComponent(Graphics g) 方法,并在此方法内进行绘图。
  • 请务必在覆盖内部调用 super 的方法:super.paintComponent(g)
  • 然后在您想要的任何顶级 Swing 窗口内显示 JPanel,但它是 JApplet、JFrame、JDialog,或者在另一个 JPanel 内(如果需要)。您可以通过 add(myDrawingJPanel);
  • 在 JApplet 的 init() 方法中将此绘图 JPanel 添加到您的小程序中
<小时/>

关于:

is it possible to use applet without extending Applet class?

不,我不这么认为。如果您想创建一个 GUI 并在 JApplet 或 Applet 中显示它,那么您必须重写该类,特别是重写其 init() 方法,并将 GUI 添加到那里的 applet。

如需更具体的帮助,请考虑针对您的问题提供更多信息和代码。

<小时/>

编辑
关于您的编辑:

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 ?

同样,如果您想绘制某些内容并显示它,您需要一个 GUI,因此不妨创建并显示一个独立的 GUI,例如 JFrame 或 JDialog,或者至少是 JOptionPane 中的 JPanel。再次强调,您不能也不应该在其中使用小程序。时期。我之前的建议仍然有效。

关于java - 在 Applet 上绘图而不扩展 Applet 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27091328/

相关文章:

java - Spring 验证: Difference between validation annotations in Class Attributes VS Constructor Parameters

java - 用 O(nlogn) 时间 O(1) 空间有效地计算数组中等值对的数量

Java选择txt文件并在函数中使用上述字符串

Java Applet 显示数据库中的名称

java - 使用 java 获取可移植设备

java - HTML Java 小程序集成

Java Applet JSlider 宽度

java - 如何在每次请求后将对象添加到模型中(Spring Boot)

java - 使用优先级堆/比较器时输出不规则

java - 将 JComboBox 与 ItemListener/ActionListener 结合使用