使用java畫圓要用到繪圖類Graphics,下面是實(shí)例代碼和運(yùn)行效果:
網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、重慶小程序開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了延津免費(fèi)建站歡迎大家使用!
package?com.dikea.demo01;
import?java.awt.*;
import?javax.swing.*;
//?java繪圖原理
public?class?demo_01??extends?JFrame?{
MyPanel?mp?=?null;
public?static?void?main(String[]?args)?{
//?TODO?自動(dòng)生成的方法存根
demo_01?demo01?=?new?demo_01();
}
public?demo_01(){
mp?=?new?MyPanel();
this.add(mp);
this.setSize(400,?300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
//?定義一個(gè)MyPanel面板,用于繪圖區(qū)域
class?MyPanel?extends?JPanel{
//覆蓋JPanel
//?Graphics?是繪圖的重要類,可以理解成一支畫筆
public?void?paint(Graphics?g){
//??1.?調(diào)用父類函數(shù)完成初始化任務(wù)
//??這句話不可以少
super.paint(g);
//?先畫出一個(gè)圓圈
g.drawOval(100,?100,?30,?30);
}
}
代碼復(fù)制進(jìn)ide編程工具,運(yùn)行效果如下:
Graphics類提供基本的幾何圖形繪制方法,主要有:畫線段、畫矩形、畫圓、畫帶顏色的圖形、畫橢圓、畫圓弧、畫多邊形等。
1. 畫線
在窗口畫一條線段,可以使用Graphics類的drawLine()方法:
drawLine(int x1,int y1,int x2,int y2)
例如,以下代碼在點(diǎn)(3,3)與點(diǎn)(50,50)之間畫線段,在點(diǎn)(100,100)處畫一個(gè)點(diǎn)。
g.drawLine(3,3,50,50);//畫一條線段 g.drawLine(100,100,100,100);//畫一個(gè)點(diǎn)。
可以調(diào)用draw方法, 但是你就這樣寫,無法直觀的顯示出來. 要想真的顯示出來 ,你需要在面板上繪制,并添加到窗口上.
下面是參考代碼
import?java.awt.EventQueue;
import?java.awt.Graphics;
import?java.util.ArrayList;
import?javax.swing.JFrame;
import?javax.swing.JPanel;
//宇宙類?:?主窗口,用于顯示數(shù)據(jù)
public?class?Test?extends?JFrame?{
public?Test()?{
ArrayListStar?stars?=?new?ArrayList();
Star?sun?=?new?Star(120,?120);
stars.add(sun);
//stars.add(new?Star(50,?50));//還可以添加其他星星
Sky?sky?=?new?Sky(stars);
add(sky);
setTitle("果殼中的宇宙");
setSize(380,?380);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public?static?void?main(String[]?args)?{
EventQueue.invokeLater(new?Runnable()?{
public?void?run()?{
new?Test().setVisible(true);
}
});
}
}
//天空類?:?可以有很多的星星?,并且可以繪制出來
class?Sky?extends?JPanel?{
private?ArrayListStar?stars;//用于存放星星
public?Sky(ArrayListStar?stars)?{
this.stars?=?stars;
}
@Override
protected?void?paintComponent(Graphics?g)?{
super.paintComponent(g);
for?(Star?star?:?stars)?{
star.draw(g);//調(diào)用星星的draw方法?來繪制星星
}
}
}
//星星?類?:?可以設(shè)置?xy的位置
class?Star?{
int?x,?y;
public?Star(int?x,?int?y)?{?//?構(gòu)造方法
this.x?=?x;
this.y?=?y;
}
public?void?draw(Graphics?g)?{
//g.drawRect()?這是繪制矩形的星星
g.drawOval(this.x,?this.y,?50,?50);//繪制一個(gè)圓形的星星
}
}
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class ResponseToMouse extends Applet implements ActionListener,MouseListener,MouseMotionListener
{
int xPos,yPos,k=0;
Button btnDraw,btnClean,btnErase;
public void init()
{
btnDraw = new Button("畫圖");
btnClean = new Button("清屏");
btnErase = new Button("擦除");
add(btnDraw);
add(btnClean);
add(btnErase);
btnDraw.addActionListener(this);
btnClean.addActionListener(this);
btnErase.addActionListener(this);
addMouseListener(this);
addMouseMotionListener(this);
}
public void paint(Graphics g)
{
if(k==1)
{
g.setColor(Color.red);
g.fillOval(xPos-5,yPos-5,10,10);
}
if(k==2)
{
super.update(g);
}
if(k==3)
{
g.setColor(Color.white);
g.fillOval(xPos-5,yPos-5,10,10);
}
}
public void actionPerformed(ActionEvent e){
if (e.getSource()==btnDraw)
k=1;
if(e.getSource()==btnClean){
k=2;
repaint();
}
if(e.getSource()==btnErase)
k=3;
}
public void mouseClicked(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
showStatus("鼠標(biāo)鍵按下");
}
public void mouseReleased(MouseEvent e)
{
showStatus("鼠標(biāo)鍵釋放");
}
public void mouseEntered(MouseEvent e)
{
showStatus("鼠標(biāo)進(jìn)入");
}
public void mouseExited(MouseEvent e)
{
showStatus("鼠標(biāo)移出");
}
public void mouseDragged(MouseEvent e)
{
xPos=e.getX();
yPos=e.getY();
repaint();
}
public void mouseMoved(MouseEvent e)
{
showStatus("鼠標(biāo)移動(dòng)");
}
public void update(Graphics g){
paint(g);
}
}
使用Java的Graphics類進(jìn)行繪圖
Graphics類提供基本的幾何圖形繪制方法,主要有:畫線段、畫矩形、畫圓、畫帶顏色的圖形、畫橢圓、畫圓弧、畫多邊形等
我基于你原來畫圖的方法,添加了事件觸發(fā)的命令b[j].setActionCommand("b" + j);否則你不能在事件響應(yīng)處理的方法中使用e.getActionCommand(),而且字符串的比較用equals方法比較好?,F(xiàn)在可以運(yùn)行了,你可以看一下:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class drawing extends Applet implements ActionListener {
Button b[] = new Button[5];
String fontname = "仿宋_GB2312";
int style = Font.PLAIN;
int size = 24;
int index = 0;
Font myfont;
public void init() {
setSize(700,700);
myfont = new Font(fontname, style, size);
b[0] = new Button("扇形");
b[1] = new Button("圓形");
b[2] = new Button("三角形");
b[3] = new Button("長方形");
b[4] = new Button("橢圓形");
for (int j = 0; j b.length; j++) {
b[j].setBounds(10, 10, 50, 20);
b[j].addActionListener(this);
b[j].setActionCommand("b" + j);
add(b[j]);
}
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("b0")) {
index = 0;
repaint();
}
if (e.getActionCommand().equals("b1")) {
index = 1;
repaint();
}
if (e.getActionCommand().equals("b2")) {
index = 2;
repaint();
}
if (e.getActionCommand().equals("b3")) {
index = 3;
repaint();
}
if (e.getActionCommand().equals("b4")) {
index = 4;
repaint();
}
}
public void paint(Graphics g) {
switch (index) {
case 0:
g.fillArc(0, 60, 80, 60, 30, 120);
break;
case 1:
g.drawOval( 300, 50, 60, 60);
break;
case 2:
Polygon filledPolygon = new Polygon();
filledPolygon.addPoint(380, 50);
filledPolygon.addPoint(380, 110);
filledPolygon.addPoint(450, 90);
g.drawPolygon(filledPolygon);
break;
case 3:
g.drawRect( 200, 50, 80, 60);
break;
case 4:
g.drawOval(100, 50, 80, 60);
break;
default:
g.fillArc(0, 60, 80, 60, 30, 120);
break;
}
}
/*
* public void paint(Graphics g) { g.fillArc( 0, 60, 80, 60, 30, 120);
* //繪制扇形 g.drawOval( 100, 50, 80, 60); g.drawRect( 200, 50, 80, 60);
* g.drawOval( 300, 50, 60, 60); Polygon filledPolygon=new Polygon();
* filledPolygon.addPoint(380,50); filledPolygon.addPoint(380,110);
* filledPolygon.addPoint(450,90); g.drawPolygon(filledPolygon); }
*/
}