该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
//代码就是一个画板,按左键可以进行绘画,然后按右键是橡皮擦,右侧一排按钮可以调整颜色。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ScribbleDemo extends JFrame
implements ActionListener{
ColorPanel colorChoose = new ColorPanel();
ScribblePanel sP1 = new ScribblePanel();
public ScribbleDemo(){
getContentPane().setLayout(new BorderLayout());
colorChoose.setLayout(new GridLayout(0,1));
colorChoose.jb1.addActionListener(this);
colorChoose.jb2.addActionListener(this);
colorChoose.jb3.addActionListener(this);
colorChoose.jb4.addActionListener(this);
colorChoose.jb5.addActionListener(this);
colorChoose.jb6.addActionListener(this);
colorChoose.jb7.addActionListener(this);
colorChoose.jb8.addActionListener(this);
colorChoose.jb9.addActionListener(this);
getContentPane().add(sP1,BorderLayout.CENTER);
getContentPane().add(colorChoose,BorderLayout.EAST);
}
public static void main(String[] args){
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
ScribbleDemo frame = new ScribbleDemo();
frame.setTitle("White Board");
frame.setSize(screenSize.width/2,screenSize.height/2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setLocation(screenSize.width/4,screenSize.height/4);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == colorChoose.jb1){
if(e.getSource() == colorChoose.jb2)
repaint();
if(e.getSource() == colorChoose.jb3)
sP1.g.setColor(Color.YELLOW);
if(e.getSource() == colorChoose.jb4)
sP1.g.setColor(Color.GREEN);
if(e.getSource() == colorChoose.jb5)
sP1.g.setColor(Color.CYAN);
if(e.getSource() == colorChoose.jb6)
sP1.g.setColor(Color.BLUE);
if(e.getSource() == colorChoose.jb7)
sP1.g.setColor(Color.MAGENTA);
if(e.getSource() == colorChoose.jb8)
sP1.g.setColor(Color.WHITE);
if(e.getSource() == colorChoose.jb9)
sP1.g.setColor(Color.BLACK);
}
}
public void setWhiteColor(Graphics g) {
g.setColor(Color.WHITE);
}
}
class ScribblePanel extends JPanel
implements MouseListener,MouseMotionListener{
final int CIRCLESIZE = 20;
private Point lineStart = new Point(0,0);
public Graphics g;
public ScribblePanel(){
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent e){
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public void mouseReleased(MouseEvent e){
}
public void mousePressed(MouseEvent e){
lineStart.move(e.getX(),e.getY());
}
public void mouseDragged(MouseEvent e){
g = getGraphics();
if(e.isMetaDown()){
g.setColor(getBackground());
g.fillOval(e.getX()-CIRCLESIZE/2,e.getY()-CIRCLESIZE/2, CIRCLESIZE,CIRCLESIZE);
}
else{
g.setColor(Color.BLACK);
g.drawLine(lineStart.x,lineStart.y,e.getX(), e.getY());
}
lineStart.move(e.getX(), e.getY());
g.dispose();
}
public void mouseMoved(MouseEvent e){
}
}
class ColorPanel extends JPanel{
JButton jb1 = new JButton("red"),jb2 = new JButton("orange"),jb3 = new JButton("yellow"),
jb4 = new JButton("green"),jb5 = new JButton("cyan"),jb6 = new JButton("blue"),
jb7 = new JButton("purple"),jb8 = new JButton("white"),jb9 = new JButton("black");
ColorPanel(){
jb1.setBackground(Color.red);
jb2.setBackground(Color.ORANGE);
jb3.setBackground(Color.YELLOW);
jb4.setBackground(Color.GREEN);
jb5.setBackground(Color.CYAN);
jb6.setBackground(Color.BLUE);
jb7.setBackground(Color.MAGENTA);
jb8.setBackground(Color.WHITE);
jb9.setBackground(Color.BLACK);
jb1.setForeground(Color.WHITE);
jb2.setForeground(Color.WHITE);
jb3.setForeground(Color.WHITE);
jb4.setForeground(Color.WHITE);
jb5.setForeground(Color.WHITE);
jb6.setForeground(Color.WHITE);
jb7.setForeground(Color.WHITE);
jb8.setForeground(Color.BLACK);
jb9.setForeground(Color.WHITE);
this.add(jb1);
this.add(jb2);
this.add(jb3);
this.add(jb4);
this.add(jb5);
this.add(jb6);
this.add(jb7);
this.add(jb8);
this.add(jb9);
}
}