改一下那個(gè)MyJpanel類代碼就可以啊
創(chuàng)新互聯(lián)建站專注于樟樹網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供樟樹營銷型網(wǎng)站建設(shè),樟樹網(wǎng)站制作、樟樹網(wǎng)頁設(shè)計(jì)、樟樹網(wǎng)站官網(wǎng)定制、小程序定制開發(fā)服務(wù),打造樟樹網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供樟樹網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
/*內(nèi)部繼承JPanel類*/
class MyJPanel extends JPanel {
protected void paintComponent(Graphics g) {
Color c = g.getColor();
g.setColor(Color.blue);
g.fillRect(0, 0, 600, 500);
g.setColor(Color.red);
g.fillOval(200, 100, 300, 300);
}
}
import java.awt.*;
import javax.swing.*;
public class TankWar extends JFrame {
public TankWar (String title) {
super(title);
this.setSize(600,500);
this.setLocationRelativeTo(null); //設(shè)置窗口的位置屏幕中間
this.setResizable(false); //設(shè)置為不可改變大小
this.setDefaultCloseOperation(EXIT_ON_CLOSE); //設(shè)置關(guān)閉事件
MyJPanel myJPanel = new MyJPanel(); //創(chuàng)建一個(gè)MyJPanel實(shí)例對象myJPanel
myJPanel.setBackground(Color.blue);//設(shè)置myJPanel的背景顏色
this.add(myJPanel);
this.setVisible(true);
}
/*內(nèi)部繼承JPanel類*/
class MyJPanel extends JPanel {
protected void paintComponent(Graphics g) {
Color c = g.getColor();
g.setColor(Color.red);
g.fillOval(200, 100, 300, 300);
g.setColor(c);
}
}
public static void main(String args[]){
new TankWar ("坦克大戰(zhàn)");
}
}
/*
*drawCircle.java
*拖動(dòng)鼠標(biāo)畫空心圓或?qū)嵭膱A
*/
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class drawCircle extends Applet implements MouseListener,MouseMotionListener
{
int x1,y1,x2,y2;
CheckboxGroup checkboxGroup1=new CheckboxGroup();
Checkbox checkbox1,checkbox2;
public void init(){
checkbox1=new Checkbox("畫空心圓",checkboxGroup1,true);
checkbox2=new Checkbox("畫實(shí)心圓",checkboxGroup1,false);
add(checkbox1);
add(checkbox2);
addMouseListener(this);
addMouseMotionListener(this);
}
public void paint(Graphics g){
if(checkbox1.getState()==true){//畫空心圓
int r=(int)Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
g.drawOval(x1,y1,r,r);
}
if(checkbox2.getState()==true){//畫實(shí)心圓
int r=(int)Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
g.fillOval(x1,y1,r,r);
}
}
//implementation of MouseListener
public void mousePressed(MouseEvent e){
x1=e.getX();
y1=e.getY();
}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
//implementation of MouseMotionEvent
public void mouseDragged(MouseEvent e){
x2=e.getX();
y2=e.getY();
repaint();
}
public void mouseMoved(MouseEvent e){}
}
使用java畫圓要用到繪圖類Graphics,下面是實(shí)例代碼和運(yùn)行效果:
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)行效果如下: