2010年4月16日 星期五

99/04/16 Java 期中考



















import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TriangleMethod extends JFrame
implements ActionListener {

private JButton button;
private JPanel panel;

public static int b1,b2;
public static void main(String[] args) {

int a1,a2;

a1 = Integer.parseInt(args[0]);
a2 = Integer.parseInt(args[1]);
b1 = a1;
b2 = a2;

TriangleMethod frame = new TriangleMethod();
frame.setSize(350, 400);
frame.createGUI();

frame.setVisible(true);

}



private void createGUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new FlowLayout() );

panel = new JPanel();
panel.setPreferredSize(new Dimension(300, 300));
panel.setBackground(Color.white);
window.add(panel);

button = new JButton("Press me");
window.add(button);
button.addActionListener(this);
}

public void actionPerformed(ActionEvent event) {
Graphics paper = panel.getGraphics();
drawLogo(paper, 10, 20);
//drawLogo(paper, 100, 100);
//drawTriangle(paper, 100, 10, 40, 40);
drawTriangle(paper, 10, 100, 20, 60);
}

private void drawLogo(Graphics drawingArea,
int xPos, int yPos) {
drawingArea.drawRect(xPos, yPos, b1, b2);//drawingArea.drawRect(xPos, yPos, 40, 40);
//drawingArea.drawRect(xPos, yPos, 20, 20);
}

private void drawTriangle(Graphics drawingArea,
int xPlace,
int yPlace,
int width,
int height) {
drawingArea.drawLine(xPlace, yPlace,
xPlace, yPlace + b1);
drawingArea.drawLine(xPlace, yPlace + b1,
xPlace + b2, yPlace + b1);
drawingArea.drawLine(xPlace, yPlace,
xPlace + b2, yPlace + b1);
}
}



注意事項
1.首先必須先把值改程可以用輸入的。
2.最直接的方法就是放外層共用。
3.並不是直接將值傳入,改程讓程式自動取值。

沒有留言:

張貼留言