Java- AWT Applets- 如何更改程序中形状的大小?

标签 java applet awt

My program

该程序旨在采用用户决定的形状,并根据输入更改其大小。

我有两个问题。一个是用户必须将其作为字符串输入,但大小值是整数。如果我将整数转换为字符串,转换时会出现空异常。 (java.lang.Integer.parseInt(未知来源)异常)。)

另一个问题是我不知道在actionPerfomed方法中添加什么。因为所有的信息只需要传到paint方法中。如何将整数值传递到 Paint 方法中。

import java.io.*;
import java.util.*;
import java.text.*;

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.applet.Applet;


public class ChangeSize extends Applet
    implements ActionListener
{
    Button bttn1 = new Button ("Circle");
    Button bttn2 = new Button ("Square");
    Button bttn3 = new Button ("Triangle");
    Button bttn4 = new Button ("Rectangle");

    Label lab1; // text within applet
    TextField t1; // where user inputs text
    String input;

    int choice;

    public void init ()
    {
        this.setSize (500, 300);

        setBackground (Color.lightGray);

        lab1 = new Label ("Insert the size of the shape:");
        //int Size = Integer.parseInt (input);
        add (lab1);
        t1 = new TextField ();
        add (t1);

        bttn1.addActionListener (this); // circle button
        bttn2.addActionListener (this); // square button
        bttn3.addActionListener (this); // triangle button
        bttn4.addActionListener (this); // rectangle button

        add (bttn1);
        add (bttn2);
        add (bttn3);
        add (bttn4);

    }
    public void paint (Graphics g)
    {
        int xpoints[] = {25, 145, 25, 145, 25}; \
        int ypoints[] = {25, 25, 145, 145, 25};
        int npoints = 5;

        switch (choice)
        {
            case 1:
                if (choice == 1)
                    g.setColor (Color.red);
                g.fillOval (30, 40, 20, 20); // I want it to be (30,40, 20, size). Instead


            case 2:
                if (choice == 2)
                    g.fillRect (20, 40, 100, 100);
            case 3:
                if (choice == 3)
                    g.fillPolygon (xpoints, ypoints, npoints);
            case 4:
                if (choice == 4)
                    g.fillRect (20, 40, 50, 100);

                break;
        }

        showStatus ("Please seclect an option.");
    }
    public void actionPerformed (ActionEvent evt)
    {
        if (evt.getSource () == bttn1)
            choice = 1;
        else if (evt.getSource () == bttn2)
            choice = 2;
        else if (evt.getSource () == bttn3)
            choice = 3;
        else if (evt.getSource () == bttn4)
            choice = 4;

          Size = t1.getText ();

          // I dont know what to put here

        repaint ();
    }

}

最佳答案

您已经接近解决方案和您的评论,//我希望它是(30,40,20,大小)。反而 持有 key 。

  • 为您的应用程序提供一个或多个 int 类字段来保存绘图尺寸。
  • 使用该尺寸在绘画例程中设置形状的尺寸。
  • 允许用户在 actionPerformed 方法中更改大小。
  • 您获得的文本将是一个字符串,您需要将其解析为 int,然后才能通过 Integer.parseInt(...) 方法使用它。

关于:

I have two issues. One is that the user will have to input it as a string yet the size value is an integer. If I convert integer to string it will give me an null exception when converting. (java.lang.Integer.parseInt(Unknown Source)Exception).)

问题在于您尝试解析 int 的位置。您正在 init 方法中执行此操作,该方法是创建和构建小程序的方法,这是用户有时间将输入添加到文本字段之前的方法。在actionPerformed方法中解析会好得多。

关于Java- AWT Applets- 如何更改程序中形状的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15180944/

相关文章:

java - fragment recyclerview 不同的 View 类型不起作用

java - 浏览器支持 Java Applet 的线程吗?

java - Eclipse Glassfish 4.0 无法按时启动服务器

java - 绩效十亿计数器

java - 重新加载 java 小程序

java - Swing:如何制作带有软边框的非矩形窗口?

java - 在 Windows 上检测 Java 中的 SHIFT 键时出现问题

java - 如何将 Ellipse2D 和 Rectangle 作为一个形状表示/使用?

java - 将字符串转换为整数

java - 多类java小程序到jar到html