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];?//?記錄鼠標拖動的起始點和終點
public?PainterPanel()?{
super();?//?調用父類構造函數(shù)
this.setBackground(Color.white);?//?設置背景顏色
point[0]?=?new?Point(-1,?-1);?//?初始化變量
point[1]?=?new?Point(-1,?-1);
addMouseListener(this);?//?增加鼠標事件
}
public?void?mouseReleased(MouseEvent?e)?{?//?鼠標釋放事件
point[1]?=?new?Point(e.getX(),?e.getY());?//?設置終點位置
repaint();?//?重繪屏幕
}
public?void?mouseEntered(MouseEvent?e)?{
}
public?void?mouseExited(MouseEvent?e)?{
}
public?void?mouseClicked(MouseEvent?e)?{
}
public?void?mousePressed(MouseEvent?e)?{?//?鼠標按下時事件
point[0]?=?new?Point(e.getX(),?e.getY());?//?設置起始點位置
}
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);?//?繪實心橢圓
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);?//?繪實心矩形
break;
}
}
public?void?drawShape(int?shape)?{
this.shape?=?shape;
}
}/**上面代碼沒有問題*/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[][] = { {}, { "空心橢圓", "實心橢圓" }, { "空心矩形", "實心矩形" }, {} }; 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]));????????????????????????//注冊菜單事件,增加菜單處理事件如何做 } } super.setJMenuBar(br); } JToggleButton[] button = new JToggleButton[6]; // 按鈕組 PainterPanel painter = new PainterPanel(); // 繪圖面板 public PainterDemo() { super("Java畫圖程序"); // 調用父類構造函數(shù) init(); // String[][] menultems = {{"直線"},{"空心橢圓","實心橢圓"}}; String[] buttonName = { "直線", "空心橢圓", "實心橢圓", "空心矩形", "實心矩形", "多邊形" }; // 按鈕文字 DrawShapeListener buttonListener = new DrawShapeListener();// 按鈕事件 JToolBar toolBar = new JToolBar(); // 實例化工具欄 ButtonGroup buttonGroup = new ButtonGroup(); // 實例化按鈕組 for (int i = 0; i < button.length; i++) { button[i] = new JToggleButton(buttonName[i]); // 實例化按鈕 // 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);// 設置窗口尺寸 setLocation(500, 70); setVisible(true); // 設置窗口為可視 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 關閉窗口時退出程序 } // class DrawShapeListener implements ActionListener { // 按鈕事件處理 // public void actionPerformed(ActionEvent e) { // for (int i = 0; i < menuItems.length; i++) { // if (e.getSource() == menuItems[i]) { // 判斷來自于哪個菜單 // 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]) { // 判斷來自于哪個按鈕 painter.drawShape(i); // 繪制圖形 } } } } public static void main(String[] args) { new PainterDemo(); }}
添加回答
舉報
0/150
提交
取消