2013-03-03 38 views
1

My programJava- AWT Applets-如何更改我的程序中形狀的大小?

該程序的設計目的是根據用戶設定的形狀,根據輸入改變其大小。

我有兩個問題。一個是用戶必須輸入它作爲一個字符串,但大小的值是一個整數。如果我將整數轉換爲字符串,轉換時會給我一個空例外。 (java.lang.Integer.parseInt(Unknown Source)Exception)。)

另一個問題是我不知道要在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(); 
    } 

} 
+0

爲什麼在2013年AWT? Swing是第3個千年(開始時)使用的GUI工具包。 'this.setSize(500,300);'不要在applet中執行此操作(無論是Applet ***還是*** JApplet),大小設置爲HTML(可能由JavaScript編寫)。 – 2013-03-03 02:03:09

回答

2

您已接近解決方案,您的評論// I want it to be (30,40, 20, size). Instead 是關鍵。

  • 給你的應用程序中的int類或多個字段來保存您的圖紙尺寸。
  • 使用該尺寸在繪畫例程中設置形狀的大小。
  • 允許用戶更改actionPerformed方法中的尺寸。
  • 您得到的文本將是一個字符串,並且您需要將其解析爲一個int,然後才能通過Integer.parseInt(...)方法使用它。

關於:

我有兩個問題。一個是用戶必須將其作爲字符串輸入,但大小值是整數。如果我將整數轉換爲字符串,轉換時會給我一個空例外。 (java.lang.Integer.parseInt(Unknown Source)Exception)。)

問題是你試圖解析int的地方。您在init方法中執行此操作,該方法是在用戶有時間將輸入添加到文本字段之前創建並構建小程序的方法,該方法爲方式。在actionPerformed方法中分析會更好。