这是我写的一个画图程序,只有400多行,小的不能再小,菜得不能再菜,但是我自己花了几个小时写的,我很菜的,所以高手飞过喽!
如果大家努力写的程序,都可以发出来的,希望版主不要骂我啊。
有人说:学习编程的方法有二个:一个是看别人写的好东西,一个是自己写代码。
已经在eclipse上编译过的,没有问题:
Draw类:
// Fig.12.15: Draw.java
// by rookie
// Mon Aug 4, 2:09 PM
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Draw extends JPanel {
public final static int BLACK=1,BLUE=2,RED=3,
GREEN=4,CYAN=5,MAGENTA=6;
public final static int LINE=1, RECT=2,CIRCLE=3;
private int shape;
private int x1,y1,x2,y2;
private static int fillColor, lineColor;
public Draw() {
// set up mouse listener
addMouseListener(
// anonymous inner class for mouse pressed and
// released event handling
new MouseAdapter() {
// handle mouse press event
public void mousePressed(MouseEvent event)
{
x1=event.getX();
y1=event.getY();
}
public void mouseReleased(MouseEvent event) {
x2=event.getX();
y2=event.getY();
repaint();
}
} // end anonymous class
); // end call to addMouseListener
// set up mouse motion listener
addMouseMotionListener(
// anonymous class to handle mouse drag event
new MouseMotionAdapter() {
// handle mouse listener
public void mouseDragged(MouseEvent event) {
x2=event.getX();
y2=event.getY();
repaint();
}
} // end anonymous inner class
); // end call to addMouseMotionListener
} // end constructor
public Dimension getPreferredSize()
{
return new Dimension(250,300);
}
public void paintComponent(Graphics gg) {
super.paintComponent(gg);
Graphics2D g=(Graphics2D)gg;
g.setStroke(new BasicStroke(3.0f));
if(fillColor==BLACK)
g.setColor(Color.black);
else if(fillColor==BLUE)
g.setColor(Color.blue);
else if(fillColor==RED)
g.setColor(Color.red);
else if(fillColor==GREEN)
g.setColor(Color.green);
else if(fillColor==CYAN)
g.setColor(Color.cyan);
else if(fillColor==MAGENTA)
g.setColor(Color.magenta);
else
g.setColor(Color.white);
if(shape==LINE)
g.drawLine(x1,y1,x2,y2);
else if(shape==RECT)
g.fillRect(Math.min(x1,x2),Math.min(y1,y2),
Math.abs(x1-x2),Math.abs(y1-y2));
else if(shape==CIRCLE)
g.fillOval(Math.min(x1,x2), Math.min(y1,y2),
Math.abs(x1-x2),Math.abs(y1-y2));
if(lineColor==BLACK)
g.setColor(Color.black);
else if(lineColor==BLUE)
g.setColor(Color.blue);
else if(lineColor==RED)
g.setColor(Color.red);
else if(lineColor==GREEN)
g.setColor(Color.green);
else if(lineColor==CYAN)
g.setColor(Color.cyan);
else if(lineColor==MAGENTA)
g.setColor(Color.magenta);
else
g.setColor(Color.white);
if(shape==LINE)
g.drawLine(x1,y1,x2,y2);
else if(shape==RECT)
g.drawRect(Math.min(x1,x2),Math.min(y1,y2),
Math.abs(x1-x2),Math.abs(y1-y2));
else if(shape==CIRCLE)
g.drawOval(Math.min(x1,x2), Math.min(y1,y2),
Math.abs(x1-x2),Math.abs(y1-y2));
}
public void draw(int shapeToDraw)
{
shape=shapeToDraw;
}
public void fillColor(int colorType)
{
fillColor=colorType;
repaint();
}
public void lineColor(int colorType)
{
lineColor=colorType;
repaint();
}
public double getDimension() {
if(shape==LINE)
return 0;
else if(shape==RECT)
return Math.abs(x1-x2)*Math.abs(y1-y2);
else if(shape==CIRCLE)
return 3.14*Math.abs(x1-x2)*Math.abs(y1-y2);
else
return 0;
}
public double getZhouchang()
{
double line=Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
if(shape==LINE)
return line;
else if(shape==RECT)
return 2*(Math.abs(x1-x2)+Math.abs(y1-y2));
else if(shape==CIRCLE)
return Math.PI*Math.abs(x1-x2)*Math.abs(y1-y2);
else return 0;
}
}
DrawByMenu类:
// By rookie
// Thu Aug 7, 3:30
// Draw some graphics by menu.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DrawByMenu extends JFrame {
private JRadioButtonMenuItem fillColorItem[], lineColorItem[];
private ButtonGroup lineGroup,fillGroup,graphicsGroup;
private Draw myPanel;
private JLabel label1,label2,label3,label4;
private JRadioButtonMenuItem graphicsType[];
private String colors[]={"Black","Blue","Red","Green","Cyan","Magenta"};
// set up GUI
public DrawByMenu()
{
super("Drawing by menu");
Container container=getContentPane();
container.setLayout(new BorderLayout());
myPanel=new Draw();
myPanel.setBackground(Color.white);
// contain the label
JPanel panel=new JPanel();
panel.setLayout(new GridLayout(1,4));
myPanel.draw(Draw.BLACK);
myPanel.fillColor(Draw.BLACK);
myPanel.lineColor(Draw.RED);
ItemHandler1 itemHandler1=new ItemHandler1();
ItemHandler2 itemHandler2=new ItemHandler2();
label1=new JLabel("Dimension:");
label2=new JLabel("Zhouchang:");
label3=new JLabel("Fill color:Black");
label4=new JLabel("Line color:Red");
panel.add(label1);
panel.add(label2);
panel.add(label3);
panel.add(label4);
// set up File menu and its items
JMenu fileMenu=new JMenu("File");
fileMenu.setMnemonic('F');
// set up About...menu items
JMenuItem aboutItem=new JMenuItem("About...");
aboutItem.setMnemonic('A');
aboutItem.addActionListener(
// anonymous inner class to handle menu item event
new ActionListener() {
// display message dialog when user selects About...
public void actionPerformed(ActionEvent event)
{
JOptionPane.showMessageDialog(DrawByMenu.this,
"This is an example\nof using menu to draw graphics",
"About",JOptionPane.PLAIN_MESSAGE);
}
}
);
fileMenu.add(aboutItem);
// set up exit menu item
JMenuItem exitItem=new JMenuItem("Exit");
exitItem.addActionListener(
// anonymous inner class to handle exitItem
new ActionListener() {
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
}
);
fileMenu.add(exitItem);
// create menu bar and attach it to the window
JMenuBar bar=new JMenuBar();
setJMenuBar(bar);
bar.add(fileMenu);
// create graphics menu
JMenu graphics=new JMenu("Graphics");
graphics.setMnemonic('G');
graphicsGroup=new ButtonGroup();
// create submenu for graphics
String type[]={"Line","Rectangle","Circle"};
graphicsType=new JRadioButtonMenuItem[type.length];
for(int count=0;count
graphicsType[count]=
new JRadioButtonMenuItem(type[count]);
graphics.add(graphicsType[count]);
graphicsGroup.add(graphicsType[count]);
graphicsType[count].addActionListener(itemHandler1);
}
graphicsType[0].setSelected(true);
bar.add(graphics);
// create colorMenu, its submenus and menu items
JMenu colorMenu=new JMenu("Color");
colorMenu.setMnemonic('c');
// create submenu
JMenu fill=new JMenu("Fill");
fill.setMnemonic('l');
fillColorItem=new JRadioButtonMenuItem[colors.length];
fillGroup=new ButtonGroup();
// create fill color radio button menu items
for(int count=0;count
fillColorItem[count]=
new JRadioButtonMenuItem(colors[count]);
fill.add(fillColorItem[count]);
fillGroup.add(fillColorItem[count]);
fillColorItem[count].addActionListener(itemHandler2);
}
// select first color menu item
fillColorItem[0].setSelected(true);
// add colorMenu to menu bar
colorMenu.add(fill);
colorMenu.addSeparator();
// create line submenu
JMenu line=new JMenu("Line");
line.setMnemonic('i');
// create color submenu for line
lineColorItem=new JRadioButtonMenuItem[colors.length];
lineGroup=new ButtonGroup();
for(int count=0;count
lineColorItem[count]=
new JRadioButtonMenuItem(colors[count]);
line.add(lineColorItem[count]);
lineGroup.add(lineColorItem[count]);
lineColorItem[count].addActionListener(itemHandler2);
}
lineColorItem[2].setSelected(true);
colorMenu.add(line);
bar.add(colorMenu);
container.add(myPanel,BorderLayout.CENTER);
container.add(panel, BorderLayout.SOUTH);
myPanel.addMouseMotionListener(
new MouseMotionListener() {
public void mouseDragged(MouseEvent event)
{
setTitle("Dragging: x="+event.getX()+
"; y="+event.getY());
label1.setText("Dimension:"+Double.toString(myPanel.getDimension()));
label2.setText("Zhouchang:"+Double.toString(myPanel.getZhouchang()));
}
public void mouseMoved(MouseEvent event)
{
setTitle("Moving: x="+event.getX()+
"; y="+event.getY());
}
}
);
setSize(700,700);
setVisible(true);
}
// execute application
public static void main(String args[])
{
DrawByMenu app=new DrawByMenu();
app.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
}
// inner class to handle action events from menu items
private class ItemHandler1 implements ActionListener {
public void actionPerformed(ActionEvent event) {
// process fillColor selection
for(int count=0;count
{
if(graphicsType[count].isSelected()) {
myPanel.draw(count+1);
break;
}
}
}
}
private class ItemHandler2 implements ActionListener {
public void actionPerformed(ActionEvent event) {
for(int count=0;count
{
// process fill color selection
if(fillColorItem[count].isSelected()) {
myPanel.fillColor(count+1);
label3.setText("Fill color:"+colors[count]);
break;
}
}
for(int count=0;count
{
// process line color selection
if(lineColorItem[count].isSelected()) {
myPanel.lineColor(count+1);
label4.setText("Line color:"+colors[count]);
break;
}
}
}
}
} // end class DrawByMenu
--------------------next---------------------
阅读(241) | 评论(0) | 转发(0) |