2012-04-25 12 views
1

スレッドを検索し、すべてのソリューションを試しましたが、drawRectをパネルに配置しました。それが最善の解決策なのですか、何か簡単なものがありませんか?私は目に見えるセットを使用したが、私はそれが必要であるとは信じていないし、また再塗装を試みた。アプレットがウィンドウ枠でリサイズされるまで描画されたJavaのdrawRectは表示されません

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


public class HW5_drawRec extends Applet implements ActionListener 

{ 
    int width, height; 
    int y = 150; 
    int x = 75; 

//construct Components 
Label appletLabel = new Label("Homework 5, Draw a Rectangle"); 
Label widthLabel = new Label("Enter the desired width:"); 
TextField widthField = new TextField(10); 
Label heightLabel = new Label("Enter the desired height:"); 
    TextField heightField = new TextField(10); 
Button drawButton = new Button("Draw"); 
Label outputLabel = new Label("Click to draw the rectangle."); 

public void init(){ 
    setForeground(Color.gray); 
    add(appletLabel); 
    add(widthLabel); 
    add(widthField); 
    add(heightLabel); 
    add(heightField); 
    add(drawButton); 
    drawButton.addActionListener(this); 
    add(outputLabel); 
} 

public void actionPerformed(ActionEvent e){ 

    width = Integer.parseInt(widthField.getText()); 
    height = Integer.parseInt(heightField.getText()); 

} 

    public void paint(Graphics g){ 

if (width >= 401){ 
    if (height >= 401){ 
    g.drawString("Please input dimensions greater than zero and/or less than 400",x,y); 
    }}else{ 
    g.setColor(Color.RED); 
    g.fillRect(x,y,width,height); 
    g.setColor(Color.BLACK); 
    g.drawRect(x,y,width,height); 
    setVisible(true); 

    } 
}} 

答えて

1

actionPerformedイベントの最後にrepaint()を挿入します。

+0

すごくうれしい! – Daishi100

関連する問題