📄 huatu_program.java
字号:
R = colorarray[count][0];
G = colorarray[count][1];
B = colorarray[count][2];
itemList[index].R=R;
itemList[index].G=G;
itemList[index].B=B;
}
}
}
}
public class MouseEventA extends MouseAdapter
{
public void mouseReleased( MouseEvent e )
{
text.setText("鼠标释放:[" + e.getX() + ", " + e.getY() + "]");
if(currentChoice == 0 || currentChoice == 1)
{
itemList[index].x1 = e.getX();
itemList[index].y1 = e.getY();
}
if(huabucolor_Choice == 10)
{
huabucolor_Choice ++;
}
else
{
itemList[index].x2=e.getX();
itemList[index].y2=e.getY();
a = e.getX();
b = e.getY();
repaint();
index ++;
createNew();
}
}
public void mousePressed( MouseEvent e )
{
text.setText("鼠标点击:[" + e.getX() + ", " + e.getY() + "]");
itemList[index].x1 = itemList[index].x2 = e.getX();
itemList[index].y1 = itemList[index].y2 = e.getY();
if(currentChoice ==0 || currentChoice == 1)
{
itemList[index].x1=itemList[index].x2=e.getX();
itemList[index].y1=itemList[index].y2=e.getY();
index ++;
createNew();
}
if(huabucolor_Choice == 10)
{
drawingArea.setBackground(new Color(R,G,B));
R = G = B = 0;
itemList[index].R=R;
itemList[index].G=G;
itemList[index].B=B;
}
if(currentChoice == 2)
{
if(record == 1)
{
itemList[index].x1 = e.getX();
itemList[index].y1 = e.getY();
record ++;
}
else
{
itemList[index].x1 = a;
itemList[index].y1 = b;
}
}
}
public void mouseEntered( MouseEvent e )
{
text.setText("鼠标进入:[" + e.getX() + ", " + e.getY() + "]");
}
public void mouseExited( MouseEvent e )
{
text.setText("鼠标离开:[" + e.getX() + ", " + e.getY() + "]");
}
}
public class MouseEventB extends MouseMotionAdapter
{
public void mouseDragged( MouseEvent e )
{
text.setText("鼠标拖动:[" + e.getX() + ", " + e.getY() + "]");
if(currentChoice == 0 || currentChoice == 1)
{
itemList[index-1].x1 = itemList[index].x2 = itemList[index].x1 = e.getX();
itemList[index-1].y1 = itemList[index].y2 = itemList[index].y1 = e.getY();
index ++;
createNew();
}
else
{
itemList[index].x2 = e.getX();
itemList[index].y2 = e.getY();
}
repaint();
}
public void mouseMoved( MouseEvent e )
{
text.setText("鼠标移动:[" + e.getX() + ", " + e.getY() + "]");
}
}
public class DrawPanel extends JPanel
{
public DrawPanel()
{
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
setBackground(Color.white);
addMouseListener(new MouseEventA());
addMouseMotionListener(new MouseEventB());
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
int i = 0;
while(i <= index)
{
drawPicter(g2,itemList[i]);
i ++;
}
}
public void drawPicter(Graphics2D g2,Drawings draws)
{
draws.Draw(g2);
}
}
public void createNew()
{
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
switch(currentChoice)
{
case 0:
itemList[index] = new Rubber();
record = 1;
break;
case 1:
itemList[index] = new Pencil();
record = 1;
break;
case 2:
itemList[index] = new Quxian();
break;
case 3:
itemList[index] = new Line();
record = 1;
break;
case 4:
itemList[index] = new FillRect();
record = 1;
break;
case 5:
itemList[index] = new Rect();
record = 1;
break;
case 6:
itemList[index] = new FillOval();
record = 1;
break;
case 7:
itemList[index] = new Oval();
record = 1;
break;
case 8:
itemList[index] = new RoundRect();
record = 1;
break;
}
itemList[index].drawType=currentChoice;
itemList[index].R=R;
itemList[index].G=G;
itemList[index].B=B;
itemList[index].stroke=stroke;
}
public void setHuabi()
{
String input;
input=JOptionPane.showInputDialog("请输入画笔的粗细大小(>0)");
stroke = Float.parseFloat(input);
itemList[index].stroke=stroke;
}
public void colorChoose()
{
color=JColorChooser.showDialog(Huatu_Program.this,"编辑颜色",color);
R=color.getRed();
G=color.getGreen();
B=color.getBlue();
itemList[index].R=R;
itemList[index].G=G;
itemList[index].B=B;
}
public static void main(String args[])
{
try
{
UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName());
}
catch ( Exception e ) {}
Huatu_Program hp=new Huatu_Program();
hp.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
class Drawings
{
int x1,y1;
int x2,y2;
int R,G,B;
int drawType;
float stroke;
void Draw(Graphics2D g2){};
}
class Rubber extends Drawings
{
public void Draw(Graphics2D g2)
{
g2.setPaint(new Color(255,255,255));
g2.setStroke(new BasicStroke(stroke));
g2.drawLine(x1,y1,x2,y2);
}
}
class Pencil extends Drawings
{
public void Draw(Graphics2D g2)
{
g2.setPaint(new Color(R,G,B));
g2.setStroke(new BasicStroke(stroke));
g2.drawLine(x1,y1,x2,y2);
}
}
class Quxian extends Drawings
{
public void Draw(Graphics2D g2)
{
g2.setPaint(new Color(R,G,B));
g2.setStroke(new BasicStroke(stroke));
g2.drawLine(x1,y1,x2,y2);
}
}
class Line extends Drawings
{
public void Draw(Graphics2D g2)
{
g2.setPaint(new Color(R,G,B));
g2.setStroke(new BasicStroke(stroke));
g2.drawLine(x1,y1,x2,y2);
}
}
class Rect extends Drawings
{
public void Draw(Graphics2D g2)
{
g2.setPaint(new Color(R,G,B));
g2.setStroke(new BasicStroke(stroke));
g2.drawRect(Math.min(x1,x2),Math.min(y1,y2),Math.abs(x1-x2),Math.abs(y1-y2));
}
}
class FillRect extends Drawings
{
public void Draw(Graphics2D g2)
{
g2.setPaint(new Color(R,G,B));
g2.setStroke(new BasicStroke(stroke));
g2.fillRect(Math.min(x1,x2),Math.min(y1,y2),Math.abs(x1-x2),Math.abs(y1-y2));
}
}
class Oval extends Drawings
{
public void Draw(Graphics2D g2)
{
g2.setPaint(new Color(R,G,B));
g2.setStroke(new BasicStroke(stroke));
g2.drawOval(Math.min(x1,x2),Math.min(y1,y2),Math.abs(x1-x2),Math.abs(y1-y2));
}
}
class FillOval extends Drawings
{
public void Draw(Graphics2D g2)
{
g2.setPaint(new Color(R,G,B));
g2.setStroke(new BasicStroke(stroke));
g2.fillOval(Math.min(x1,x2),Math.min(y1,y2),Math.abs(x1-x2),Math.abs(y1-y2));
}
}
class RoundRect extends Drawings
{
public void Draw(Graphics2D g2)
{
g2.setPaint(new Color(R,G,B));
g2.setStroke(new BasicStroke(stroke));
g2.drawRoundRect(Math.min(x1,x2),Math.min(y1,y2),Math.abs(x1-x2),Math.abs(y1-y2),30,30);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -