简体   繁体   English

如何运行java小程序代码

[英]How to run a java applet code

I have never run a Java code and I want to run the code I am going to post below.我从来没有运行过 Java 代码,我想运行我将在下面发布的代码。 So can someone tell me how I can run the code on Ubuntu including what things I should install?那么有人可以告诉我如何在 Ubuntu 上运行代码,包括我应该安装哪些东西?

I also want to modify it and put a heart image on the guy's chest and write I LOVE YOU .我也想修改一下,在男孩的胸前放一个心形图,写我爱你 Can anyone help me with that as well?任何人都可以帮助我吗?

import java.awt.*;
import java.applet.*;

public class FunnyClown extends Applet {

    public void init() {
    }

    public void paint(Graphics g) {

        g.drawString("See!  A Funny Clown=)", 30, 30 );

        //Draw a beautiful sky
        setBackground(Color.cyan);

         //Grass
        g.setColor(Color.green);
        g.fillRect(0,580,1500,3000);

        //Draw a round face
        g.setColor(Color.white);
        g.fillArc(555,300,140,150,0,360);

        //Eyes
        g.setColor(Color.black);
        g.fillArc(600,360,15,20,0,360);
        g.fillArc(640,360,15,20,0,360);

        //Smile
        g.setColor(Color.red);
        g.drawArc(615,420,20,8,190,200);

        //Hat
        g.setColor(Color.pink);
        g.fillArc(520,290,205,60,0,360);
        g.fillRect(568,220,115,100);
        g.fillArc(568,200,115,50,0,360);

        //Ribbons
        g.setColor(Color.white);
        g.fillRect(568,285,115,15);

        //Body
        g.setColor(Color.white);
        g.fillRect(543,470,25,70);
        g.fillRect(683,470,25,70);
        g.fillArc(543,535,20,25,0,360);
        g.fillArc(685,535,20,25,0,360);

        //T-shirt
        g.setColor(Color.yellow);
        g.fillRect(570,450,110,200);
        int []x1={570,500,570};
        int []y1={450,475,490};
        g.fillPolygon(x1,y1,3);
        int []x2={680,750,680};
        int []y2={450,475,490};
        g.fillPolygon(x2,y2,3);

        //Pants
        g.setColor(Color.blue);
        g.fillRect(570,580,110,150);
        g.setColor(Color.green);
        int []x3={625,598,638};
        int []y3={600,730,730};
        g.fillPolygon(x3,y3,3);

        //Shoes
        g.setColor(Color.black);
        g.fillArc(570,728,35,30,0,360);
        g.fillArc(642,728,35,30,0,360);

        //Clouds
        g.setColor(Color.white);
        g.fillArc(100,100,60,60,0,360);
        g.fillArc(120,120,70,70,0,360);
        g.fillArc(120,80,70,70,0,360);
        g.fillArc(160,70,80,80,0,360);
        g.fillArc(190,75,90,90,0,360);
        g.fillArc(240,85,60,60,0,360);
        g.fillArc(220,110,70,70,0,360);
        g.fillArc(170,120,70,70,0,360);

        //Sun
        g.setColor(Color.yellow);
        int x[]={900,1080,1180,980,1080,1180,800,900,1000};
        int y[]={100,80,150,80,100,80,110,100,60};
        g.fillPolygon(x,y,9);
        g.setColor(Color.red);
        g.fillArc(1015,50,80,80,0,360);

        //Flower
        g.setColor(Color.magenta);
        g.fillArc(80,500,20,20,0,360);
        g.fillArc(95,485,20,20,0,360);
        g.fillArc(110,500,20,20,0,360);
        g.fillArc(86,515,20,20,0,360);
        g.fillArc(103,515,20,20,0,360);
        g.setColor(Color.yellow);
        g.fillArc(95,503,20,20,0,360);
        g.setColor(Color.darkGray);
        g.fillRect(103,525,5,70);

        g.setColor(Color.magenta);
        g.fillArc(300,500,20,20,0,360);
        g.fillArc(315,485,20,20,0,360);
        g.fillArc(330,500,20,20,0,360);
        g.fillArc(306,515,20,20,0,360);
        g.fillArc(323,515,20,20,0,360);
        g.setColor(Color.yellow);
        g.fillArc(315,503,20,20,0,360);
        g.setColor(Color.darkGray);
        g.fillRect(323,525,5,70);

        g.setColor(Color.magenta);
        g.fillArc(190,500,20,20,0,360);
        g.fillArc(205,485,20,20,0,360);
        g.fillArc(220,500,20,20,0,360);
        g.fillArc(196,515,20,20,0,360);
        g.fillArc(217,515,20,20,0,360);
        g.setColor(Color.yellow);
        g.fillArc(205,503,20,20,0,360);
        g.setColor(Color.darkGray);
        g.fillRect(215,525,5,70);

        //House
        g.setColor(Color.gray);
        int []x4={850,1050,1250};
        int []y4={300,200,300};
        g.fillPolygon(x4,y4,3);
        g.setColor(Color.lightGray);
        g.fillRect(850,300,400,300);


        //Window
        g.setColor(Color.cyan);
        g.drawRect(1100,350,100,100);
        g.drawLine(1100,400,1200,400);
        g.drawLine(1150,350,1150,450);

        //Door
        g.setColor(Color.darkGray);
        g.fillRect(900,400,130,200);
        g.setColor(Color.yellow);
        g.fillArc(930,480,10,10,0,360);





    }
}

Here's what the code supposed to show when you run it:以下是代码在运行时应该显示的内容: 在此处输入图片说明

To draw the I love you text you can call: g.drawString(...) method and position it over the T-Shirt.要绘制I love you文本,您可以调用: g.drawString(...)方法并将其放置在 T 恤上。

Next to draw a heart shape, you can call the same method using the ASCII's heart shape: 接下来要绘制心形,您可以使用 ASCII 的心形调用相同的方法:

But be sure to use a nice (big enough) font so it can be visualized correctly, for example:但一定要使用漂亮(足够大)的字体,以便可以正确显示,例如:

g.setFont(new Font("Verdana", Font.PLAIN, 25));
g.setColor(Color.BLACK);
g.drawString("I love you", 570, 480);

g.setColor(Color.RED);
g.drawString("♥", 620, 510);

But as you've been said, Java Applets are no longer supported by browsers, there are 2 things you can do:但是正如您所说,浏览器不再支持 Java Applet,您可以做两件事:

  1. Convert the code into a Swing Application (recommended)将代码转换为 Swing 应用程序(推荐)
  2. Use AppletViewer (Which I've never used)使用 AppletViewer(我从未使用过)

We're going to go through the 1st one:我们将通过第一个:

First, we need to change:首先,我们需要改变:

// Draw a beautiful sky
setBackground(Color.cyan);

for:为了:

g.setColor(Color.CYAN);
g.fillRect(0, 0, 1500, 1000); //I'm lazy to search the window size... change it accordingly

Next, we need to remove extends Applet from our class and change it to extends JPanel and change接下来,我们需要从我们的类中删除extends Applet并将其更改为extends JPanel并更改

public void paint(Graphics g) {

To:到:

@Override
public void paintComponent(Graphics g) {

And add another overriden method:并添加另一个覆盖方法:

@Override
public Dimension getPreferredSize() {
    return new Dimension(1500, 1000); //Again, change it to the window's correct size.
}

To finish we need to create a main method as follows:为了完成,我们需要创建一个main方法,如下所示:

private JFrame frame;

public static void main(String[] args) {
    SwingUtilities.invokeLater(new FunnyClown()::createAndShowGui);
}

private void createAndShowGui() {
    frame = new JFrame(getClass().getSimpleName());

    this.setOpaque(true);
    frame.add(this);
    frame.pack();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

Now, if you run the application you should see something like this:现在,如果您运行该应用程序,您应该会看到如下内容:

在此处输入图片说明

For reference, you're going to need:作为参考,您将需要:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM