當前位置:編程學習大全網 - 編程語言 - 編程五角碼大全

編程五角碼大全

公共類MiniDraw實現ActionListener,mouse listener {//Fields private JFrame frame = new JFrame(" MiniDraw ");

private drawing canvas canvas = new drawing canvas();私有JButton colorButton//顏色按鈕,這樣我們可以將其背景更改為當前顏色。

私有JTextField文本字段;//用於輸入要放在畫布上的文本的字段//用於記錄執行下壹個操作所需信息的字段

私有枚舉操作{Line,Rect,Oval,Triangle,Text,Dot,Move,Delete,Push,Pull}。private int pressedX//鼠標被按下的位置

私有int pressedY//鼠標被按下的位置

私人行動currentAction =行動。線;

私有顏色currentColor = Color.black個人分發名單& lt形狀& gtshapes = new ArrayList & lt形狀& gt();

/**構造函數設置GUI。*/

公共MiniDraw(){

frame.setSize(800,600);//圖形區域

canvas . addmouse listener(this);

frame.getContentPane()。添加(canvas,BorderLayout。中心);//按鈕

JPanel top panel = new JPanel();

frame.getContentPane()。add(topPanel,BorderLayout。北);

JPanel botPanel = new JPanel();

frame.getContentPane()。添加(botPanel,BorderLayout。南);

addButton(topPanel,“New”);

addButton(topPanel,“打開”);

addButton(topPanel,“保存”);

addButton(topPanel,“Delete”);

addButton(topPanel,“Move”);

colorButton = addButton(topPanel," Color ");//記住這個按鈕,這樣我們就可以改變它的顏色

addButton(topPanel,“Push”);

addButton(topPanel,“Pull”);

addButton(topPanel,“Reverse”);

addButton(topPanel,“退出”);botpanel . add(new JLabel(" Shapes:"));

addButton(botPanel,“Line”);

addButton(botPanel,“Rect”);

addButton(botPanel,“Oval”);

addButton(botPanel,“三角形”);

addButton(botPanel,“Dot”);

addButton(botPanel,“Text”);

textField = new JTextField("輸入文本",12);

botpanel . add(new JLabel(":));

botpanel . add(textField);frame . set visible(true);

} /**制作新按鈕並將其添加到面板的實用方法

把扣子還回去,萬壹我們需要它。*/

private JButton addButton(JPanel面板,字符串名稱){

JButton button = new JButton(name);

button . addactionlistener(this);

panel.add(按鈕);

返回按鈕;

} /**響應按鈕按下*/public void action performed(action event e){

string button = e . getactioncommand();

//system . out . printf(" Button:% s \ n ",Button);//用於調試

if (button.equals("New "))

new drawing();

else if (button.equals("Open "))

open drawing();

else if (button.equals("Save "))

save drawing();

else if (button.equals("Color "))

select color();

else if (button.equals("Reverse "))

reversed drawing();

else if (button.equals("Quit "))

frame . dispose();

else{ //

current action = action . value of(button);//將字符串轉換為動作。

}

} /**設置當前顏色。

*還更改顏色按鈕的顏色並設置畫布的背景顏色*/

私有void selectColor(){

Color new Color = jcolorchooser . showdialog(frame,“為新形狀選擇顏色”,current Color);

如果(newColor!=null){

currentColor = newColor

color button . set background(current color);

}

} //響應鼠標事件

/**當按下鼠標時,記住位置以便在以下情況下構建形狀

*鼠標被釋放。*/

public void mouse pressed(mouse event e){

int x = e . getx();int y = e . gety();

//system . out . printf(" Pressed at(% d,%d)\n ",x,y);//用於調試

pressedX = x;

pressedY = y;

} /**釋放鼠標時,根據當前操作,

*要麽構造正在繪制的形狀,要麽執行

*在鼠標按下點下的形狀上執行操作(刪除或移動)。*/

public void mouse released(mouse event e){

int x = e . getx();int y = e . gety();

//System.out.printf("發布於(%d,%d)\n ",x,y);//用於調試

if (currentAction==Action。移動)

moveShape(pressedX,pressedY,x,y);

else if (currentAction==Action。刪除)

deleteShape(x,y);

else if (currentAction==Action。推送)

pushShapeBackward(x,y);

else if (currentAction==Action。拉)

pullShapeForward(x,y);

其他

addShape(pressedX,pressedY,x,y);

} public void mouse clicked(mouse event e){ }//需要滿足接口

需要public void mouse enter(mouse event e){ }//來滿足接口

需要公共void mouse exited(mouse event e){ }//來滿足接口

//實現按鈕和鼠標操作的幫助器方法

/**開始新的繪圖。*/

public void newDrawing(){

shapes = new ArrayList & lt形狀& gt();

canvas . clear();

}

/**打開壹個文件,將所有形狀描述讀入當前圖形。*/

public void openDrawing(){

string fname = file chooser . open();

if (fname==null)返回;

嘗試{

掃描儀文件=新掃描儀(新文件(fname));

//System.out.printf("打開文件%s\n ",fname);

shapes = new ArrayList & lt形狀& gt();

while (file.hasNext()){

String shapetype = file.next()。toLowerCase();

if (shapetype.equals("oval "))

shapes.add(新橢圓(文件));

else if(shape type . equals(" rectangle "))

shapes.add(新矩形(文件));

if (shapetype.equals("line "))

shapes.add(新行(文件));

if (shapetype.equals("textshape "))

shapes.add(新建TextShape(文件));

else if (shapetype.equals("三角形"))

shapes.add(新三角形(文件));

else if (shapetype.equals("dot "))

shapes . add(new Dot(file));

}

render();

}

catch(IOException e) {

system . out . println(e . getmessage());

e . printstacktrace();

}

} /**將當前圖形保存到文件中。*/

public void saveDrawing(){

string fname = file chooser . save();

if ( fname == null)

返回;

嘗試{

PrintStream f = new PrintStream(新文件(fname));

用於(形狀形狀:形狀)

f . println(shape . tostring());

f . close();

}

catch(IOException ex){

system . out . println(ex . getmessage());

ex . printstacktrace();

}

}

/**返回位置(x y)下的形狀,如果沒有這樣的形狀,則返回null */

公共形狀findShape(int x,int y){

for(int I = shapes . size()-1;我& gt=0;i - ){ //必須後退,所以不能使用foreach

shape shape = shapes . get(I);

if (shape.pointOnShape(x,y))

返回形狀;

}

返回null//在位置找不到形狀

}

/**移動鼠標按下位置(pressedX,pressedY)下的形狀

去放老鼠的地方。

即通過(newX-pressedX)和(newY-pressedY)移動它

*/

public void moveShape(int fromX,int fromY,int toX,int toY){

//System.out.printf("將(%d,%d)下的形狀移動到(%d,%d)\n ",pressedX,pressedY,newX,newY);//用於調試

Shape shape = findShape(fromX,fromY);

如果(形狀!=空)

shape.moveBy((toX-fromX),(toY-fromY));

render();

} /**刪除鼠標釋放位置(x,y)下的形狀

*/

public void deleteShape(int x,int y){ for ( Shape shape : shapes) {

if ( shape.pointOnShape(x,y))

shapes . remove(shape);

}

render();

}

/**將鼠標下的形狀向“前面”拉近壹個位置

圖紙的*/

public void pullShapeForward(int x,int y){

// System.out.printf("向前拉動(%d,%d)下的形狀\n ",x,y);//用於調試

//您的代碼在這裏

Shape shape = findShape( x,y);

如果(形狀!=null ) {

int index = shapes . index of(shape);

if(index & lt;shapes.size()-1){

shapes.remove(索引);

shapes.add(索引+1,形狀);

}

render();

}

}

/**將鼠標下的形狀從“前面”再推壹個位置

圖紙的*/

public void pushShapeBackward(int x,int y){

//System.out.printf("將(%d,%d)下的形狀向後推\n ",x,y);//用於調試

//您的代碼在這裏

Shape shape = findShape(x,y);

如果(形狀!=null ){

int index = shapes . index of(shape);

if(index & gt;0 )

shapes.remove(索引);

shapes.add(索引-1,形狀);

}

render();

}

/**顛倒繪圖中形狀的順序*/

public void reversed drawing(){ List & lt;形狀& gttemp = new ArrayList & lt形狀& gt();

while(shapes . size()>;0 ) {

temp . add(shapes . remove(shapes . size()-1));

}

形狀=溫度;

render();

}

/**構造適當種類的新形狀對象(取決於當前操作)

使用Line、Rectangle、Oval、TextShape或Triangle類的適當構造函數。

將該形狀添加到繪圖中的形狀集合中,並

在畫布上呈現形狀*/

public void addShape(int x1,int y1,int x2,int y2){

//System.out.printf("繪制形狀%s,位於(%d,%d)-(%d,%d)\n ",currentAction,pressedX,pressedY,x,y);//用於調試

Shape shape = null

if (currentAction==Action。線)

shapes.add(新行(x1,y1,x2,y2,current color));

if (currentAction==Action。三角形)

shapes.add(新三角形(x1,y1,x2,y2,current color));

if (currentAction==Action。點)

shapes.add(new Dot(x2,y2,current color));

else if (currentAction==Action。正文)

shapes.add(new TextShape(x2,y2,textField.getText(),current color));

否則{

int left= Math.min(x1,x2);

int top= Math.min(y1,y2);

int width = math . ABS(x 1-x2);

int height = math . ABS(y 1-y2);

if (currentAction==Action。Rect)

shapes.add(新矩形(left,top,width,height,current color));

else if (currentAction==Action。橢圓形)

shapes.add(new Oval(left,top,width,height,current color));

}

render();

} public void render(){

canvas . clear(false);

對於(形狀形狀:形狀){

shape.render(畫布);

}

canvas.display()。

} public static void main(String args[]){

new MiniDraw();

}}

  • 上一篇:太原英語培訓學校哪裏好?線上和線下怎麽選?
  • 下一篇:飛船是怎樣控制的?
  • copyright 2024編程學習大全網