Java画图(直线,矩形,椭圆),并显示其周长面积
生活随笔
收集整理的這篇文章主要介紹了
Java画图(直线,矩形,椭圆),并显示其周长面积
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Shape類 父類
package com.lovoinfo.shape;import java.awt.Color; import java.awt.Graphics; /*** 圖形(抽象類,不能實例化)* @author Administrator**/ public abstract class Shape {protected int startX,startY;//起點橫縱坐標protected int endX,endY;//終點橫縱坐標protected Color color;public void setColor(Color color) {this.color = color;}/*** 繪圖(抽象方法留給子類重寫)* @param g 畫筆*/public abstract void draw(Graphics g);public abstract void calculate(Graphics g);public int getStartX() {return startX;}public void setStartX(int startX) {this.startX = startX;}public int getStartY() {return startY;}public void setStartY(int startY) {this.startY = startY;}public int getEndX() {return endX;}public void setEndX(int endX) {this.endX = endX;}public int getEndY() {return endY;}public void setEndY(int endY) {this.endY = endY;}}定義子類
Rectangle
Oval
Line
工具類
產生直線橢圓矩形坐標隨機數
UI
package com.lovoinfo.ui;import java.awt.Color; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;import javax.swing.JButton; import javax.swing.JFrame;import com.lovoinfo.shape.Line; import com.lovoinfo.shape.Oval; import com.lovoinfo.shape.Rectangle; import com.lovoinfo.shape.Shape; import com.lovoinfo.util.MyUtil;@SuppressWarnings("serial") public class MyFrame extends JFrame {private class ButtonHandler implements ActionListener{@Overridepublic void actionPerformed(ActionEvent e) {String command=e.getActionCommand();if(command.equals("Line")){shape=new Line();}else if(command.equals("Oval")){shape=new Oval();}else{shape=new Rectangle();}shape.setStartX(MyUtil.random(0, 1000));shape.setStartY(MyUtil.random(0, 600));shape.setEndX(MyUtil.random(0, 1000));shape.setEndY(MyUtil.random(0, 600));// shape.setColor(MyUtil.randomColor());shape.setColor(Color.black);repaint();}}private JButton lineButton;private JButton ovalButton;private JButton recButton;// private Line line = null; // private Rectangle rec = null; // private Oval oval = null;private Shape shape=null;public MyFrame() {this.setSize(1000, 600);this.setTitle("繪圖窗口");// super.setTitle("我的第一個窗口");this.setResizable(false);this.setLocationRelativeTo(null);// 窗口居中this.setDefaultCloseOperation(EXIT_ON_CLOSE);// 設置默認的關閉操作lineButton = new JButton("Line");ovalButton = new JButton("Oval");recButton = new JButton("Rectangle"); // recButton.addActionListener(new ActionListener() { // // @Override // public void actionPerformed(ActionEvent e) { // rec = new Rectangle(); // rec.setStartX(MyUtil.random(0, 1000)); // rec.setStartY(MyUtil.random(0, 600)); // rec.setEndX(MyUtil.random(0, 1000)); // rec.setEndY(MyUtil.random(0, 600)); // repaint(); // // } // });// ovalButton.addActionListener(new ActionListener() { // // @Override // public void actionPerformed(ActionEvent e) { // oval = new Oval(); // oval.setStartX(MyUtil.random(0, 1000)); // oval.setStartY(MyUtil.random(0, 600)); // oval.setEndX(MyUtil.random(0, 1000)); // oval.setEndY(MyUtil.random(0, 600)); // repaint(); // // } // }); // lineButton.addActionListener(new ActionListener() { // /** // * 點擊按鈕后要執行的方法 // */ // @Override // public void actionPerformed(ActionEvent e) { // // line = new Line(); // line.setStartX(MyUtil.random(0, 1000)); // line.setStartY(MyUtil.random(0, 600)); // line.setEndX(MyUtil.random(0, 1000)); // line.setEndY(MyUtil.random(0, 600)); // // repaint(); // } // });// 需要放一個Action監聽器ActionListener handler=new ButtonHandler();lineButton.addActionListener(handler);ovalButton.addActionListener(handler);recButton.addActionListener(handler);this.setLayout(new FlowLayout());// 設置流式布局管理器this.add(lineButton);this.add(ovalButton);this.add(recButton);}@Overridepublic void paint(Graphics g) {super.paint(g);if(shape!=null){shape.draw(g);shape.calculate(g);} // if (line != null) { // line.draw(g); // } // if (rec != null) { // rec.draw(g); // } // if (oval != null) { // oval.draw(g); // }} }測試類
package com.lovoinfo.ui;import java.awt.Color; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;import javax.swing.JButton; import javax.swing.JFrame;import com.lovoinfo.shape.Line; import com.lovoinfo.shape.Oval; import com.lovoinfo.shape.Rectangle; import com.lovoinfo.shape.Shape; import com.lovoinfo.util.MyUtil;@SuppressWarnings("serial") public class MyFrame extends JFrame {private class ButtonHandler implements ActionListener{@Overridepublic void actionPerformed(ActionEvent e) {String command=e.getActionCommand();if(command.equals("Line")){shape=new Line();}else if(command.equals("Oval")){shape=new Oval();}else{shape=new Rectangle();}shape.setStartX(MyUtil.random(0, 1000));shape.setStartY(MyUtil.random(0, 600));shape.setEndX(MyUtil.random(0, 1000));shape.setEndY(MyUtil.random(0, 600));// shape.setColor(MyUtil.randomColor());shape.setColor(Color.black);repaint();}}private JButton lineButton;private JButton ovalButton;private JButton recButton;// private Line line = null; // private Rectangle rec = null; // private Oval oval = null;private Shape shape=null;public MyFrame() {this.setSize(1000, 600);this.setTitle("繪圖窗口");// super.setTitle("我的第一個窗口");this.setResizable(false);this.setLocationRelativeTo(null);// 窗口居中this.setDefaultCloseOperation(EXIT_ON_CLOSE);// 設置默認的關閉操作lineButton = new JButton("Line");ovalButton = new JButton("Oval");recButton = new JButton("Rectangle"); // recButton.addActionListener(new ActionListener() { // // @Override // public void actionPerformed(ActionEvent e) { // rec = new Rectangle(); // rec.setStartX(MyUtil.random(0, 1000)); // rec.setStartY(MyUtil.random(0, 600)); // rec.setEndX(MyUtil.random(0, 1000)); // rec.setEndY(MyUtil.random(0, 600)); // repaint(); // // } // });// ovalButton.addActionListener(new ActionListener() { // // @Override // public void actionPerformed(ActionEvent e) { // oval = new Oval(); // oval.setStartX(MyUtil.random(0, 1000)); // oval.setStartY(MyUtil.random(0, 600)); // oval.setEndX(MyUtil.random(0, 1000)); // oval.setEndY(MyUtil.random(0, 600)); // repaint(); // // } // }); // lineButton.addActionListener(new ActionListener() { // /** // * 點擊按鈕后要執行的方法 // */ // @Override // public void actionPerformed(ActionEvent e) { // // line = new Line(); // line.setStartX(MyUtil.random(0, 1000)); // line.setStartY(MyUtil.random(0, 600)); // line.setEndX(MyUtil.random(0, 1000)); // line.setEndY(MyUtil.random(0, 600)); // // repaint(); // } // });// 需要放一個Action監聽器ActionListener handler=new ButtonHandler();lineButton.addActionListener(handler);ovalButton.addActionListener(handler);recButton.addActionListener(handler);this.setLayout(new FlowLayout());// 設置流式布局管理器this.add(lineButton);this.add(ovalButton);this.add(recButton);}@Overridepublic void paint(Graphics g) {super.paint(g);if(shape!=null){shape.draw(g);shape.calculate(g);} // if (line != null) { // line.draw(g); // } // if (rec != null) { // rec.draw(g); // } // if (oval != null) { // oval.draw(g); // }} }總結
以上是生活随笔為你收集整理的Java画图(直线,矩形,椭圆),并显示其周长面积的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 添加到收藏夹js(兼容ie/ff/op)
- 下一篇: java美元兑换,(Java实现) 美元