Hihi 發問時間： 電腦與網際網路程式設計 · 5 年前

# JFrame 內用JPanel繪圖

"背景要設成白色"

import java.awt.*;

import java.awt.geom.*;

import javax.swing.*;

class DrawPicture extends JFrame

{

private DrawPic dpan;

public DrawPicture()

{

dpan=new DrawPic();

dpan.setBounds(0,0,500,400);

dpan.setBackground(Color.white);

setTitle("繪製圖形");

setVisible(true);

setLayout(null);

setBounds(400,150,500,400);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

class DrawPic extends JPanel

{

public void paintComponent(Graphics g)

{

Graphics2D g2=(Graphics2D)g;

g2.setColor(Color.red);

//distence between circle & line =10

g2.draw(new Line2D.Double(85,75-25-10,85,75-25-10-30));//up Line

g2.draw(new Line2D.Double(85,75+25+10,85,75+25+10+30));//Down Line

g2.draw(new Line2D.Double(85-25-10,75,85-25-10-30,75));//Left Line

g2.draw(new Line2D.Double(85+25+10,75,85+25+10+30,75));//Righ Line

//Draw house

g2.setColor(Color.black);

g2.draw(new Rectangle2D.Double(200,200,150,50));

int x[]={200,200+30,200+30+150,200+150};

int y[]={200,200-40,200-40,200};

g2.drawPolygon(x,y,4);

g2.drawLine(350,250,350+30+20,250-20);//rectangle right-botton point =(350,250)

g2.drawLine(350+30+20,250-20,350+30+20,250-20-50);

g2.drawLine(350+30+20,250-20-50,200+30+150,200-40);//Polygon right-top point=(200+30+150,200-40)

}

}

}

public class Q10_2 extends JFrame

{

public static void main (String[] args)

{

new DrawPicture();

}

}

### 1 個解答

• 吉他
Lv 5
5 年前
最佳解答

import java.awt.*;

import java.awt.geom.*;

import javax.swing.*;

class DrawPicture extends JFrame {

private DrawPic dpan;

public DrawPicture() {

//…

}

class DrawPic extends JPanel {

public void paintComponent(Graphics g) {

Graphics2D g2 = (Graphics2D) g;

g2.setColor(Color.WHITE);

g2.fillRect(0, 0, getWidth(), getHeight());

g2.setColor(Color.red);

g2.fill(new Ellipse2D.Double(60, 50, 50, 50)); // (xo,yo)=(85,75),radian=25,Diameter=50

// distence between circle & line =10

g2.draw(new Line2D.Double(85, 75 - 25 - 10, 85, 75 - 25 - 10 - 30));// up

// Line

g2.draw(new Line2D.Double(85, 75 + 25 + 10, 85, 75 + 25 + 10 + 30));// Down

// Line

g2.draw(new Line2D.Double(85 - 25 - 10, 75, 85 - 25 - 10 - 30, 75));// Left

// Line

g2.draw(new Line2D.Double(85 + 25 + 10, 75, 85 + 25 + 10 + 30, 75));// Righ

// Line

// Draw house

g2.setColor(Color.black);

g2.draw(new Rectangle2D.Double(200, 200, 150, 50));

int x[] = { 200, 200 + 30, 200 + 30 + 150, 200 + 150 };

int y[] = { 200, 200 - 40, 200 - 40, 200 };

g2.drawPolygon(x, y, 4);

g2.drawLine(350, 250, 350 + 30 + 20, 250 - 20);// rectangle

// right-botton

// point =(350,250)

g2.drawLine(350 + 30 + 20, 250 - 20, 350 + 30 + 20, 250 - 20 - 50);

g2.drawLine(350 + 30 + 20, 250 - 20 - 50, 200 + 30 + 150, 200 - 40);// Polygon

// right-top

// point=(200+30+150,200-40)

}

}

public static void main(String[] args) {

new DrawPicture();

}

}

說明:紅色字體是增加著色背景為白色的程式，在swing裡panel可以畫圖片、或是矩形、圓形等等，先畫上去的會在最下面，所以你說的背景要先畫就可以了，而g2.setColor()，你可以想像成是畫筆，你設定白色之後再去畫圓、線都會是你白色，之後再set一次color紅色，就會變成畫紅色。

而你提出的問題是:

可否順便說明一下JFrame的層次，每一層的功能和如何設定?