用JAVA写一个简单图形类
1.编写一个图形类MyGraphic。1)它有两个基本属性:图形线条的颜色StringlineColor和图形的填充颜色StringfillColor。2)设计矩形类CR...
1.编写一个图形类MyGraphic。1)它有两个基本属性:图形线条的颜色String lineColor和图形的填充颜色String fillColor。2)设计矩形类CRectangle,有属性double rLong和宽double rWidth,使用方法 float calCircum()可以返回矩形的周长,使用方法float calSquare()可以返回矩形的面积。编写方法show(),显示图形的线条颜色和填充颜色,输出面积和方法。3)设计圆形类CCircle,定义属性:半径double radius,可以通过同名方法计算周长和面积。编写方法show(),显示图形的线条颜色和填充颜色,输出面积和方法。4)编写出应用程序对CRectangle类和CCircle类进行验证。 完成上述要求即可
展开
2个回答
推荐于2018-04-30
展开全部
package com.qzd.swing.security;import javax.swing.*;import com.qzd.swing.io.IOUtils;
import com.qzd.swing.security.util.CipherUtils;import java.awt.Rectangle;
import java.awt.Font;import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import java.io.File;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;public class MyDecrypt extends JFrame { private static final long serialVersionUID = 1144364976994226418L; public MyDecrypt() {
super("DES 解密");
init(); } private JLabel pathLabel;
private JTextField filePathField;
private JButton fileChooserButton;
private JTextArea originalArea;
private JLabel originalTextLabel;
private JLabel decryptLabel;
private JTextArea decryptArea;
private JButton saveButton;
private JButton decryptButton;
private JScrollPane scrollPaneOne;
private JScrollPane scrollPaneTwo; private JFileChooser fc; public static void main(String[] args) {
new MyDecrypt();
} private void init() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(null);
setSize(580, 600);
creatComponent();
setMyFont();
addComponents();
setMyBounds();
addListener();
//System.out.println(Charset.defaultCharset());
this.setVisible(true);
} public void addListener() {
MyDecryptActionListener my = new MyDecryptActionListener(this);
fileChooserButton.addActionListener(my);
decryptButton.addActionListener(my);
saveButton.addActionListener(my);
} public void actionPerformed(ActionEvent e) {
if (e.getSource() == fileChooserButton) {
int returnVal = fc.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
filePathField.setText(file.getPath());
originalArea.setText(IOUtils.inputFileISO(file)); }
} else if (e.getSource() == decryptButton) { String password = JOptionPane.showInputDialog(this, "请输入口令进行解密:");
byte bytes[] = null;
try {
bytes = CipherUtils.decrypt(password,
"12345678".getBytes(), originalArea.getText().getBytes("ISO8859-1"));
decryptArea.setText(new String(bytes,"GBK"));
JOptionPane.showMessageDialog(this, "恭喜你解密成功!!");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
} else if (e.getSource() == saveButton) {
int returnVal = fc.showSaveDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
IOUtils.outputFile(decryptArea.getText(), file.getPath());
JOptionPane.showMessageDialog(MyDecrypt.this, "恭喜你!!"
+ file.getPath() + "成功保存", "提示信息",
JOptionPane.WARNING_MESSAGE);
}
}
} private void addComponents() {
add(pathLabel);
add(filePathField);
add(fileChooserButton);
add(originalTextLabel);
add(decryptLabel);
add(saveButton);
add(decryptButton);
add(scrollPaneOne);
add(scrollPaneTwo); } private void setMyBounds() {
pathLabel.setBounds(new Rectangle(8, 14, 70, 31));
saveButton.setBounds(new Rectangle(235, 495, 94, 29));
decryptLabel.setBounds(new Rectangle(235, 282, 108, 29));
originalTextLabel.setBounds(new Rectangle(246, 49, 93, 35));
scrollPaneTwo.setBounds(new Rectangle(12, 321, 554, 164));
scrollPaneOne.setBounds(new Rectangle(11, 92, 555, 176));
fileChooserButton.setBounds(new Rectangle(442, 14, 83, 29));
filePathField.setBounds(new Rectangle(97, 14, 321, 30));
} public void setMyFont() {
pathLabel.setFont(new java.awt.Font("宋体", Font.PLAIN, 15));
decryptButton.setBounds(new Rectangle(460, 281, 104, 31));
decryptLabel.setFont(new java.awt.Font("宋体", Font.PLAIN, 25));
originalTextLabel.setFont(new java.awt.Font("宋体", Font.PLAIN, 25));
} public void creatComponent() {
pathLabel = new JLabel("文件路径");
filePathField = new JTextField();
fileChooserButton = new JButton("文件", createImageIcon("Open16.gif"));
originalArea = new JTextArea();
originalTextLabel = new JLabel("原文");
decryptLabel = new JLabel("解密后");
decryptArea = new JTextArea();
saveButton = new JButton("保存", createImageIcon("Save16.gif"));
decryptButton = new JButton("点击解密");
scrollPaneOne = new JScrollPane(originalArea);
scrollPaneTwo = new JScrollPane(decryptArea);
fc = new JFileChooser();
} protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = MyEncrypt.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}}class MyDecryptActionListener implements ActionListener {
private MyDecrypt myDecrypt; public MyDecryptActionListener(MyDecrypt myDecrypt) {
this.myDecrypt = myDecrypt;
} public void actionPerformed(ActionEvent e) {
myDecrypt.actionPerformed(e); }} //自己看下吧 我做的一个简单的加密程序
import com.qzd.swing.security.util.CipherUtils;import java.awt.Rectangle;
import java.awt.Font;import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import java.io.File;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;public class MyDecrypt extends JFrame { private static final long serialVersionUID = 1144364976994226418L; public MyDecrypt() {
super("DES 解密");
init(); } private JLabel pathLabel;
private JTextField filePathField;
private JButton fileChooserButton;
private JTextArea originalArea;
private JLabel originalTextLabel;
private JLabel decryptLabel;
private JTextArea decryptArea;
private JButton saveButton;
private JButton decryptButton;
private JScrollPane scrollPaneOne;
private JScrollPane scrollPaneTwo; private JFileChooser fc; public static void main(String[] args) {
new MyDecrypt();
} private void init() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(null);
setSize(580, 600);
creatComponent();
setMyFont();
addComponents();
setMyBounds();
addListener();
//System.out.println(Charset.defaultCharset());
this.setVisible(true);
} public void addListener() {
MyDecryptActionListener my = new MyDecryptActionListener(this);
fileChooserButton.addActionListener(my);
decryptButton.addActionListener(my);
saveButton.addActionListener(my);
} public void actionPerformed(ActionEvent e) {
if (e.getSource() == fileChooserButton) {
int returnVal = fc.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
filePathField.setText(file.getPath());
originalArea.setText(IOUtils.inputFileISO(file)); }
} else if (e.getSource() == decryptButton) { String password = JOptionPane.showInputDialog(this, "请输入口令进行解密:");
byte bytes[] = null;
try {
bytes = CipherUtils.decrypt(password,
"12345678".getBytes(), originalArea.getText().getBytes("ISO8859-1"));
decryptArea.setText(new String(bytes,"GBK"));
JOptionPane.showMessageDialog(this, "恭喜你解密成功!!");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
} else if (e.getSource() == saveButton) {
int returnVal = fc.showSaveDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
IOUtils.outputFile(decryptArea.getText(), file.getPath());
JOptionPane.showMessageDialog(MyDecrypt.this, "恭喜你!!"
+ file.getPath() + "成功保存", "提示信息",
JOptionPane.WARNING_MESSAGE);
}
}
} private void addComponents() {
add(pathLabel);
add(filePathField);
add(fileChooserButton);
add(originalTextLabel);
add(decryptLabel);
add(saveButton);
add(decryptButton);
add(scrollPaneOne);
add(scrollPaneTwo); } private void setMyBounds() {
pathLabel.setBounds(new Rectangle(8, 14, 70, 31));
saveButton.setBounds(new Rectangle(235, 495, 94, 29));
decryptLabel.setBounds(new Rectangle(235, 282, 108, 29));
originalTextLabel.setBounds(new Rectangle(246, 49, 93, 35));
scrollPaneTwo.setBounds(new Rectangle(12, 321, 554, 164));
scrollPaneOne.setBounds(new Rectangle(11, 92, 555, 176));
fileChooserButton.setBounds(new Rectangle(442, 14, 83, 29));
filePathField.setBounds(new Rectangle(97, 14, 321, 30));
} public void setMyFont() {
pathLabel.setFont(new java.awt.Font("宋体", Font.PLAIN, 15));
decryptButton.setBounds(new Rectangle(460, 281, 104, 31));
decryptLabel.setFont(new java.awt.Font("宋体", Font.PLAIN, 25));
originalTextLabel.setFont(new java.awt.Font("宋体", Font.PLAIN, 25));
} public void creatComponent() {
pathLabel = new JLabel("文件路径");
filePathField = new JTextField();
fileChooserButton = new JButton("文件", createImageIcon("Open16.gif"));
originalArea = new JTextArea();
originalTextLabel = new JLabel("原文");
decryptLabel = new JLabel("解密后");
decryptArea = new JTextArea();
saveButton = new JButton("保存", createImageIcon("Save16.gif"));
decryptButton = new JButton("点击解密");
scrollPaneOne = new JScrollPane(originalArea);
scrollPaneTwo = new JScrollPane(decryptArea);
fc = new JFileChooser();
} protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = MyEncrypt.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}}class MyDecryptActionListener implements ActionListener {
private MyDecrypt myDecrypt; public MyDecryptActionListener(MyDecrypt myDecrypt) {
this.myDecrypt = myDecrypt;
} public void actionPerformed(ActionEvent e) {
myDecrypt.actionPerformed(e); }} //自己看下吧 我做的一个简单的加密程序
2014-05-18
展开全部
package quation_graphic;public class CRectangle { double rLong ;
double rWidth;
public CRectangle(double l,double w)
{
rLong = l; // 长
rWidth = w; //宽
}
public float calCircum() //返回 矩形周长
{
return (float) (rLong+rWidth)*2;
}
public float calSquare()//可以返回矩形的面积
{
return (float)(rLong*rWidth);
}
public void show(String l,String f)
{
System.out.println("线条色:"+l);
System.out.println("填充色:"+f);
System.out.println("周长"+calCircum());
System.out.println("面积"+calSquare());
}}
package quation_graphic;public class CCircle { /**
* @param args
*/
double radius;
CCircle(double r)
{
radius = r;
}
public float calCircum()
{
return (float)(2*3.14*radius);
}
public float calSquare()
{
return (float)(3.14*radius);
}
public void show(String l,String f)
{
System.out.println("线条色:"+l);
System.out.println("填充色:"+f);
System.out.println("圆周长:"+calCircum());
System.out.println("圆面积:"+calSquare());
}
} package quation_graphic;public class MyGraphic { /**
* @param args
*/
static String lineColor="#fff";
static String fillColor ="#000";
public static void main(String[] args) {
// TODO Auto-generated method stub
new CRectangle(5, 5).show(lineColor, fillColor);
new CCircle(5).show(lineColor, fillColor);
}}
//希望对你有用 同学
double rWidth;
public CRectangle(double l,double w)
{
rLong = l; // 长
rWidth = w; //宽
}
public float calCircum() //返回 矩形周长
{
return (float) (rLong+rWidth)*2;
}
public float calSquare()//可以返回矩形的面积
{
return (float)(rLong*rWidth);
}
public void show(String l,String f)
{
System.out.println("线条色:"+l);
System.out.println("填充色:"+f);
System.out.println("周长"+calCircum());
System.out.println("面积"+calSquare());
}}
package quation_graphic;public class CCircle { /**
* @param args
*/
double radius;
CCircle(double r)
{
radius = r;
}
public float calCircum()
{
return (float)(2*3.14*radius);
}
public float calSquare()
{
return (float)(3.14*radius);
}
public void show(String l,String f)
{
System.out.println("线条色:"+l);
System.out.println("填充色:"+f);
System.out.println("圆周长:"+calCircum());
System.out.println("圆面积:"+calSquare());
}
} package quation_graphic;public class MyGraphic { /**
* @param args
*/
static String lineColor="#fff";
static String fillColor ="#000";
public static void main(String[] args) {
// TODO Auto-generated method stub
new CRectangle(5, 5).show(lineColor, fillColor);
new CCircle(5).show(lineColor, fillColor);
}}
//希望对你有用 同学
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询