最近中文字幕高清中文字幕无,亚洲欧美高清一区二区三区,一本色道无码道dvd在线观看 ,一个人看的www免费高清中文字幕

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

java繪圖程序中如何用菜單調(diào)用繪圖方法

java繪圖程序中如何用菜單調(diào)用繪圖方法

恩恩好吧 2016-07-05 11:01:35
package?xatu05; import?java.awt.*; import?java.awt.event.*; import?javax.swing.*; //import?javax.swing.event.*; public?class?PainterPanel?extends?JPanel?implements?MouseListener?{ private?static?final?long?serialVersionUID?=?1L; int?shape?=?-1;?//?圖案類型 Point[]?point?=?new?Point[2];?//?記錄鼠標(biāo)拖動(dòng)的起始點(diǎn)和終點(diǎn) public?PainterPanel()?{ super();?//?調(diào)用父類構(gòu)造函數(shù) this.setBackground(Color.white);?//?設(shè)置背景顏色 point[0]?=?new?Point(-1,?-1);?//?初始化變量 point[1]?=?new?Point(-1,?-1); addMouseListener(this);?//?增加鼠標(biāo)事件 } public?void?mouseReleased(MouseEvent?e)?{?//?鼠標(biāo)釋放事件 point[1]?=?new?Point(e.getX(),?e.getY());?//?設(shè)置終點(diǎn)位置 repaint();?//?重繪屏幕 } public?void?mouseEntered(MouseEvent?e)?{ } public?void?mouseExited(MouseEvent?e)?{ } public?void?mouseClicked(MouseEvent?e)?{ } public?void?mousePressed(MouseEvent?e)?{?//?鼠標(biāo)按下時(shí)事件 point[0]?=?new?Point(e.getX(),?e.getY());?//?設(shè)置起始點(diǎn)位置 } public?void?paint(Graphics?g)?{ super.paint(g); switch?(shape)?{?//?根據(jù)shape值繪制圖形 case?0: g.drawLine(point[0].x,?point[0].y,?point[1].x,?point[1].y);?//?繪線 break; case?1: int?width?=?point[1].x?-?point[0].x; int?height?=?point[1].y?-?point[0].y; g.drawOval(point[0].x,?point[0].y,?width,?height);?//?繪橢圓 break; case?2: int?width1?=?point[1].x?-?point[0].x; int?height1?=?point[1].y?-?point[0].y; g.fillOval(point[0].x,?point[0].y,?width1,?height1);?//?繪實(shí)心橢圓 break; case?3: width1?=?point[1].x?-?point[0].x; ; height?=?point[1].y?-?point[0].y; g.drawRect(point[0].x,?point[0].y,?width1,?height);?//?繪矩形 break; case?4: width1?=?point[1].x?-?point[0].x; ; height1?=?point[1].y?-?point[0].y; g.fillRect(point[0].x,?point[0].y,?width1,?height1);?//?繪實(shí)心矩形 break; } } public?void?drawShape(int?shape)?{ this.shape?=?shape; } }/**上面代碼沒(méi)有問(wèn)題*/package xatu05;import java.awt.*;import java.awt.event.*;import javax.swing.*;//import javax.swing.event.*;import xatu05.PainterPanel;public class PainterDemo extends JFrame { String menuItems[][] = { {}, { "空心橢圓", "實(shí)心橢圓" }, { "空心矩形", "實(shí)心矩形" }, {} }; public void init() { JMenuBar br = new JMenuBar(); // 創(chuàng)建菜單工具欄 String menu[] = { "直線", "橢圓", "矩形", "多邊形" }; for (int i = 0; i < menu.length; i++) { // 用數(shù)組創(chuàng)建Menu JMenu menu1 = new JMenu(menu[i]); br.add(menu1); for (int k = 0; k < menuItems[i].length; k++) { menu1.add(new JMenuItem(menuItems[i][k]));????????????????????????//注冊(cè)菜單事件,增加菜單處理事件如何做 } } super.setJMenuBar(br); } JToggleButton[] button = new JToggleButton[6]; // 按鈕組 PainterPanel painter = new PainterPanel(); // 繪圖面板 public PainterDemo() { super("Java畫(huà)圖程序"); // 調(diào)用父類構(gòu)造函數(shù) init(); // String[][] menultems = {{"直線"},{"空心橢圓","實(shí)心橢圓"}}; String[] buttonName = { "直線", "空心橢圓", "實(shí)心橢圓", "空心矩形", "實(shí)心矩形", "多邊形" }; // 按鈕文字 DrawShapeListener buttonListener = new DrawShapeListener();// 按鈕事件 JToolBar toolBar = new JToolBar(); // 實(shí)例化工具欄 ButtonGroup buttonGroup = new ButtonGroup(); // 實(shí)例化按鈕組 for (int i = 0; i < button.length; i++) { button[i] = new JToggleButton(buttonName[i]); // 實(shí)例化按鈕 // button[i] = new JToggleButton(buttonName[i]); button[i].addActionListener(buttonListener); // 增加按鈕事件處理 buttonGroup.add(button[i]); // 增加按鈕到按鈕組 toolBar.add(button[i]); // 增加按鈕到工具欄 } Container container = getContentPane(); // 得到窗口容器 container.add(toolBar, BorderLayout.SOUTH); // 增加組件到容器上 container.add(painter, BorderLayout.CENTER); setSize(600, 700);// 設(shè)置窗口尺寸 setLocation(500, 70); setVisible(true); // 設(shè)置窗口為可視 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 關(guān)閉窗口時(shí)退出程序 } // class DrawShapeListener implements ActionListener { // 按鈕事件處理 // public void actionPerformed(ActionEvent e) { // for (int i = 0; i < menuItems.length; i++) { // if (e.getSource() == menuItems[i]) { // 判斷來(lái)自于哪個(gè)菜單 // painter.drawShape(i); // 繪制圖形 // } // } // } // } class DrawShapeListener implements ActionListener { // 按鈕事件處理 public void actionPerformed(ActionEvent e) { for (int i = 0; i < button.length; i++) { if (e.getSource() == button[i]) { // 判斷來(lái)自于哪個(gè)按鈕 painter.drawShape(i); // 繪制圖形 } } } } public static void main(String[] args) { new PainterDemo(); }}
查看完整描述

1 回答

?
晴天小文友

TA貢獻(xiàn)4條經(jīng)驗(yàn) 獲得超1個(gè)贊

還沒(méi)學(xué)到那,現(xiàn)在看不懂

查看完整回答
1 反對(duì) 回復(fù) 2016-07-05
  • 1 回答
  • 0 關(guān)注
  • 1663 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)