資源簡介
1:采用單例模式設計;
2:應用雙緩沖技術(shù);
3:更改線段的顏色,寬度。

代碼片段和文件信息
import?java.awt.Color;
import?java.awt.Container;
import?java.awt.Graphics;
import?java.awt.GridLayout;
import?java.awt.Image;
import?java.awt.Point;
import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
import?java.awt.event.ItemEvent;
import?java.awt.event.ItemListener;
import?java.awt.event.MouseEvent;
import?java.awt.event.MouseListener;
import?java.awt.event.MouseMotionListener;
import?javax.swing.JButton;
import?javax.swing.JComboBox;
import?javax.swing.Jframe;
import?javax.swing.JLabel;
import?javax.swing.JPanel;
import?javax.swing.event.InternalframeEvent;
import?javax.swing.event.InternalframeListener;
public?class?DrawPen?implements?ActionListener?MouseListener
MouseMotionListener?ItemListener?
{
private?static?DrawPen?instance=null;
Jframe?frame;
MyCanvas?cans;
JButton?bt[]?=?new?JButton[4];
JLabel?lColor?=?new?JLabel(“Color?Setting“JLabel.RIGHT);
JLabel?lLine?=?new?JLabel(“Line?Width“JLabel.RIGHT);
JLabel?la?=?new?JLabel();
JLabel?lc?=?new?JLabel();
JComboBox?cob[]?=?new?JComboBox[2];
String?color[]?=?{?“red“?“yellow“?“green“?“blue“?“black“?“white“};
String?size[]?=?{“1““2““3““4““5““6““7““8““9“};
private??Point?dots[]?=?new?Point[2048];
private??int?dot?=?0;
private??int?LC?=?0LD?=?2flag?=?0iStart_xiStart_yiLast_xiLast_yiHeightiWidth;
private?int?Screen_Width?=?1024Screen_Height?=?768;
private?DrawPen()
{
Jframe?frame?=?new?Jframe(“JYGraphicsEX“);
frame.setSize(Screen_WidthScreen_Height);
Container?con?=?frame.getContentPane();?//????
JPanel?jp1?=?new?JPanel(new?GridLayout(1?9?10?10));
String?[]btName?=?{“Line““Curve““Rectangle““Ellipse“};
for(int?i?=?0;?i? {
bt[i]?=?new?JButton(btName[i]);
jp1.add(bt[i]);
bt[i].addActionListener(this);
}
for(int?i?=?0;?i? {
cob[i]?=?new?JComboBox();
if(i?==?0)
{
con.add(jp1“North“);
jp1.add(lColor);
for(int?j?=?0;?j?6;?j++)
{
cob[i].addItem(color[j]);
jp1.add(cob[i]);
}
}else
{
for(int?k?=?0;?k?9;?k++)
{
con.add(jp1“North“);
jp1.add(lLine);
cob[i].addItem(size[k]);
jp1.add(cob[i]);
}
}
cob[i].setBackground(Color.white);
cob[i].addItemListener(this);
}
jp1.add(la);
jp1.add(lc);
cans?=?new?MyCanvas();
cans.setBackground(Color.BLACK);
cans.addMouseListener(this);
cans.addMouseMotionListener((MouseMotionListener)this);
con.add(cans);
frame.setVisible(true);
}
public?void?itemStateChanged(ItemEvent?event)
{
?if?(event.getSource()?==?cob[0])
?{???
?String?str?=?(String)?event.getItem();???
?for?(int?i?=?0;?i?6;?i++)
?if?(str?==?color[i])?????
?LC?=?cob[0].getSelectedIndex();?//???????
?}
?if?(event.getSource()?==?cob[1])
?{
?String?str?=?(String)event.getItem();???
?for?(int?i?=?0;?i?9;?i++)
?if?(str?==?size[i])
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????301??2012-11-26?08:53??Jpen\.classpath
?????文件????????380??2012-11-26?08:53??Jpen\.project
?????文件????????629??2012-11-26?08:53??Jpen\.settings\org.eclipse.jdt.core.prefs
?????文件???????9575??2012-11-28?15:32??Jpen\bin\DrawPen.class
?????文件???????3185??2012-11-28?15:32??Jpen\bin\MyCanvas.class
?????文件???????7136??2012-11-27?11:35??Jpen\src\DrawPen.java
?????文件???????2678??2012-11-27?10:06??Jpen\src\MyCanvas.java
?????目錄??????????0??2012-11-26?10:00??Jpen\.settings
?????目錄??????????0??2012-11-28?15:32??Jpen\bin
?????目錄??????????0??2012-11-26?10:00??Jpen\src
?????目錄??????????0??2012-11-26?10:00??Jpen
-----------?---------??----------?-----??----
????????????????23884????????????????????11
評論
共有 條評論