1. 抽象形狀類
公司專注于為企業(yè)提供成都做網(wǎng)站、成都網(wǎng)站建設(shè)、微信公眾號(hào)開發(fā)、商城系統(tǒng)網(wǎng)站開發(fā),小程序開發(fā),軟件按需定制設(shè)計(jì)等一站式互聯(lián)網(wǎng)企業(yè)服務(wù)。憑借多年豐富的經(jīng)驗(yàn),我們會(huì)仔細(xì)了解各客戶的需求而做出多方面的分析、設(shè)計(jì)、整合,為客戶設(shè)計(jì)出具風(fēng)格及創(chuàng)意性的商業(yè)解決方案,創(chuàng)新互聯(lián)更提供一系列網(wǎng)站制作和網(wǎng)站推廣的服務(wù)。
package com;
//抽象的形狀類
public abstract class Shape{
}
2. 顯示接口
package com;
//接口
public interface IDisplay{
void display(); //顯示圖形的基本信息
double getArea(); //計(jì)算面積
double getGirth(); //計(jì)算周長(zhǎng)
}
3. 三角形類
package com.tri;
import com.*;
//三角形類
public class Triangle extends Shape implements IDisplay{
protected double a;
protected double b;
protected double c;
public Triangle(double a, double b, double c){
this.a = a;
this.b = b;
this.c = c;
}
@Override
public double getArea() {
double s = (a + b + c) / 2;
return Math.sqrt(s*(s-a)*(s-b)*(s-c));
}
@Override
public double getGirth() {
return this.a + this.b + this.c;
}
@Override
public void display() {
System.out.println("三角形");
System.out.println("邊長(zhǎng):" + a + ", " + b + ", " + c);
}
}
4. 矩形類
package com.rec;
import com.*;
//矩形類
public class Rectangle extends Shape implements IDisplay {
protected double width;
protected double height;
public Rectangle(double width, double height){
this.width = width;
this.height = height;
}
@Override
public double getArea() {
return this.width * this.height;
}
@Override
public double getGirth() {
return 2 * ( this.width + this.height);
}
@Override
public void display() {
System.out.println("矩形");
System.out.println("寬:" + this.width + ", 高:" + this.height);
}
}
5. 圓類
package com.cir;
import com.*;
//圓類
public class Circle extends Shape implements IDisplay {
protected double radius;
public Circle(double radius){
this.radius = radius;
}
@Override
public double getArea() {
return Math.PI * this.radius * this.radius;
}
@Override
public double getGirth() {
return 2 * Math.PI * this.radius;
}
@Override
public void display() {
System.out.println("圓");
System.out.println("半徑:" + this.radius);
}
}
6. 正多邊形類
package com.mul;
import com.*;
//正多邊形類
public class MulSide extends Shape implements IDisplay {
protected double side; //邊長(zhǎng)
protected int n; //邊數(shù)
public MulSide(double side, int n){
this.side = side;
this.n = n;
}
@Override
public double getArea() {
return n * side * side * Math.tan((n-2) * Math.PI / (2 * n)) / 4;
}
@Override
public double getGirth() {
return this.side * this.n;
}
@Override
public void display() {
System.out.println("正多邊形");
System.out.println("連長(zhǎng):" + this.side + ", 邊數(shù):" + this.n);
}
}
7. 主類(測(cè)試類)
package com;
import java.util.Scanner;
import com.cir.Circle;
import com.mul.MulSide;
import com.rec.Rectangle;
import com.tri.Triangle;
public class Test22 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double a, b, c;
double width, height;
double radius;
double side;
int n;
IDisplay s;
System.out.println("請(qǐng)輸入三角形的基本信息");
System.out.print("邊長(zhǎng)1:");
a = scan.nextDouble();
System.out.print("邊長(zhǎng)2:");
b = scan.nextDouble();
System.out.print("邊長(zhǎng)3:");
c = scan.nextDouble();
s = new Triangle(a, b, c);
s.display();
System.out.println("三角形的面積:" + s.getArea());
System.out.println("三角形的周長(zhǎng):" + s.getGirth());
System.out.println("請(qǐng)輸入矩形的基本信息");
System.out.print("寬:");
width = scan.nextDouble();
System.out.print("高:");
height = scan.nextDouble();
s = new Rectangle(width, height);
s.display();
System.out.println("矩形的面積:" + s.getArea());
System.out.println("矩的周長(zhǎng):" + s.getGirth());
System.out.println("請(qǐng)輸入圓的基本信息");
System.out.print("半徑:");
radius = scan.nextDouble();
s = new Circle(radius);
s.display();
System.out.println("圓的面積:" + s.getArea());
System.out.println("圓的周長(zhǎng):" + s.getGirth());
System.out.println("請(qǐng)輸入正多邊形的基本信息");
System.out.print("邊長(zhǎng):");
side = scan.nextDouble();
System.out.print("邊數(shù):");
n = scan.nextInt();
s = new MulSide(side, n);
s.display();
System.out.println("正多邊形的面積:" + s.getArea());
System.out.println("正多邊形的周長(zhǎng):" + s.getGirth());
}
}
運(yùn)行測(cè)試:
請(qǐng)輸入三角形的基本信息
邊長(zhǎng)1:3
邊長(zhǎng)2:4
邊長(zhǎng)3:5
三角形
邊長(zhǎng):3.0, 4.0, 5.0
三角形的面積:6.0
三角形的周長(zhǎng):12.0
請(qǐng)輸入矩形的基本信息
寬:3
高:4
矩形
寬:3.0, 高:4.0
矩形的面積:12.0
矩的周長(zhǎng):14.0
請(qǐng)輸入圓的基本信息
半徑:2
圓
半徑:2.0
圓的面積:12.566370614359172
圓的周長(zhǎng):12.566370614359172
請(qǐng)輸入正多邊形的基本信息
邊長(zhǎng):2
邊數(shù):6
正多邊形
連長(zhǎng):2.0, 邊數(shù):6
正多邊形的面積:10.39230484541326
正多邊形的周長(zhǎng):12.0
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
//不規(guī)則圖形的繪制
public class IrregularShapeDemo extends JFrame {
GeneralPath gPath= new GeneralPath(); //GeneralPath對(duì)象實(shí)例
Point aPoint;
//構(gòu)造函數(shù)
public IrregularShapeDemo() {
super("不規(guī)則圖形的繪制"); //調(diào)用父類構(gòu)造函數(shù)
enableEvents(AWTEvent.MOUSE_EVENT_MASK|AWTEvent.MOUSE_MOTION_EVENT_MASK); //允許事件
setSize(300, 200); //設(shè)置窗口尺寸
setVisible(true); //設(shè)置窗口可視
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //關(guān)閉窗口時(shí)退出程序
}
public void paint(Graphics g) { //重載窗口組件的paint()方法
Graphics2D g2D = (Graphics2D)g; //獲取圖形環(huán)境
g2D.draw(gPath); //繪制路徑
}
public static void main(String[] args) {
new IrregularShapeDemo();
}
protected void processMouseEvent(MouseEvent e) { //鼠標(biāo)事件處理
if(e.getID() == MouseEvent.MOUSE_PRESSED) {
aPoint = e.getPoint(); //得到當(dāng)前鼠標(biāo)點(diǎn)
gPath = new GeneralPath(); //重新實(shí)例化GeneralPath對(duì)象
gPath.moveTo(aPoint.x,aPoint.y); //設(shè)置路徑點(diǎn)
}
}
protected void processMouseMotionEvent(MouseEvent e) { //鼠標(biāo)運(yùn)動(dòng)事件處理
if(e.getID() == MouseEvent.MOUSE_DRAGGED) {
aPoint = e.getPoint(); //得到當(dāng)前鼠標(biāo)點(diǎn)
gPath.lineTo(aPoint.x, aPoint.y); //設(shè)置路徑
gPath.moveTo(aPoint.x, aPoint.y);
repaint(); //重繪組件
}
}
}
好像是作業(yè), 源代碼發(fā)給你參考, ?注意學(xué)習(xí)下代碼. ?了解下布局 和按鈕的工作原理
參考代碼
import?java.awt.*;
import?java.awt.event.*;
public?class?MyRandomFrame?extends?Frame?implements?ActionListener?{
private?final?TextField?tf1;
private?final?TextField?tf2;
private?final?TextField?tf3;
public?static?void?main(String[]?args)?{
new?MyRandomFrame().setVisible(true);
}
public?MyRandomFrame()?{
setSize(600,?100);
setLocationRelativeTo(null);
setTitle("學(xué)號(hào):XXXXX??姓名:張三");
Button?but1?=?new?Button("獲取1~100之間的隨機(jī)數(shù)");
but1.setActionCommand("隨即");
but1.addActionListener(this);
Label?label1?=?new?Label("第一個(gè)數(shù):");
tf1?=?new?TextField(5);
Label?label2?=?new?Label("第二個(gè)數(shù):");
tf2?=?new?TextField(5);
Button?but2?=?new?Button("求和");
but2.setActionCommand("求和");
but2.addActionListener(this);
tf3?=?new?TextField(5);
setLayout(new?FlowLayout());
add(but1);
add(label1);
add(tf1);
add(label2);
add(tf2);
add(but2);
add(tf3);
addWindowListener(new?WindowAdapter()?{//?關(guān)閉窗口
@Override
public?void?windowClosing(WindowEvent?e)?{
System.exit(0);
}
});
}
@Override
public?void?actionPerformed(ActionEvent?e)?{
String?command?=?e.getActionCommand();
if?(command.equals("隨即"))?{
int?a?=?(int)?(Math.random()?*?100);
int?b?=?(int)?(Math.random()?*?100);
tf1.setText(String.valueOf(a));
tf2.setText(String.valueOf(b));
}?else?if?(command.equals("求和"))?{
int?x?=?Integer.parseInt(tf1.getText());
int?y?=?Integer.parseInt(tf2.getText());
tf3.setText(String.valueOf((x?+?y)));
}
}
}
public class Rectangle{ private int width; private int height; public Rectangle(){ this.width = 10; this.height = 10; } public Rectangle(int width, int height){ this.width = width; this.height = height; } public int area(){ return width * height; } //省略getter/setter }
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class vv extends JDialog {
private static final long serialVersionUID = 1L;
private JLabel l_Id = new JLabel("登陸賬戶", JLabel.CENTER);
private JLabel l_pw = new JLabel("登陸密碼", JLabel.CENTER);
private JTextField t_Id = new JTextField(10);
private JPasswordField t_pw = new JPasswordField(10);
private JButton btnLogin;
private JButton btnClose;
public vv() {
super();
setResizable(false);
getContentPane().setBackground(new Color(225, 225, 225));
getContentPane().setLayout(null);
initialize();
}
protected void initialize() {
setTitle("系統(tǒng)登錄");
l_Id.setBounds(48, 43, 53, 25);
t_Id.setBounds(110, 43, 150, 25);
l_pw.setBounds(48, 93, 53, 25);
t_pw.setBounds(110, 93, 150, 25);
getContentPane().add(l_Id);
getContentPane().add(l_pw);
getContentPane().add(t_Id);
getContentPane().add(t_pw);
btnLogin = new JButton();
btnLogin.setText("登 錄");
btnLogin.setBounds(70, 142, 85, 28);
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addBtnLoginActionListener();
}
});
getContentPane().add(btnLogin);
btnClose = new JButton();
btnClose.setText("關(guān) 閉");
btnClose.setBounds(175, 142, 85, 28);
btnClose.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
System.exit(-1);
}
});
getContentPane().add(btnClose);
}
private void addBtnLoginActionListener() {
String user = t_Id.getText();
String password = new String(t_pw.getPassword());
if (user.equals("")) {
JOptionPane.showMessageDialog(this, "帳號(hào)不可為空", "Caution",
JOptionPane.WARNING_MESSAGE);
return;
}
if (password.equals("")) {
JOptionPane.showMessageDialog(this, "密碼不可為空", "Caution",
JOptionPane.WARNING_MESSAGE);
return;
}
String sql = "select * FROM login WHERE id = '" + user + "' and pw = '"
+ password + "'";
boolean success = false;
// TODO:數(shù)據(jù)校驗(yàn) success = executeQuery(sql);
if (success) {
// TODO: 如果數(shù)據(jù)校驗(yàn)成功 顯示主界面 并關(guān)閉登錄界面
JOptionPane.showMessageDialog(this, "成功登錄", "提示",
JOptionPane.INFORMATION_MESSAGE);
this.dispose();
} else {
JOptionPane.showMessageDialog(this, "帳號(hào)或密碼錯(cuò)誤!", "警告",
JOptionPane.WARNING_MESSAGE);
t_pw.requestFocus(); // 密碼框選中
}
}
public Dimension getPreferredSize() {
return new Dimension(320, 170);
}
public void show() {
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension screen = tk.getScreenSize();
Dimension d = getSize();
this.setLocation((screen.width - d.width) / 2,
(screen.height - d.height) / 2);
// 輸入密碼后回車相當(dāng)于點(diǎn)擊了登錄按鈕
getRootPane().setDefaultButton(btnLogin);
t_pw.requestFocus();
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setSize(300, 220);
super.show();
}
public static void main(String[] args) {
vv loginFrame = new vv();
loginFrame.setVisible(true);
}
}
希望對(duì)你有幫助
新聞標(biāo)題:java程序設(shè)計(jì)圖形代碼 java程序設(shè)計(jì)圖形代碼怎么用
文章網(wǎng)址:http://aaarwkj.com/article30/doooiso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、品牌網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站、域名注冊(cè)、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)