?? huatu2.java
字號(hào):
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import java.awt.event.*;
import java.io.File;
import javax.imageio.ImageIO;
public class HuaTu2 implements MouseMotionListener,ActionListener{
static final int r=3;
static int ox, oy;
static int flag=1;
static Color [] color = {Color.red,Color.green,Color.blue,Color.yellow,Color.pink,Color.orange,Color.cyan,Color.magenta,Color.gray,Color.black};
static String [] s = {"Red","Green","Blue","Yellow","Pink","Orange","Cyan","Magenta","Gray","Black"};
static int mode;
public static void main(String arg[]){
final JFrame f=new JFrame("Paint");
Container p=f.getContentPane();
JPanel jpanel = new JPanel();
p.setLayout(new FlowLayout());
JMenuBar jmbar = new JMenuBar();
JMenu jm = new JMenu("File");
JMenuItem [] jmitem = {new JMenuItem("New"),new JMenuItem("Load"),new JMenuItem("Save"),new JMenuItem("Exit")};
final BufferedImage ibuff;
f.setJMenuBar(jmbar);
jmbar.add(jm);
for(int i=0;i<4;i++)
jm.add(jmitem[i]);
jmitem[0].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Graphics g = f.getGraphics();
Color col = g.getColor();
g.setColor(Color.white);
g.fillRect(0,0,f.getWidth(),f.getHeight());
g.setColor(col);
}
}
);
jmitem[1].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Graphics g = f.getGraphics();
JFileChooser jfc = new JFileChooser();
//jfc.setFileFilter(new JPGFilter());
int option = jfc.showOpenDialog(f);
if (option == JFileChooser.CANCEL_OPTION)
return;
File file = jfc.getSelectedFile();
Image img = Toolkit.getDefaultToolkit().getImage(file.getAbsolutePath());
g.drawImage(img,0,0,f.getWidth(),f.getHeight(),f);
}
}
);
jmitem[2].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JFileChooser jfc = new JFileChooser();
//jfc.setFileFilter(new JPGFilter());
int option = jfc.showSaveDialog(f);
if (option == JFileChooser.CANCEL_OPTION)
return;
File file = jfc.getSelectedFile();
//ImageIO.write(ibuff,"JPG",file);
}
}
);
jmitem[3].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{System.exit(0);}
}
);
jpanel.setBackground(Color.white);
p.add(jpanel);
JButton [] jb = new JButton[10];
final JComboBox jc = new JComboBox();
jc.addItem("Lines");
jc.addItem("Rectangles");
jc.addItem("Ovals");
jc.addItem("Points");
jc.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
if (e.getStateChange() == ItemEvent.SELECTED)
mode = jc.getSelectedIndex();
}
}
);
jpanel.add(jc);
//Graphics g.setColor(new Color(255,0,0));
//ButtonEvent arg=new ButtonEvent(arg);
for (int i=0;i<10;i++)
{
jb[i] = new JButton(s[i]);
jb[i].addActionListener(new HuaTu());
p.add(jb[i]);
}
p.addMouseMotionListener(new HuaTu());
p.addMouseListener(new MouseAdapter()
{
int x1,y1,x2,y2;
int temp;
public void mousePressed(MouseEvent e)
{
if (mode!=3)
{
x1 = e.getX();
y1 = e.getY();
}
}
public void mouseReleased(MouseEvent e)
{
Container c = (Container)e.getSource();
Graphics g = c.getGraphics();
if (mode!=3)
{
x2 = e.getX();
y2 = e.getY();
if (mode!=0){
if (x1>x2)
{
temp = x1;
x1 = x2;
x2 = temp;
}
if (y1>y2)
{
temp = y1;
y1 = y2;
y2 = temp;
}
}
g.setColor(color[flag]);
switch(mode)
{
case 0:
g.drawLine(x1,y1,x2,y2);break;
case 1:
g.drawRect(x1,y1,x2-x1,y2-y1);break;
case 2:
g.drawOval(x1,y1,x2-x1,y2-y1);break;
}
}
}
}
);
f.setSize(800,800);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
for (int i=0;i<10;i++)
if (e.getActionCommand().equals(s[i]))
{
flag = i;
System.out.println(flag);
break;
}
}
public void mouseDragged(MouseEvent e){
if (mode == 3)
{
Container c=(Container)e.getSource();
Graphics g=c.getGraphics();
if (ox>=0) {
g.setColor(color[flag]);
g.drawLine(ox,oy,e.getX(),e.getY());}
ox=e.getX();oy=e.getY();
}
}
public void mouseMoved(MouseEvent e){
if (mode == 3)
ox=-1;oy=-1;
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -