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怎麼把圖片處理成圓形
程序中是沒有圓形的,都是按坐標放的,圓形其實是一張正方形的圖,四邊是透明,