📄 graphicspanel.java
字号:
if(toolState==5||toolState==9)
{
if(brushStyle==1)
g.setStroke(new BasicStroke(3.0f));
if(brushStyle==2)
g.setStroke(new BasicStroke(5.0f));
if(brushStyle==3)
g.setStroke(new BasicStroke(3.0f,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
if(brushStyle==4)
g.setStroke(new BasicStroke(5.0f,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
if(brushStyle==5)
g.setStroke(new BasicStroke(5.0f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL));
if(brushStyle==6)
g.setStroke(new BasicStroke(5.0f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER));
}
else
{
if(lineStyle==1);
g.setStroke(new BasicStroke(1.0f));
if(lineStyle==2)
g.setStroke(new BasicStroke(3.0f));
if(lineStyle==3)
g.setStroke(new BasicStroke(5.0f));
}
pointText.setText(currentPosX+","+currentPosY);
switch(toolState)
{
case 1:
{
repaint();
g.setColor(preColor);
g.drawLine(startPosX,startPosY,currentPosX,currentPosY);
break;
}
case 2:
{
repaint();
g.setColor(preColor);
g.drawLine(previousPosX,previousPosY,currentPosX,currentPosY);
break;
}
case 4:
{
g.setColor(preColor);
g.setStroke(new BasicStroke(1.0f));
g.drawLine(startPosX,startPosY,currentPosX,currentPosY);
Graphicsnum++;
Bgt[Graphicsnum][0]=1;
Bgt[Graphicsnum][1]=startPosX;
Bgt[Graphicsnum][2]=startPosY;
Bgt[Graphicsnum][3]=currentPosX;
Bgt[Graphicsnum][4]=currentPosY;
Bgt[Graphicsnum][5]=1;
colorChoice[Graphicsnum]=preColor;
startPosX = currentPosX;
startPosY = currentPosY;
break;
}
case 6:
{
repaint();
g.setColor(preColor);
graphicsHeight=Math.abs(currentPosY-startPosY);
graphicsWidth=Math.abs(currentPosX-startPosX);
top_leftPosX=Math.min(currentPosX,startPosX);
top_leftPosY=Math.min(currentPosY,startPosY);
if(paintStyle==2)
g.fillOval(top_leftPosX,top_leftPosY,graphicsWidth,graphicsHeight);
else
g.drawOval(top_leftPosX,top_leftPosY,graphicsWidth,graphicsHeight);
break;
}
case 5:
{
g.setColor(preColor);
g.drawLine(startPosX,startPosY,currentPosX,currentPosY);
Graphicsnum++;
Bgt[Graphicsnum][0]=1;
Bgt[Graphicsnum][1]=startPosX;
Bgt[Graphicsnum][2]=startPosY;
Bgt[Graphicsnum][3]=currentPosX;
Bgt[Graphicsnum][4]=currentPosY;
Bgt[Graphicsnum][5]=brushStyle+1;
colorChoice[Graphicsnum]=preColor;
startPosX = currentPosX;
startPosY = currentPosY;
break;
}
case 7:
{
repaint();
g.setColor(preColor);
graphicsHeight=Math.abs(currentPosY-startPosY);
graphicsWidth=Math.abs(currentPosX-startPosX);
top_leftPosX=Math.min(currentPosX,startPosX);
top_leftPosY=Math.min(currentPosY,startPosY);
if(paintStyle==2)
g.fillRect(top_leftPosX,top_leftPosY,graphicsWidth,graphicsHeight);
g.drawRect(top_leftPosX,top_leftPosY,graphicsWidth,graphicsHeight);
break;
}
case 9:
{
g.setColor(Color.white);
g.drawLine(startPosX,startPosY,currentPosX,currentPosY);
Graphicsnum++;
Bgt[Graphicsnum][0]=1;
Bgt[Graphicsnum][1]=startPosX;
Bgt[Graphicsnum][2]=startPosY;
Bgt[Graphicsnum][3]=currentPosX;
Bgt[Graphicsnum][4]=currentPosY;
Bgt[Graphicsnum][5]=brushStyle+1;
colorChoice[Graphicsnum]=Color.white;
startPosX = currentPosX;
startPosY = currentPosY;
break;
}
case 10:
{
/*repaint();
g.setColor(Color.black);
graphicsHeight=Math.abs(currentPosY-startPosY);
graphicsWidth=Math.abs(currentPosX-startPosX);
top_leftPosX=Math.min(currentPosX,startPosX);
top_leftPosY=Math.min(currentPosY,startPosY);
g.drawRect(top_leftPosX,top_leftPosY,graphicsWidth,graphicsHeight);
break;*/
}
}
}
public void mouseMoved(MouseEvent e){
currentPosX= e.getX();
currentPosY= e.getY();
pointText.setText(currentPosX+","+currentPosY);
}
});
addMouseListener(new MouseAdapter(){
public void mouseEntered(MouseEvent e){
if (toolState==6||toolState==7||toolState==1)
setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
else if(toolState==9)
setCursor(new Cursor(Cursor.MOVE_CURSOR));
else setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
public void mouseExited(MouseEvent e){
pointText.setText("");
}
public void mousePressed(MouseEvent e){
startPosX = e.getX();
startPosY = e.getY();
if(Pointnum==0)
{
previousPosX = e.getX();
previousPosY = e.getY();
polyPoint[++Pointnum][0]=previousPosX;
polyPoint[Pointnum][1]=previousPosY;
}
//f(getComponents()!=null)
//
//Bgt[Graphicsnum][4]=currentPosY;
// aTextField.setText("");
// remove(aTextField);
// repaint();
//
}
public void mouseReleased(MouseEvent e){
Graphics2D g=(Graphics2D)aPanel.getGraphics();
currentPosY = e.getY();
currentPosX = e.getX();
if(toolState==5||toolState==9)
{
if(brushStyle==1)
g.setStroke(new BasicStroke(3.0f));
if(brushStyle==2)
g.setStroke(new BasicStroke(5.0f));
if(brushStyle==3)
g.setStroke(new BasicStroke(3.0f,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
if(brushStyle==4)
g.setStroke(new BasicStroke(5.0f,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
if(brushStyle==5)
g.setStroke(new BasicStroke(5.0f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL));
if(brushStyle==6)
g.setStroke(new BasicStroke(5.0f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER));
}
else
{
if(lineStyle==1);
g.setStroke(new BasicStroke(1.0f));
if(lineStyle==2)
g.setStroke(new BasicStroke(3.0f));
if(lineStyle==3)
g.setStroke(new BasicStroke(5.0f));
}
switch(toolState)
{
case 1:
{
g.setColor(preColor);
g.drawLine(startPosX,startPosY,currentPosX,currentPosY);
Graphicsnum++;
Bgt[Graphicsnum][0]=1;
Bgt[Graphicsnum][1]=startPosX;
Bgt[Graphicsnum][2]=startPosY;
Bgt[Graphicsnum][3]=currentPosX;
Bgt[Graphicsnum][4]=currentPosY;
Bgt[Graphicsnum][5]=lineStyle;
colorChoice[Graphicsnum]=preColor;
break;
}
case 2:
{
polyPoint[++Pointnum][0]=currentPosX;
polyPoint[Pointnum][1]=currentPosY;
g.setColor(preColor);
g.drawLine(previousPosX,previousPosY,currentPosX,currentPosY);
Graphicsnum++;
Bgt[Graphicsnum][0]=1;
Bgt[Graphicsnum][1]=previousPosX;
Bgt[Graphicsnum][2]=previousPosY;
Bgt[Graphicsnum][3]=currentPosX;
Bgt[Graphicsnum][4]=currentPosY;
Bgt[Graphicsnum][5]=lineStyle;
colorChoice[Graphicsnum]=preColor;
previousPosX=currentPosX;
previousPosY=currentPosY;
break;
}
case 3:
{
Color oldColor;
try{
Robot r=new Robot();
positionX=currentPosX+aFrame.getX()+leftPanel.getWidth();
positionY=currentPosY+aFrame.getY()+aMenuBar.getHeight()+30;
oldColor=r.getPixelColor(positionX,positionY);
g.setColor(preColor);
pointText.setText(positionX+","+positionY);
areaFill(currentPosX,currentPosY,oldColor,preColor);
Graphicsnum++;
Bgt[Graphicsnum][0]=3;
Bgt[Graphicsnum][1]=currentPosX;
Bgt[Graphicsnum][2]=currentPosY;
//Bgt[Graphicsnum][3]=currentPosX;
//Bgt[Graphicsnum][4]=currentPosY;
//Bgt[Graphicsnum][5]=1;
colorChoice[Graphicsnum]=preColor;
}
catch(Exception error){
}
break;
}
case 4:
{
g.setColor(preColor);
g.setStroke(new BasicStroke(1.0f));
g.drawLine(startPosX,startPosY,currentPosX,currentPosY);
Graphicsnum++;
Bgt[Graphicsnum][0]=1;
Bgt[Graphicsnum][1]=startPosX;
Bgt[Graphicsnum][2]=startPosY;
Bgt[Graphicsnum][3]=currentPosX;
Bgt[Graphicsnum][4]=currentPosY;
Bgt[Graphicsnum][5]=1;
colorChoice[Graphicsnum]=preColor;
break;
}
case 5:
{
g.setColor(preColor);
g.drawLine(startPosX,startPosY,currentPosX,currentPosY);
Graphicsnum++;
Bgt[Graphicsnum][0]=1;
Bgt[Graphicsnum][1]=startPosX;
Bgt[Graphicsnum][2]=startPosY;
Bgt[Graphicsnum][3]=currentPosX;
Bgt[Graphicsnum][4]=currentPosY;
Bgt[Graphicsnum][5]=brushStyle+1;
colorChoice[Graphicsnum]=preColor;
startPosX = currentPosX;
startPosY = currentPosY;
break;
}
case 6:
{
g.setColor(preColor);
graphicsHeight=Math.abs(currentPosY-startPosY);
graphicsWidth=Math.abs(currentPosX-startPosX);
top_leftPosX=Math.min(currentPosX,startPosX);
top_leftPosY=Math.min(currentPosY,startPosY);
if(paintStyle==1)
g.drawOval(top_leftPosX,top_leftPosY,graphicsWidth,graphicsHeight);
else
g.fillOval(top_leftPosX,top_leftPosY,graphicsWidth,graphicsHeight);
Graphicsnum++;
Bgt[Graphicsnum][0]=6;
Bgt[Graphicsnum][1]=top_leftPosX;
Bgt[Graphicsnum][2]=top_leftPosY;
Bgt[Graphicsnum][3]=graphicsWidth;
Bgt[Graphicsnum][4]=graphicsHeight;
Bgt[Graphicsnum][5]=lineStyle;
Bgt[Graphicsnum][6]=paintStyle;
colorChoice[Graphicsnum]=preColor;
break;
}
case 7:
{
g.setColor(preColor);
graphicsHeight=Math.abs(currentPosY-startPosY);
graphicsWidth=Math.abs(currentPosX-startPosX);
top_leftPosX=Math.min(currentPosX,startPosX);
top_leftPosY=Math.min(currentPosY,startPosY);
if(paintStyle==1)
g.drawRect(top_leftPosX,top_leftPosY,graphicsWidth,graphicsHeight);
else
g.fillRect(top_leftPosX,top_leftPosY,graphicsWidth,graphicsHeight);
Graphicsnum++;
Bgt[Graphicsnum][0]=7;
Bgt[Graphicsnum][1]=top_leftPosX;
Bgt[Graphicsnum][2]=top_leftPosY;
Bgt[Graphicsnum][3]=graphicsWidth;
Bgt[Graphicsnum][4]=graphicsHeight;
Bgt[Graphicsnum][5]=lineStyle;
Bgt[Graphicsnum][6]=paintStyle;
colorChoice[Graphicsnum]=preColor;
break;
}
case 9:
{
g.setColor(Color.white);
g.drawLine(startPosX,startPosY,currentPosX,currentPosY);
Graphicsnum++;
Bgt[Graphicsnum][0]=1;
Bgt[Graphicsnum][1]=startPosX;
Bgt[Graphicsnum][2]=startPosY;
Bgt[Graphicsnum][3]=currentPosX;
Bgt[Graphicsnum][4]=currentPosY;
Bgt[Graphicsnum][5]=brushStyle+1;
//Bgt[Graphicsnum][5]=1;
colorChoice[Graphicsnum]=preColor;
startPosX = currentPosX;
startPosY = currentPosY;
break;
}
case 10:
{
/*repaint();
g.setColor(Color.black);
graphicsHeight=Math.abs(currentPosY-startPosY);
graphicsWidth=Math.abs(currentPosX-startPosX);
top_leftPosX=Math.min(currentPosX,startPosX);
top_leftPosY=Math.min(currentPosY,startPosY);
g.drawRect(top_leftPosX,top_leftPosY,graphicsWidth,graphicsHeight);
add(aTextField);
aTextField.setBounds(top_leftPosX,top_leftPosY,graphicsWidth,graphicsHeight);
getText=aTextField.getText();
Text[Textnum++]=getText;
Graphicsnum++;
Bgt[Graphicsnum][0]=10;
Bgt[Graphicsnum][1]=top_leftPosX;
Bgt[Graphicsnum][2]=top_leftPosY;
Bgt[Graphicsnum][3]=Textnum;*/
//
try{
Robot r=new Robot();
positionX=currentPosX+aFrame.getX()+leftPanel.getWidth();
positionY=currentPosY+aFrame.getY()+aMenuBar.getHeight()+25;
preColor=r.getPixelColor(positionX,positionY);
colorButton.setBackground(preColor);
}
catch(Exception e3)
{
}
break;
}
default: break;
}
//display(g);
}
});
}
public void areaFill(int x,int y,Color oldColor,Color fillColor) throws Exception
{
/*Graphics2D g=(Graphics2D)aPanel.getGraphics();
Robot r=new Robot();
positionX=x+aFrame.getX()+leftPanel.getWidth();
positionY=y+aFrame.getY()+aMenuBar.getHeight()+25;
Color c=r.getPixelColor(positionX,positionY);
g.setColor(fillColor);
if(c.equals(oldColor))
{
g.drawLine(x,y,x+1,y+1);
areaFill(x+1,y,oldColor,fillColor);
areaFill(x-1,y,oldColor,fillColor);
//areaFill(x,y+1,oldColor,fillColor);
//areaFill(x,y-1,oldColor,fillColor);
}*/
/*if(c.equals(Color.white))
c=Color.red;
g.setColor(c);
g.drawLine(10,10,20,20);*/
}
public void lastLine()
{
Graphics2D g=(Graphics2D)aPanel.getGraphics();
if(lineStyle==1);
g.setStroke(new BasicStroke(1.0f));
if(lineStyle==2)
g.setStroke(new BasicStroke(3.0f));
if(lineStyle==3)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -