注意!!!以下不一定正確
原始檔:-------------------------------------------------------------------------------------------------------
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 void main(String[] args) //新視窗的規格,應該吧
{
TriangleMethod frame = new TriangleMethod();
frame.setSize(350, 300);
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, 200));
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); //這裡是圖形1(正方形)位置高低
drawLogo(paper, 100, 100);//這裡是圖形4(正方形)位置高低
drawTriangle(paper, 100, 10, 40, 40);//這裡是圖形2(三角形)位置高低和大小
drawTriangle(paper, 10, 100, 20, 60);//這裡是圖形3(三角形)位置高低和大小
}
private void drawLogo(Graphics drawingArea, int xPos, int yPos)
{
drawingArea.drawRect(xPos, yPos, 60, 60);//這裡是圖形1,3的最外層大小
drawingArea.drawRect(xPos, yPos, 40, 40);//這裡是圖形1,3的中層大小
drawingArea.drawRect(xPos, yPos, 20, 20);//這裡是圖形1,3的內層大小
}
private void drawTriangle(Graphics drawingArea,
int xPlace,
int yPlace,
int width,
int height) //用來印出圖形的,可能吧
{
drawingArea.drawLine(xPlace, yPlace,
xPlace, yPlace + height);
drawingArea.drawLine(xPlace, yPlace + height,
xPlace + width, yPlace + height);
drawingArea.drawLine(xPlace, yPlace,
xPlace + width, yPlace + height);
}
}
修改過--------------------------------------------------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TriangleMethod extends JFrame implements ActionListener
{
public static int s0,s1,s2; //設定變數,可以讓所有類別使用
private JButton button;
private JPanel panel;
public static void main(String[] args)
{
TriangleMethod frame = new TriangleMethod();
frame.setSize(350, 300);
frame.createGUI();
frame.setVisible(true);
s0=Integer.parseInt(args[0]); //正方形長
s1=Integer.parseInt(args[1]); //三角形寬
s2=Integer.parseInt(args[2]); //三角形高
}
private void createGUI()
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new FlowLayout() );
panel = new JPanel();
panel.setPreferredSize(new Dimension(300, 200));
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();
//int t1,t2;
//t1=20;
//t2=60;
drawLogo(paper, 10, 20);
//drawLogo(paper, 100, 100);
//drawTriangle(paper, 100, 10, 40, 40);
drawTriangle(paper, 10, 100, s1, s2);
//1.不要其中2個圖型,所以註解掉
//2.將三角形定值改成變數t1,t2
//3.加入輸入動態變數s1,s2
}
private void drawLogo(Graphics drawingArea, int xPos, int yPos)
{
//int n1;
//n1=60;
//drawingArea.drawRect(xPos, yPos, 60, 60);
//drawingArea.drawRect(xPos, yPos, 40, 40);
drawingArea.drawRect(xPos, yPos, s0, s0);
//1.將最外層,中層註解掉
//4.將正方形定值改成變數n1
//3.加入輸入動態變數s0
}
private void drawTriangle(Graphics drawingArea,
int xPlace,
int yPlace,
int width,
int height)
{
drawingArea.drawLine(xPlace, yPlace,
xPlace, yPlace + height);
drawingArea.drawLine(xPlace, yPlace + height,
xPlace + width, yPlace + height);
drawingArea.drawLine(xPlace, yPlace,
xPlace + width, yPlace + height);
}
}