导航:首页 > 编程语言 > java图片圆形

java图片圆形

发布时间:2022-09-06 12:57:17

1. java如何在圆形中填充图片

给圆形的背景赋值为图片呗,有个设置背景的属性我记得。

2. java 怎么获取图片圆型区域里的所有像素点

首先,点动成线,也就是如果把一条线花短一点就可以类似一个点。其次,点的大小问题,就像windows自带的画图一样,点的大小是可以改变的,很灵活,用代码写很麻烦。在其次,Graphics虽说没有点,但是你画一个实心的圆就是点了

3. 求一个java程序:绘图程序包括画圆,椭圆,线,矩形,自定义。并且可以调图形颜色!

publicenumShapeTypes{
LINE,CIRCLE,RECTANGLE
}
publicinterfaceShape{
voidpaint(Graphicsg);
}
{

//矩形左上角的坐标
privateintx,y;
//矩形的宽度和高度
privateintwidth,height;

privateColorrectangleColor;

publicRectangle(){
super();
}

publicRectangle(intx,inty,intwidth,intheight,ColorrectangleColor){
super();
this.x=x;
this.y=y;
this.width=width;
this.height=height;
this.rectangleColor=rectangleColor;
}

@Override
publicvoidpaint(Graphicsg){
g.setColor(rectangleColor);
g.drawRect(x,y,width,height);
}
}
{

//直线的起始位置
privateintx1,y1;
//直线的终止位置
privateintx2,y2;

privateColorlineColor;

publicLine(intx1,inty1,intx2,inty2,ColorlineColor){
super();
this.x1=x1;
this.y1=y1;
this.x2=x2;
this.y2=y2;
this.lineColor=lineColor;
}

publicLine(){
super();
}

@Override
publicvoidpaint(Graphicsg){
g.setColor(lineColor);
g.drawLine(x1,y1,x2,y2);
}
}
{

//圆的颜色
privateColorcircleColor;
//圆心的坐标
privateintx,y;
//圆的半径
privateintradius;

publicCircle(){
super();
}

publicCircle(intx,inty,intradius,ColorcircleColor){
super();
this.circleColor=circleColor;
this.x=x;
this.y=y;
this.radius=radius;
}

@Override
publicvoidpaint(Graphicsg){
g.setColor(circleColor);
//画弧,当弧的宽度和高度一致且从0~360度时就是原形了
g.drawArc(x,y,radius,radius,0,360);
}
}
,MouseMotionListener{

=-5229161042153132522L;

//鼠标点击起始坐标和当前坐标
privateintbeginX=0,beginY=0,currentX=0,currentY=0;

//判断鼠标是否被按下
privatebooleanisMousePressing=false;

//保存当前的图形,在撤销和恢复时使用
privatefinalStack<Shape>currentShapes=newStack<Shape>();

//保存已经删除过的图形
privatefinalStack<Shape>deletedShapes=newStack<Shape>();

privateShapeTypestype;
privateColorcolor;

publicSketchpadPanel(){
addMouseListener(this);
addMouseMotionListener(this);
}

/**
*撤销方法
*/
publicvoindo(){
if(currentShapes.size()>0){
//从所有保存过的图形中取出最后一个,放入到已删除的图形中去
Shapeshape=currentShapes.pop();
deletedShapes.push(shape);
repaint();
}
}

/**
*恢复撤销方法
*/
publicvoidredo(){
if(deletedShapes.size()>0){
//从所有删除的图形中取出最后一个,放入保存的图形中
Shapeshape=deletedShapes.pop();
currentShapes.push(shape);
repaint();
}
}

/**
*设置命令
*
*@paramtype
*/
publicvoidsetShapeType(ShapeTypestype){
this.type=type;
}

/**
*设置颜色
*
*@paramcolor
*/
publicvoidsetColor(Colorcolor){
this.color=color;
}

publicvoipdete(Graphicsg){
paint(g);
}

/**
*绘制画板
*/
@Override
publicvoidpaint(Graphicsg){
//绘制画板
Dimensionsize=getSize();
intwidth=size.width;
intheight=size.height;
g.setColor(Color.WHITE);
g.fillRect(0,0,width,height);

//绘制所有图形
Shapeshape=null;
Enumeration<Shape>e=currentShapes.elements();
while(e.hasMoreElements()){
shape=e.nextElement();
shape.paint(g);
}

//如果当前鼠标没有释放
if(isMousePressing){
g.setColor(color);
switch(type){
//绘制直线
caseLINE:
g.drawLine(beginX,beginY,currentX,currentY);
break;
//绘制矩形
caseRECTANGLE:
if(currentX<beginX){
if(currentY<beginY){
//如果当前位置在起始位置的左上方,则以鼠标当前位置为矩形的左上角位置
g.drawRect(currentX,currentY,beginX-currentX,beginY-currentY);
}else{
//如果当前位置在起始位置的左下方,则以鼠标当前位置的横坐标和起始位置的纵坐标作为矩形的左上角位置
g.drawRect(currentX,beginY,beginX-currentX,currentY-beginY);
}
}else{
if(currentY<beginY){
//如果当前位置在起始位置的右上方,则以鼠标起始位置的很坐标和当前位置的纵坐标作为矩形的左上角位置
g.drawRect(beginX,currentY,currentX-beginX,beginY-currentY);
}else{
//如果当前位置在起始位置的右下方,则已起始位置作为矩形的左上叫位置
g.drawRect(beginX,beginY,currentX-beginX,currentY-beginY);
}
}
break;
//绘制圆形
caseCIRCLE:
//半径为a*a+b*b的平方根
intradius=(int)Math
.sqrt((beginX-currentX)*(beginX-currentX)+(beginY-currentY)*(beginY-currentY));
g.drawArc(beginX-radius/2,beginY-radius/2,radius,radius,0,360);
break;
}
}

}

@Override
publicvoidmouseClicked(MouseEvente){
}

@Override
publicvoidmouseEntered(MouseEvente){
}

@Override
publicvoidmouseExited(MouseEvente){
}

/**
*当鼠标按下的时候获得起始坐标
*/
@Override
publicvoidmousePressed(MouseEvente){
beginX=e.getX();
beginY=e.getY();
isMousePressing=true;
}

/**
*当鼠标释放时获得当前坐标
*/
@Override
publicvoidmouseReleased(MouseEvente){
currentX=e.getX();
currentY=e.getY();
isMousePressing=false;

//当释放鼠标时,将绘制的图形保存到shapes中
switch(type){
//绘制直线
caseLINE:
Lineline=newLine(beginX,beginY,currentX,currentY,color);
currentShapes.push(line);
break;
//绘制圆形
caseCIRCLE:
//半径为a*a+b*b的平方根
intradius=(int)Math
.sqrt((beginX-currentX)*(beginX-currentX)+(beginY-currentY)*(beginY-currentY));
Circlecircle=newCircle(beginX-radius/2,beginY-radius/2,radius,color);
currentShapes.push(circle);
break;
//绘制矩形
caseRECTANGLE:
Rectanglerectangle=null;
if(currentX<beginX){
if(currentY<beginY){
rectangle=newRectangle(currentX,currentY,beginX-currentX,beginY-currentY,color);
}else{
rectangle=newRectangle(currentX,beginY,beginX-currentX,currentY-beginY,color);
}
}else{
if(currentY<beginY){
rectangle=newRectangle(beginX,currentY,currentX-beginX,beginY-currentY,color);
}else{
rectangle=newRectangle(beginX,beginY,currentX-beginX,currentY-beginY,color);
}
}
currentShapes.push(rectangle);
break;
}

repaint();
}

@Override
publicvoidmouseDragged(MouseEvente){
currentX=e.getX();
currentY=e.getY();
this.repaint();
}

@Override
publicvoidmouseMoved(MouseEvente){
}
}
{

=-7080053971741609904L;

=newJPanel();//存放命令的面板
privatefinalJPanelcolorPanel=newJPanel();//存放颜色的面板
privatefinalJPanelmainPanel=newJPanel();//主面板

privatefinalJButtonredButton=newJButton("红色");
privatefinalJButtonblueButton=newJButton("蓝色");
=newJButton("绿色");

privatefinalJButtonlineButton=newJButton("直线");
=newJButton("圆");
=newJButton("矩形");

privatefinalJButtonundoButton=newJButton("撤销");
privatefinalJButtonredoButton=newJButton("恢复撤销");
privatefinalJButtonexitButton=newJButton("退出");

SketchpadPanelsketchPanel=newSketchpadPanel();

privatevoidinitFrame(){
commandPanel.setLayout(newFlowLayout());
commandPanel.add(lineButton);
commandPanel.add(circleButton);
commandPanel.add(rectangleButton);
commandPanel.add(undoButton);
commandPanel.add(redoButton);
commandPanel.add(exitButton);

colorPanel.setLayout(newFlowLayout());
colorPanel.add(redButton);
colorPanel.add(blueButton);
colorPanel.add(greenButton);

mainPanel.setLayout(newBorderLayout());
mainPanel.add(commandPanel,BorderLayout.NORTH);
mainPanel.add(colorPanel,BorderLayout.CENTER);

getContentPane().add("South",mainPanel);
getContentPane().add("Center",sketchPanel);

//初始化设置:颜色和命令
lineButton.setForeground(Color.RED);
sketchPanel.setColor(Color.RED);
redButton.setForeground(Color.RED);

sketchPanel.setShapeType(ShapeTypes.LINE);
}

privatevoidinitListener(){
redButton.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
redAction(e);
}
});
blueButton.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
blueAction(e);
}
});
greenButton.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
greenAction(e);
}
});

undoButton.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
undoAction(e);
}
});
redoButton.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
redoAction(e);
}
});
exitButton.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
exitAction(e);
}
});

lineButton.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
lineAction(e);
}
});
circleButton.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
circleAction(e);
}
});
rectangleButton.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
rectangleAction(e);
}
});
}

publicSketchpadFrame(){
initFrame();
initListener();
this.setSize(500,600);
setLocationByPlatform(true);
setResizable(true);
}

/*********************处理事件**********************/
privatevoindoAction(ActionEvente){
sketchPanel.undo();
}

privatevoidredoAction(ActionEvente){
sketchPanel.redo();
}

privatevoidexitAction(ActionEvente){
System.exit(0);
}

privatevoidlineAction(ActionEvente){
//选中按钮为红色,其余为黑色
lineButton.setForeground(Color.RED);
circleButton.setForeground(Color.BLACK);
rectangleButton.setForeground(Color.BLACK);
sketchPanel.setShapeType(ShapeTypes.LINE);
}

privatevoidcircleAction(ActionEvente){
circleButton.setForeground(Color.RED);
lineButton.setForeground(Color.BLACK);
rectangleButton.setForeground(Color.BLACK);
sketchPanel.setShapeType(ShapeTypes.CIRCLE);
}

privatevoidrectangleAction(ActionEvente){
rectangleButton.setForeground(Color.RED);
lineButton.setForeground(Color.BLACK);
circleButton.setForeground(Color.BLACK);
sketchPanel.setShapeType(ShapeTypes.RECTANGLE);
}

privatevoidredAction(ActionEvente){
redButton.setForeground(Color.RED);
blueButton.setForeground(Color.BLACK);
greenButton.setForeground(Color.BLACK);
sketchPanel.setColor(Color.RED);
}

privatevoidblueAction(ActionEvente){
blueButton.setForeground(Color.RED);
redButton.setForeground(Color.BLACK);
greenButton.setForeground(Color.BLACK);
sketchPanel.setColor(Color.BLUE);
}

privatevoidgreenAction(ActionEvente){
greenButton.setForeground(Color.RED);
redButton.setForeground(Color.BLACK);
blueButton.setForeground(Color.BLACK);
sketchPanel.setColor(Color.GREEN);
}
}
/**
*
*@author不落的太阳(SeanYang)
*@version1.0
*@sinceJDK1.8
*
*/
publicclassSketchpadMain{

/**
*测试方法
*
*@paramargs命令行参数
*/
publicstaticvoidmain(String[]args){
EventQueue.invokeLater(newRunnable(){
@Override
publicvoidrun(){
JFrameframe=newSketchpadFrame();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
}

4. java怎么将一个图像画到一个指定的圆形区域

看 java.awt.Graphics 的 API文档

~
~
~

5. java如何在圆形中加入图片

加载图片,,,,,,Graphics 有方法drawImage

~
~
~
~
~

6. 在Java中如何用程序画一个圆

使用java画圆要用到绘图类Graphics,下面是实例代码和运行效果:

packagecom.dikea.demo01;

importjava.awt.*;

importjavax.swing.*;

//java绘图原理

publicclassdemo_01extendsJFrame{

MyPanelmp=null;

publicstaticvoidmain(String[]args){

//TODO自动生成的方法存根

demo_01demo01=newdemo_01();

}

publicdemo_01(){

mp=newMyPanel();

this.add(mp);

this.setSize(400,300);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

}

}

//定义一个MyPanel面板,用于绘图区域

classMyPanelextendsJPanel{

//覆盖JPanel

//Graphics是绘图的重要类,可以理解成一支画笔

publicvoidpaint(Graphicsg){

//1.调用父类函数完成初始化任务

//这句话不可以少

super.paint(g);

//先画出一个圆圈

g.drawOval(100,100,30,30);

}

}

代码复制进ide编程工具,运行效果如下:

7. JAVA中在界面中画了几个图形,比如一个圆形,一个正方形,如果只想选取圆形怎么做

不可能的。如果是自己做的画图工具,那么再画的时候记录所有的操作,选择时根据算法判断选中了哪里。再处理,那么就相当于其他部分重新画。
如果直接是图片,那么就做不到了。

8. JAVA怎么把图片处理成圆形

程序中是没有圆形的,都是按坐标放的,圆形其实是一张正方形的图,四边是透明,

阅读全文

与java图片圆形相关的资料

热点内容
androidm3u8缓存 浏览:234
imphp开源知乎 浏览:706
清除网络通配符dos命令 浏览:837
鸿蒙系统怎么快速换回安卓 浏览:712
pdf绿色虚拟打印机 浏览:213
androidtab框架 浏览:147
java转php的时间戳 浏览:639
编译libstdc依赖 浏览:659
清算法人与原法人的区别 浏览:410
家庭装修下载什么app软件 浏览:575
美食博主用什么app拍视频 浏览:816
ipone手机如何加密微信 浏览:357
自来水加密阀阀帽 浏览:438
华为交换机dhcp配置命令 浏览:319
androidbitmap缩小 浏览:275
单片机串口控制灯 浏览:88
大讯云服务器安装视频 浏览:788
华为算法领先世界 浏览:658
linux路由重启 浏览:570
php的模板编程 浏览:324