⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dline.java

📁 java写的画图程序
💻 JAVA
字号:
package line;
import javax.swing.*;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;

public class Dline extends JFrame{
	
	private MCanvas ca;
	
	public Dline() 
	{
		super();
		getContentPane().setLayout(new BorderLayout());
		
		this.setSize(300, 300);
		
		
		

		



		final JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());
		getContentPane().add(panel, BorderLayout.CENTER);
		
		ca= new MCanvas(0);
		panel.add(ca,BorderLayout.CENTER);
		ca.setBackground(Color.white);		
		ca.repaint();

		
		final JPanel panel_1 = new JPanel();
		panel.add(panel_1, BorderLayout.SOUTH);

		final JButton DDA = new JButton();
		DDA.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) 
			{
				panel.remove(ca);
				ca= new MCanvas(0);
				panel.add(ca,BorderLayout.CENTER);
				ca.setBackground(Color.white);		
				ca.repaint();
				panel.revalidate();
				panel.repaint();
				
			}
		});
		DDA.setText("DDA");
		panel_1.add(DDA);

		final JButton Bresenham = new JButton();
		Bresenham.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) 
			{
				panel.remove(ca);
				ca= new MCanvas(1);
				panel.add(ca,BorderLayout.CENTER);
				ca.setBackground(Color.white);		
				ca.repaint();
				panel.revalidate();
				panel.repaint();
				
			}
		});
		Bresenham.setText("Bresenham");
		panel_1.add(Bresenham);

		final JButton circle = new JButton();
		circle.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) 
			{
				panel.remove(ca);
				ca= new MCanvas(2);
				panel.add(ca,BorderLayout.CENTER);
				ca.setBackground(Color.white);		
				ca.repaint();
				panel.revalidate();
				panel.repaint();
			}
		});
		circle.setText("circle");
		panel_1.add(circle);



		
		this.setVisible(true);
		this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);		
		
	}
	
	public class MCanvas extends Canvas
	{
		private int type;
		MCanvas()
		{
			this(0);
		}
		
		MCanvas (int t )
		{
			type=t;

		}
		
		public void paint(Graphics g){
			if(type==0){
				DDALine(10,10,30,60,g);
			}
			else
				{if(type==1)
				Bensonham(10,10,30,60,g);
				else
					printPoint( g, 90);
					}
			
			
		}
	}
	public void DDALine(int x0,int y0,int x1,int y1,Graphics g)
	{
		float dx,dy,length,x,y;
		if(Math.abs(x1-x0)>=Math.abs(y1-y0))
			length=Math.abs(x1-x0);
		else
			length=Math.abs(y1-y0);
		dx=(x1-x0)/length;
		dy=(y1-y0)/length;
		int i=1;
		x=x0;
		y=y0;
		while(i<=length)
		{
			g.drawRect((int)(x+0.5), (int)(y+0.5), 0, 0);
			x=x+dx;
			y=y+dy;
			i++;
			
		}
		
	}
	public void Bensonham(int x0,int y0,int x1,int y1,Graphics g)
	{
		/*int dx,dy,e,i,x,y;
		dx=x1-x0;
		dy=y1-y0;
		e=2*dy-dx;
		x=x0;
		y=y0;
		for(i=0;i<=dx;i++)
		{
			g.drawRect(x, y, 0, 0);
			x++;
			if(e>=0)
			{
				y++;
				e=e+2*dy-2*dx;
				
			}
			else
				e=e+2*dy;
		}*/
		int i ;
		int dx;
		int dy;
		int e;
		int x = 0;
		int y = 0;
		dy = Math.abs(x0 - x1);
		dx = Math.abs(y0 - y1);
		e = 2 * dy -dx;
		x = x0;
		y = y0;
		for( i = 0; i <= dx; i++  ) {
			g.fillRect(x, y, 1, 1);
			y ++;
			if(e >= 0) {
				x ++;
				e = e + 2 * dy - 2 * dx;
			} else {
				e = e + 2 * dy;
			}
		}
		
	}
	public void printPoint(Graphics g, int r) {
		int x;
		int y;
		int e;
		x = 0;
		y = r;
		e = 1 - r;
		g.fillRect(x, y, 1, 1);
		//System.out.print("[" + x + ", " + y + "]  ");
		while(x <= y) {
			if(e < 0) {
				e += 2 * x +3; 
			} else {
				e += 2 * (x - y) + 5;
				y --;
			}
			x ++;
			g.drawRect(x, y, 1, 1);
			//System.out.print("[" + x + ", " + y + "]  ");
		}
		
	}
	public static void main(String arg[])
	{
		Dline d=new Dline();
	}
	

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -