📄 tuyaban.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.ArrayList;
public class tuyaban extends JPanel implements MouseListener, MouseMotionListener, ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
public Color currentColor = Color.BLACK;
public int prevX, prevY,lastX,lastY;
public boolean dragging;
public Graphics graphicsForDrawing;
protected PopupMenu pop;
private int px2,py2;
boolean drawn=false;
int status=0;
Graphics g3=getGraphics();
Graphics2D g2=(Graphics2D) g3;
public String command="";
private ArrayList<Rectangle2D> juxing;
private ArrayList<Ellipse2D> tuoyuan;
private ArrayList<Line2D> zhixian;
private ArrayList<Line2D> qianbi;
private Rectangle2D currentju;
private Ellipse2D currenttuo;
private Line2D currentzhi;
private Line2D currentqian;
public tuyaban() {
pop=new PopupMenu("color");
String labels[]={"clear","red","green","blue","black","white","orange","cyan","gray","magenta","pink","yellow"};
for (int i=0;i<labels.length;i++){
MenuItem mi =new MenuItem(labels[i]);
mi.addActionListener(this);
pop.add(mi);
}
this.add(pop);
JToolBar tool=new JToolBar();
JButton p1=new JButton("铅笔");
p1.addActionListener(this);
JButton p2=new JButton("椭圆");
p2.addActionListener(this);
JButton p3=new JButton("矩形");
p3.addActionListener(this);
JButton p4=new JButton("直线");
p4.addActionListener(this);
add("North",tool);
this.addMouseListener(
/*new MouseAdapter(){
public void mousePressed(MouseEvent e){
lastX=e.getX();lastY=e.getY();
}
}*/
this);
this.addMouseMotionListener(
/*new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
Graphics g=getGraphics();
int x=e.getX();int y=e.getY();
g.setColor(currentColor);
g.drawLine(lastX, lastY, x, y);
lastX=x;
lastY=y;
}
}*/
this);
setSize(400,600);
juxing =new ArrayList<Rectangle2D>();
currentju=null;
tuoyuan=new ArrayList<Ellipse2D>();
currenttuo=null;
zhixian=new ArrayList<Line2D>();
currentzhi=null;
qianbi=new ArrayList<Line2D>();
currentqian=null;
addMouseListener(this);
addMouseMotionListener(this);
}
public void processMouseEvent(MouseEvent e){
if((pop!=null)&&e.isPopupTrigger())
pop.show(this, e.getX(), e.getY());
else super.processMouseEvent(e);
}
public void actionPerformed(ActionEvent e){
String name =((MenuItem)e.getSource()).getLabel();
if(name.equals("clear")){
Graphics g=getGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, this.getSize().width, this.getSize().height);
}
else if (name.equals("red"))currentColor=Color.red;
else if (name.equals("green"))currentColor=Color.green;
else if (name.equals("blue"))currentColor=Color.blue;
else if (name.equals("black"))currentColor=Color.black;
else if (name.equals("cyan"))currentColor=Color.cyan;
else if (name.equals("gray"))currentColor=Color.gray;
else if (name.equals("magenta"))currentColor=Color.magenta;
else if (name.equals("orange"))currentColor=Color.orange;
else if (name.equals("pink"))currentColor=Color.pink;
else if (name.equals("white"))currentColor=Color.white;
else if (name.equals("yellow"))currentColor=Color.yellow;
//else if (name.equals("自定义")){
//Color m=JColorChooser.showDialog(tuyaban.this, "颜色", currentColor);
//graphicsForDrawing.setColor(m);
//}
}
public void update(Graphics g) {
// Redefine update so it does not fill the applet with the
// background color before calling paint().
paint(g);
}
public void paintComponent(Graphics g)
{
super.paintComponents(g);
if(!command.equals("清除"))
{
Graphics2D g2=(Graphics2D)g;
for(Rectangle2D r:juxing)
{
g2.draw(r);
}
if(currentju!=null)
{
g2.draw(currentju);
}
for(Ellipse2D r:tuoyuan)
{
g2.draw(r);
}
if(currenttuo!=null)
{
g2.draw(currenttuo);}
for(Line2D r:zhixian)
{
g2.draw(r);
}
if(currentzhi!=null)
{
g2.draw(currentzhi);}
for(Line2D r:qianbi)
{
g2.draw(r);
}
}
else
{
juxing =new ArrayList<Rectangle2D>();
currentju=null;
tuoyuan=new ArrayList<Ellipse2D>();
currenttuo=null;
zhixian=new ArrayList<Line2D>();
currentzhi=null;
qianbi=new ArrayList<Line2D>();
currentqian=null;
}
}
public void paint(Graphics g) {
int width = getSize().width;
int height = getSize().height;
int colorSpacing = (height - 56) / 10;
g.setColor(Color.white);
g.fillRect(3, 3, width - 59, height - 6);
g.fillRect(width - 56, 0, 56, height);
g.setColor(Color.white);
g.fillRect(width-53, height-53, 50, 30);
/* g.setColor(Color.black);
g.drawRect(width-53, height-53, 35, 25);
g.setColor(Color.CYAN);
g.drawString("clear", width-48, height-33);
g.setColor(Color.BLACK);
g.fillRect(width-53, 3 + 0*colorSpacing, 50, colorSpacing-3);
g.setColor(Color.BLUE);
g.fillRect(width-53, 3 + 1*colorSpacing, 50, colorSpacing-3);
g.setColor(Color.DARK_GRAY);
g.fillRect(width-53, 3 + 2*colorSpacing, 50, colorSpacing-3);
g.setColor(Color.GREEN);
g.fillRect(width-53, 3 + 3*colorSpacing, 50, colorSpacing-3);
g.setColor(Color.MAGENTA);
g.fillRect(width-53, 3 + 4*colorSpacing, 50, colorSpacing-3);
g.setColor(new Color(255,128,000));
g.fillRect(width-53, 3 + 5*colorSpacing, 50, colorSpacing-3);
g.setColor(Color.PINK);
g.fillRect(width-53, 3 + 6*colorSpacing, 50, colorSpacing-3);
g.setColor(Color.RED);
g.fillRect(width-53, 3 + 7*colorSpacing, 50, colorSpacing-3);
g.setColor(Color.YELLOW);
g.fillRect(width-53, 3 + 8*colorSpacing, 50, colorSpacing-3);
g.setColor(new Color(125,000,125));
g.fillRect(width-53, 3 + 9*colorSpacing, 50, colorSpacing-3);*/
g.dispose();
}
private void setUpDrawingGraphics(int x,int y)
{ int wide = getSize().width;
int height = getSize().height;
int colorSpacing = (height - 56) / 10;
graphicsForDrawing = getGraphics();
/* if((x>wide-53)&&(x<wide-18)&&(y>height-53)&&(y<height-18))
{ repaint();
}
else if((x>wide-53)&&(x<wide-3)&&(y>3)&&(y<colorSpacing))
{ currentColor = Color.BLACK;
}
else if((x>wide-53)&&(x<wide-3)&&(y>3+colorSpacing)&&(y<2*colorSpacing-3))
{ currentColor = Color.BLUE;
}
else if((x>wide-53)&&(x<wide-3)&&(y>3 + 2*colorSpacing)&&(y<3*colorSpacing-3))
{ currentColor = Color.DARK_GRAY;
}
else if((x>wide-53)&&(x<wide-3)&&(y>3 + 3*colorSpacing)&&(y<4*colorSpacing-3))
{ currentColor = Color.GREEN;
}
else if((x>wide-53)&&(x<wide-3)&&(y>3+4*colorSpacing)&&(y<5*colorSpacing-3))
{ currentColor = Color.MAGENTA;
}
else if((x>wide-53)&&(x<wide-3)&&(y>3+5*colorSpacing)&&(y<6*colorSpacing-3))
{ currentColor = new Color(255,128,000);
}
else if((x>wide-53)&&(x<wide-3)&&(y>3+6*colorSpacing)&&(y<7*colorSpacing-3))
{ currentColor = Color.PINK;
}
else if((x>wide-53)&&(x<wide-3)&&(y>3+7*colorSpacing)&&(y<8*colorSpacing-3))
{ currentColor = Color.RED;
}
else if((x>wide-53)&&(x<wide-3)&&(y>3+8*colorSpacing)&&(y<9*colorSpacing-3))
{ currentColor = Color.YELLOW;
}
else if((x>wide-53)&&(x<wide-3)&&(y>3+9*colorSpacing)&&(y<10*colorSpacing-3))
{ currentColor = new Color(125,000,125);
}*/
}
public void mousePressed(MouseEvent e)
{ int x = e.getX();
int y = e.getY();
int width = getSize().width;
int height = getSize().height;
if (dragging == true)
return;
else if((x>width-56)&&(x<width)&&(y>0)&&(y<height))
setUpDrawingGraphics(x,y);
else if(x > 3 && x < width - 56 && y > 3 && y < height - 3)
{ prevX = x;
prevY = y;
dragging = true;
}
}
public void mouseReleased(MouseEvent e)
{ if (dragging == false)
return;
dragging = false;
graphicsForDrawing.dispose();
graphicsForDrawing = null;
g3=getGraphics();
Graphics2D g2=(Graphics2D) g3;
g2.setColor(currentColor);
if (command.equals("椭圆"))
{
tuoyuan.add(currenttuo);
g2.draw(currenttuo);
repaint();
}
else if (command.equals("矩形"))
{
juxing.add(currentju);
g2.draw(currentju);
repaint();
}
else if (command.equals("直线"))
{
zhixian.add(currentzhi);
g2.draw(currentzhi);
}
}
public void mouseDragged(MouseEvent e)
{ if (dragging == false)
return;
int x = e.getX(); // x-coordinate of mouse.
int y = e.getY(); // y=coordinate of mouse.
/*
if (x < 3) // Adjust the value of x,
x = 3; // to make sure it's in
if (x > getSize().width - 57) // the drawing area.
x = getSize().width - 57;
if (y < 3) // Adjust the value of y,
y = 3; // to make sure it's in
if (y > getSize().height - 4) // the drawing area.
y = getSize().height - 4;
Graphics g=getGraphics();
//int x=e.getX();int y=e.getY();
g.setColor(currentColor);
g.drawLine(lastX, lastY, x, y);
lastX=x;
lastY=y;
*/
if(command.equals("铅笔"))
{
Graphics g3=getGraphics();
Graphics2D g2=(Graphics2D) g3;
// px2=e.getX();
// py2=e.getY();
currentqian=new Line2D.Double(lastX,lastY, x, y);
g2.draw(currentqian);
lastX=x;
lastY=y;
qianbi.add(currentqian);
repaint();
}
else if (command.equals("直线"))
{
px2=e.getX();
py2=e.getY();
currentzhi=new Line2D.Double(x,y,px2,py2);
}
else if (command.equals("矩形"))
{
px2=e.getX();
py2=e.getY();
currentju=new Rectangle2D.Double();
currentju.setFrameFromDiagonal(x,y,px2,py2);
repaint();
}
else if (command.equals("椭圆"))
{
px2=e.getX();
py2=e.getY();
currenttuo=new Ellipse2D.Double();
currenttuo.setFrameFromDiagonal(x,y,px2,py2);
repaint();
}
graphicsForDrawing = getGraphics();
graphicsForDrawing.setColor(currentColor);
graphicsForDrawing.drawLine(prevX, prevY, x, y);
prevX = x; // Get ready for the next line segment in the curve.
prevY = y;
}
public void mouseEntered(MouseEvent evt) { }
public void mouseExited(MouseEvent evt) { }
public void mouseClicked(MouseEvent evt) { }
public void mouseMoved(MouseEvent evt) { }
public static void main(String[] args){
tuyaban m = new tuyaban();
//m.setVisible(true);
JFrame hello = new JFrame();
hello.setSize(300, 400);
hello.add(m);
hello.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -