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

📄 midbresenhampanelcircle.java

📁 利用Java采用中点Brasenham算法同时绘制多个圆。
💻 JAVA
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. *//** * * @author Administrator */package bresenham;import java.awt.Color;import java.awt.Graphics;import java.awt.Point;import java.awt.event.ComponentEvent;import java.awt.event.ComponentListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.util.Vector;import javax.swing.JLabel;import javax.swing.JPanel;/** * * @author Lenovo */public class midBresenhamPanelCircle extends JPanel implements MouseListener, ComponentListener,Runnable {    JLabel PointLabel;    Vector<Point> pointset;    Vector<Point> pointtwo;    Point circlePoint;    Thread myThread;    int temp=0;    public midBresenhamPanelCircle() {        this.setEnabled(true);        PointLabel = new JLabel();        this.add(PointLabel, "North");        pointset = new Vector<Point>();        pointtwo = new Vector<Point>();        circlePoint = new Point();        this.addMouseListener(this);        myThread=new Thread(this);        myThread.start();    }    public void mousePressed(MouseEvent e) {        if (pointset.size() > 0) {            pointset.removeAllElements();        }        Point p = new Point(e.getX(), e.getY());        pointtwo.add(p);        if (pointtwo.size() >= 2) {            this.midBresenhamcircle();        // repaint();        }    }    public void midBresenhamcircle() {        Point firstpoint, lastpoint;        int x, y, dx, dy, d;        int r;        firstpoint = pointtwo.get(0);        circlePoint = firstpoint;        lastpoint = pointtwo.get(1);        dx = lastpoint.x - firstpoint.x;        dy = lastpoint.y - firstpoint.y;        r = (int) Math.sqrt((double) (dx * dx + dy * dy));        pointtwo.removeAllElements();                PointLabel.setText("圆心为:(" + firstpoint.x + "," + firstpoint.y + ")  半径为:R=" + r);        d = 1 - r;        x = 0;         //firstpoint.x        y = r;          //firstpoint.y+r        while (x <= y) {            Point newpoint = new Point(x, y);            pointset.add(newpoint);            repaint();            if (d < 0) {                d = d + 2 * x + 3;            } else {                d = d + 2 * (x - y) + 5;                y--;            }            x++;        }    }    public void run() {        while (true) {            if (myThread.isAlive() && !myThread.isInterrupted()) {                try {                    repaint();                    Thread.sleep(200);                    temp++;                    if(temp==pointset.size()){                       // myThread.interrupt();                        temp=0;                    }                } catch (InterruptedException ex) {                }            } else {                break;            }        }    }    @Override    protected void paintComponent(Graphics g) {        Point current;        int size;        g.clearRect(0, 0, getWidth(), this.PointLabel.getHeight() + 2);        if (pointset.size() != 0) {            size =temp+1;            for (int i = 0; i < size; i++) {                current = pointset.get(i);                g.setColor(Color.RED);                g.drawLine(current.x + circlePoint.x, current.y + circlePoint.y, current.x + circlePoint.x, current.y + circlePoint.y);                g.drawLine(current.y + circlePoint.x, current.x + circlePoint.y, current.y + circlePoint.x, current.x + circlePoint.y);                g.drawLine(-current.y + circlePoint.x, current.x + circlePoint.y, -current.y + circlePoint.x, current.x + circlePoint.y);                g.drawLine(-current.x + circlePoint.x, current.y + circlePoint.y, -current.x + circlePoint.x, current.y + circlePoint.y);                g.drawLine(-current.x + circlePoint.x, -current.y + circlePoint.y, -current.x + circlePoint.x, -current.y + circlePoint.y);                g.drawLine(-current.y + circlePoint.x, -current.x + circlePoint.y, -current.y + circlePoint.x, -current.x + circlePoint.y);                g.drawLine(current.y + circlePoint.x, -current.x + circlePoint.y, current.y + circlePoint.x, -current.x + circlePoint.y);                g.drawLine(current.x + circlePoint.x, -current.y + circlePoint.y, current.x + circlePoint.x, -current.y + circlePoint.y);            }        }    }    public void mouseClicked(MouseEvent e) {    }    public void mouseReleased(MouseEvent e) {    }    public void mouseEntered(MouseEvent e) {    }    public void mouseExited(MouseEvent e) {    }    public void componentResized(ComponentEvent e) {        repaint();    }    public void componentMoved(ComponentEvent e) {    }    public void componentShown(ComponentEvent e) {    }    public void componentHidden(ComponentEvent e) {    }}

⌨️ 快捷键说明

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