java繪圖基礎 : 用java程式碼畫一個房子

殷丿grd_志鵬發表於2020-12-19
網頁左邊,向下滑有目錄索引,可以根據標題跳轉到你想看的內容

一、概述

java繪圖的主要核心類
1、Graphics類,此類可以畫圖形,線條等,但是設定到變形等就不行了,由此衍生出更強大的類,Graphics2D
2、Graphics2D,更加強大的繪圖類

1、繪製基本圖形

繪圖方法

在這裡插入圖片描述
在這裡插入圖片描述
在這裡插入圖片描述

package com.test;

import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Rectangle2D;

import javax.swing.JFrame;

public class Test extends JFrame{
	public Test() {
		setBounds(500,300, 800, 500);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		MyCanvas myCanvas = new MyCanvas();
		add(myCanvas);
	}
	public static void main(String[] args) {
		new Test();
	}
	private class MyCanvas extends Canvas{ //Canvas類,是畫布,我們沒有紙,只有筆,也沒辦法畫
		@Override
		public void paint(Graphics g) {
			// TODO Auto-generated method stub
			Graphics2D g2d = (Graphics2D)g;
			g2d.drawOval(5, 5, 100, 100);//畫圓
			g2d.drawRect(15,15,80,80);//畫矩形
			g2d.fillRect(45, 45,80,80);//填充矩形
			g2d.drawArc(320,25,100,50,270,200);//弧線
			g2d.drawLine(5,120,100,120);//直線
			g2d.drawRoundRect(102, 120, 100, 50, 10, 10);//圓角矩形
			
			//畫多邊形,第一個引數是橫座標陣列,第二個是縱座標陣列,第三個是有幾個點
			int x[]= {250,300,250,300};
			int y[]= {130,130,200,200};
			
//			g2d.drawPolygon(x, y, 4);
			
			//畫多邊線(測試多邊線需要註釋上面的程式碼,因為用的引數一樣重合了)
			
			g2d.drawPolyline(x, y, 4);
			//上面都是Graphics的方法,下面介紹2d是如何運作的
			
			Shape shape = new Rectangle2D.Double(110,5,100,100);
			
			g2d.draw(shape);
			g2d.fill(shape);
		}
	}
}

2、設定畫筆顏色

在這裡插入圖片描述
在這裡插入圖片描述

package com.test;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Rectangle2D;

import javax.swing.JFrame;

public class Test extends JFrame{
	public Test() {
		setBounds(500,300, 800, 500);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		MyCanvas myCanvas = new MyCanvas();
		add(myCanvas);
	}
	public static void main(String[] args) {
		new Test();
	}
	private class MyCanvas extends Canvas{
		@Override
		public void paint(Graphics g) {
			// TODO Auto-generated method stub
			Graphics2D g2d = (Graphics2D)g;
			g2d.fillRect(5,120,100,120);//直線
			Color blue =Color.BLUE;
			g2d.setColor(blue);
			g2d.fillRect(125,125,103,125);
		}
	}
}

3、設定畫筆(粗細)

在這裡插入圖片描述
在這裡插入圖片描述在這裡插入圖片描述在這裡插入圖片描述在這裡插入圖片描述

package com.test;

import java.awt.BasicStroke;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.Rectangle2D;

import javax.swing.JFrame;

public class Test extends JFrame{
	public Test() {
		setBounds(500,300, 800, 500);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		MyCanvas myCanvas = new MyCanvas();
		add(myCanvas);
	}
	public static void main(String[] args) {
		new Test();
	}
	private class MyCanvas extends Canvas{
		@Override
		public void paint(Graphics g) {
			// TODO Auto-generated method stub
			Graphics2D g2d = (Graphics2D)g;
			g2d.drawLine(5, 120, 100, 120);
			Stroke stroke = new BasicStroke(10);//1、建立畫筆物件
			g2d.setStroke(stroke);//2、設定畫筆
			g2d.drawLine(5, 150, 100, 150);//畫
		}
	}
}

4、畫一個房子

在這裡插入圖片描述

package com.test;

import java.awt.BasicStroke;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.Rectangle2D;
import java.util.Random;

import javax.swing.JFrame;

public class Test extends JFrame{
	public Test() {
		setBounds(500,300, 800, 500);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		getContentPane().setBackground(new Color(78, 194, 207));
		setVisible(true);
		MyCanvas myCanvas = new MyCanvas();
		add(myCanvas);
	}
	public static void main(String[] args) {
		new Test();
	}
	private class MyCanvas extends Canvas{
		@Override
		public void paint(Graphics g) {
			// TODO Auto-generated method stub
			Graphics2D g2d = (Graphics2D)g;
			//雲
			g2d.setColor(Color.WHITE);
			g2d.fillOval(550, 2, 100, 50);
			g2d.fillOval(520, -3, 80, 50);
			g2d.fillOval(580, -5, 80, 50);
			//煙囪
			g2d.setColor(Color.BLACK);
			g2d.fillRect(250, 20, 30, 80);
			g2d.fillRect(650, 20, 30, 80);
			//房頂
			int x[] = {100,200,300};
			int y[] = {100,10,100};
			g2d.setColor(new Color(124,86,39));
			g2d.fillPolygon(x, y, 3);
			int x1[] = {500,600,700};
			int y1[] = {100,10,100};
			g2d.fillPolygon(x1, y1, 3);
			//屋子
			g2d.setColor(new Color(109,134,128));
			g2d.fillRect(120,100,160,100);
			g2d.fillRect(520,100,160,100);
			//窗戶和門
			g2d.setColor(Color.WHITE);
			g2d.fillRect(130,120,50,50);
			g2d.fillRect(220,120,50,80);
			g2d.fillRect(530,120,50,50);
			g2d.fillRect(620,120,50,80);
			//太陽
			g2d.setColor(Color.YELLOW);
			g2d.fillOval(740, -20, 50, 50);
			
			//路
			g2d.setColor(new Color(124,86,39));
			g2d.fillRect(0, 200, 800, 300);
			
			//石子
			g2d.setColor(Color.BLACK);
			g2d.fillOval(0, 205, 10, 20);
			Random r = new Random();
			int moveX = 0;
			int moveY = 0;
			for(int i = 0;i<=100;i++) {
				moveX = r.nextInt(800);
				moveY = r.nextInt(300)+200;
				g2d.fillOval(moveX, moveY, r.nextInt(10)+5,r.nextInt(15)+5);
			}
		}
	}
}

相關文章