用Java编写的应用程序,输入函数表达式生成相应的函数图像, 5
展开全部
java语言输入函数表达式生成相应的函数图像代码如下:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class UI extends JFrame
{
MyPanel mp;
JPanel pl = new JPanel();
JPanel pl1 = new JPanel(),
pl2 = new JPanel(),
pl3 = new JPanel(),
pl4 = new JPanel();
JRadioButton rb1,rb2;
ButtonGroup bg = new ButtonGroup();
JTextField tf = new JTextField(16);
String[] s = {"y = sin(x)", "y = cos(x)", "y = tan(x)",
"y = pow(x, 2)", "y = pow(x, 3)", "y = log(x)",
"y = pow(2, x)", "y = sqrt(x)", "r = a(sita)"};
JComboBox cb;
JButton bn1 = new JButton("变宽"),
bn2 = new JButton("变窄"),
bn3 = new JButton("拉长"),
bn4 = new JButton("压短"),
bn = new JButton("绘图"),
exit = new JButton("退出"),
bn5 = new JButton("左移"),
bn6 = new JButton("右移"),
bn7 = new JButton("上移"),
bn8 = new JButton("下移");
public UI()
{
mp = new MyPanel(this);
pl1.setLayout(new GridLayout(1, 2));
pl2.setLayout(new GridLayout(1, 2));
pl3.setLayout(new GridLayout(1, 2));
pl4.setLayout(new GridLayout(1, 2));
pl1.add(bn1); bn1.setEnabled(false);
pl1.add(bn2); bn2.setEnabled(false);
pl2.add(bn3); bn3.setEnabled(false);
pl2.add(bn4); bn4.setEnabled(false);
pl3.add(bn5); bn5.setEnabled(false);
pl3.add(bn6); bn6.setEnabled(false);
pl4.add(bn7); bn7.setEnabled(false);
pl4.add(bn8); bn8.setEnabled(false);
pl.setLayout(new GridLayout(20, 1));
rb1 = new JRadioButton("输入函数");
rb2 = new JRadioButton("选择已有函数");
rb2.setSelected(true);
tf.setEnabled(false);
bg.add(rb1); bg.add(rb2);
rb1.addActionListener(mp);
rb2.addActionListener(mp);
pl.add(rb1);
pl.add(tf);
pl.add(rb2);
cb = new JComboBox(s);
pl.add(cb);
pl.add(new JLabel());
pl.add(pl1); pl.add(pl2);
pl.add(pl3); pl.add(pl4);
pl.add(bn);
pl.add(exit);
bn1.addActionListener(mp);
bn2.addActionListener(mp);
bn3.addActionListener(mp);
bn4.addActionListener(mp);
bn5.addActionListener(mp);
bn6.addActionListener(mp);
bn7.addActionListener(mp);
bn8.addActionListener(mp);
bn.addActionListener(mp);
exit.addActionListener(mp);
this.setLayout(new BorderLayout());
this.add(mp, BorderLayout.CENTER);
this.add(pl, BorderLayout.EAST);
this.setTitle("平面直角坐标系画图小工具");
this.setSize(797, 600 + 37);
Dimension dn = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((dn.width - 797) / 2, (dn.height - 637) / 2);
this.setVisible(true);
this.setDefaultCloseOperation(3);
}
public static void main(String[] args)
{
new UI();
}
}
package math;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class MyPanel extends JPanel implements ActionListener,MouseMotionListener
{
UI ui;
int flag;
double h_times;
int w_times;
int dx;
int dy;
String str;
Point pt = new Point(0, 0);
void init()
{
flag = -1;
h_times = Math.PI / 100;
w_times = 100;
dx = 300;
dy = 300;
}
public MyPanel(UI ui)
{
this.addMouseMotionListener(this);
init();
this.ui = ui;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
drawCoordinate(g2);
Line2D line;
g2.setColor(Color.BLUE);
g2.drawString("(" + (pt.x - 300) + ", " + (300 - pt.y) + ")", pt.x + 20, pt.y + 20);
switch(flag)
{
case 0:
g2.drawString("y = Asin(Bx + C) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.sin(getReal_X(i)) * w_times, i + 1, dy - Math.sin(getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
case 1:
g2.drawString("y = Acos(Bx + C) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.cos(getReal_X(i)) * w_times, i + 1, dy - Math.cos(getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
case 2:
g2.drawString("y = Atan(Bx + C) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.tan(getReal_X(i)) * w_times, i + 1, dy - Math.tan(getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
case 3:
g2.drawString("y = Apow(Bx + C, 2) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.pow(getReal_X(i), 2) * w_times, i + 1, dy - Math.pow(getReal_X(i + 1), 2) * w_times);
g2.draw(line);
}
break;
case 4:
g2.drawString("y = Apow(Bx + C, 3) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.pow(getReal_X(i), 3) * w_times, i + 1, dy - Math.pow(getReal_X(i + 1), 3) * w_times);
g2.draw(line);
}
break;
case 5:
g2.drawString("y = Alog(Bx + C) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.log(getReal_X(i)) * w_times, i + 1, dy - Math.log(getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
case 6:
g2.drawString("y = Apow(2, Bx + C) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.pow(2, getReal_X(i)) * w_times, i + 1, dy - Math.pow(2, getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
case 7:
g2.drawString("y = Asqrt(Bx + C) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.sqrt(getReal_X(i)) * w_times, i + 1, dy - Math.sqrt(getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
case 8:
g2.drawString("y = a(sita)", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(getReal_X(i) * Math.cos(getReal_X(i)), dy - getReal_X(i) * Math.sin(getReal_X(i)) * w_times, getReal_X(i) * Math.cos(getReal_X(i + 1)), dy - getReal_X(i) * Math.sin(getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
}
if(flag != -1)
{
g2.drawString("A = " + w_times, 105, 90);
g2.drawString("B= " + h_times, 105, 120);
g2.drawString("C= " + (300 - dx), 105, 150);
g2.drawString("D= " + (300 - dy), 105, 180);
}
}
private double getReal_X(double x)
{
return (x - dx) * h_times;
}
private void drawCoordinate(Graphics2D g2)
{
int len = 20;
Line2D line;
for(int i = 0; i <= 600 / len; i++)
{
g2.setColor(Color.PINK.darker());
if(i == 300 / len)
g2.setColor(Color.RED);
else;
line = new Line2D.Double(0, i * len, 600, i * len);
g2.draw(line);
line = new Line2D.Double(i * len, 0, i * len, 600);
g2.draw(line);
}
drawPoint(g2, 300, 300);
}
private void drawPoint(Graphics2D g2, double x, double y)
{
g2.setColor(Color.YELLOW);
Ellipse2D circle = new Ellipse2D.Double(x - 2, y - 2, 4, 4);
g2.fill(circle);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == ui.rb1)
{
ui.tf.setEnabled(true);
ui.cb.setEnabled(false);
flag = -1;
}
if(e.getSource() == ui.rb2)
{
ui.tf.setEnabled(false);
ui.cb.setEnabled(true);
}
if(e.getSource() == ui.bn1)
{
h_times /= 1.1;
}
if(e.getSource() == ui.bn2)
{
h_times *= 1.1;
}
if(e.getSource() == ui.bn3)
{
// ui.bn4.setEnabled(true);
w_times += 10;
// if(w_times >= 300)
// ui.bn3.setEnabled(false);
}
if(e.getSource() == ui.bn4)
{
// ui.bn3.setEnabled(true);
w_times -= 10;
// if(w_times <= 0)
// ui.bn4.setEnabled(false);
}
if(e.getSource() == ui.bn5)
{
dx -= 10;
}
if(e.getSource() == ui.bn6)
{
dx += 10;
}
if(e.getSource() == ui.bn7)
{
// ui.bn8.setEnabled(true);
dy -= 10;
// if(dy <= 0)
// ui.bn7.setEnabled(false);
}
if(e.getSource() == ui.bn8)
{
// ui.bn7.setEnabled(true);
dy += 10;
// if(dy >= 600)
// ui.bn8.setEnabled(false);
}
if(e.getSource() == ui.bn)
{
if(ui.tf.isEnabled() == true)
{
str = ui.tf.getText();
if(str == null || str.length() == 0)
{
ui.bn1.setEnabled(false);
ui.bn2.setEnabled(false);
ui.bn3.setEnabled(false);
ui.bn4.setEnabled(false);
ui.bn5.setEnabled(false);
ui.bn6.setEnabled(false);
ui.bn7.setEnabled(false);
ui.bn8.setEnabled(false);
JOptionPane.showMessageDialog(this, "请输入函数表达式 !");
return;
}
}else flag = -2;
ui.bn1.setEnabled(true);
ui.bn2.setEnabled(true);
ui.bn3.setEnabled(true);
ui.bn4.setEnabled(true);
ui.bn5.setEnabled(true);
ui.bn6.setEnabled(true);
ui.bn7.setEnabled(true);
ui.bn8.setEnabled(true);
init();
if(ui.cb.isEnabled() == true)
{
flag = ui.cb.getSelectedIndex();
}
}
if(e.getSource() == ui.exit)
System.exit(0);
repaint();
}
public void mouseDragged(MouseEvent arg0)
{
}
public void mouseMoved(MouseEvent e)
{
pt = e.getPoint();
repaint();
}
}
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class UI extends JFrame
{
MyPanel mp;
JPanel pl = new JPanel();
JPanel pl1 = new JPanel(),
pl2 = new JPanel(),
pl3 = new JPanel(),
pl4 = new JPanel();
JRadioButton rb1,rb2;
ButtonGroup bg = new ButtonGroup();
JTextField tf = new JTextField(16);
String[] s = {"y = sin(x)", "y = cos(x)", "y = tan(x)",
"y = pow(x, 2)", "y = pow(x, 3)", "y = log(x)",
"y = pow(2, x)", "y = sqrt(x)", "r = a(sita)"};
JComboBox cb;
JButton bn1 = new JButton("变宽"),
bn2 = new JButton("变窄"),
bn3 = new JButton("拉长"),
bn4 = new JButton("压短"),
bn = new JButton("绘图"),
exit = new JButton("退出"),
bn5 = new JButton("左移"),
bn6 = new JButton("右移"),
bn7 = new JButton("上移"),
bn8 = new JButton("下移");
public UI()
{
mp = new MyPanel(this);
pl1.setLayout(new GridLayout(1, 2));
pl2.setLayout(new GridLayout(1, 2));
pl3.setLayout(new GridLayout(1, 2));
pl4.setLayout(new GridLayout(1, 2));
pl1.add(bn1); bn1.setEnabled(false);
pl1.add(bn2); bn2.setEnabled(false);
pl2.add(bn3); bn3.setEnabled(false);
pl2.add(bn4); bn4.setEnabled(false);
pl3.add(bn5); bn5.setEnabled(false);
pl3.add(bn6); bn6.setEnabled(false);
pl4.add(bn7); bn7.setEnabled(false);
pl4.add(bn8); bn8.setEnabled(false);
pl.setLayout(new GridLayout(20, 1));
rb1 = new JRadioButton("输入函数");
rb2 = new JRadioButton("选择已有函数");
rb2.setSelected(true);
tf.setEnabled(false);
bg.add(rb1); bg.add(rb2);
rb1.addActionListener(mp);
rb2.addActionListener(mp);
pl.add(rb1);
pl.add(tf);
pl.add(rb2);
cb = new JComboBox(s);
pl.add(cb);
pl.add(new JLabel());
pl.add(pl1); pl.add(pl2);
pl.add(pl3); pl.add(pl4);
pl.add(bn);
pl.add(exit);
bn1.addActionListener(mp);
bn2.addActionListener(mp);
bn3.addActionListener(mp);
bn4.addActionListener(mp);
bn5.addActionListener(mp);
bn6.addActionListener(mp);
bn7.addActionListener(mp);
bn8.addActionListener(mp);
bn.addActionListener(mp);
exit.addActionListener(mp);
this.setLayout(new BorderLayout());
this.add(mp, BorderLayout.CENTER);
this.add(pl, BorderLayout.EAST);
this.setTitle("平面直角坐标系画图小工具");
this.setSize(797, 600 + 37);
Dimension dn = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((dn.width - 797) / 2, (dn.height - 637) / 2);
this.setVisible(true);
this.setDefaultCloseOperation(3);
}
public static void main(String[] args)
{
new UI();
}
}
package math;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class MyPanel extends JPanel implements ActionListener,MouseMotionListener
{
UI ui;
int flag;
double h_times;
int w_times;
int dx;
int dy;
String str;
Point pt = new Point(0, 0);
void init()
{
flag = -1;
h_times = Math.PI / 100;
w_times = 100;
dx = 300;
dy = 300;
}
public MyPanel(UI ui)
{
this.addMouseMotionListener(this);
init();
this.ui = ui;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
drawCoordinate(g2);
Line2D line;
g2.setColor(Color.BLUE);
g2.drawString("(" + (pt.x - 300) + ", " + (300 - pt.y) + ")", pt.x + 20, pt.y + 20);
switch(flag)
{
case 0:
g2.drawString("y = Asin(Bx + C) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.sin(getReal_X(i)) * w_times, i + 1, dy - Math.sin(getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
case 1:
g2.drawString("y = Acos(Bx + C) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.cos(getReal_X(i)) * w_times, i + 1, dy - Math.cos(getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
case 2:
g2.drawString("y = Atan(Bx + C) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.tan(getReal_X(i)) * w_times, i + 1, dy - Math.tan(getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
case 3:
g2.drawString("y = Apow(Bx + C, 2) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.pow(getReal_X(i), 2) * w_times, i + 1, dy - Math.pow(getReal_X(i + 1), 2) * w_times);
g2.draw(line);
}
break;
case 4:
g2.drawString("y = Apow(Bx + C, 3) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.pow(getReal_X(i), 3) * w_times, i + 1, dy - Math.pow(getReal_X(i + 1), 3) * w_times);
g2.draw(line);
}
break;
case 5:
g2.drawString("y = Alog(Bx + C) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.log(getReal_X(i)) * w_times, i + 1, dy - Math.log(getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
case 6:
g2.drawString("y = Apow(2, Bx + C) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.pow(2, getReal_X(i)) * w_times, i + 1, dy - Math.pow(2, getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
case 7:
g2.drawString("y = Asqrt(Bx + C) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.sqrt(getReal_X(i)) * w_times, i + 1, dy - Math.sqrt(getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
case 8:
g2.drawString("y = a(sita)", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(getReal_X(i) * Math.cos(getReal_X(i)), dy - getReal_X(i) * Math.sin(getReal_X(i)) * w_times, getReal_X(i) * Math.cos(getReal_X(i + 1)), dy - getReal_X(i) * Math.sin(getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
}
if(flag != -1)
{
g2.drawString("A = " + w_times, 105, 90);
g2.drawString("B= " + h_times, 105, 120);
g2.drawString("C= " + (300 - dx), 105, 150);
g2.drawString("D= " + (300 - dy), 105, 180);
}
}
private double getReal_X(double x)
{
return (x - dx) * h_times;
}
private void drawCoordinate(Graphics2D g2)
{
int len = 20;
Line2D line;
for(int i = 0; i <= 600 / len; i++)
{
g2.setColor(Color.PINK.darker());
if(i == 300 / len)
g2.setColor(Color.RED);
else;
line = new Line2D.Double(0, i * len, 600, i * len);
g2.draw(line);
line = new Line2D.Double(i * len, 0, i * len, 600);
g2.draw(line);
}
drawPoint(g2, 300, 300);
}
private void drawPoint(Graphics2D g2, double x, double y)
{
g2.setColor(Color.YELLOW);
Ellipse2D circle = new Ellipse2D.Double(x - 2, y - 2, 4, 4);
g2.fill(circle);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == ui.rb1)
{
ui.tf.setEnabled(true);
ui.cb.setEnabled(false);
flag = -1;
}
if(e.getSource() == ui.rb2)
{
ui.tf.setEnabled(false);
ui.cb.setEnabled(true);
}
if(e.getSource() == ui.bn1)
{
h_times /= 1.1;
}
if(e.getSource() == ui.bn2)
{
h_times *= 1.1;
}
if(e.getSource() == ui.bn3)
{
// ui.bn4.setEnabled(true);
w_times += 10;
// if(w_times >= 300)
// ui.bn3.setEnabled(false);
}
if(e.getSource() == ui.bn4)
{
// ui.bn3.setEnabled(true);
w_times -= 10;
// if(w_times <= 0)
// ui.bn4.setEnabled(false);
}
if(e.getSource() == ui.bn5)
{
dx -= 10;
}
if(e.getSource() == ui.bn6)
{
dx += 10;
}
if(e.getSource() == ui.bn7)
{
// ui.bn8.setEnabled(true);
dy -= 10;
// if(dy <= 0)
// ui.bn7.setEnabled(false);
}
if(e.getSource() == ui.bn8)
{
// ui.bn7.setEnabled(true);
dy += 10;
// if(dy >= 600)
// ui.bn8.setEnabled(false);
}
if(e.getSource() == ui.bn)
{
if(ui.tf.isEnabled() == true)
{
str = ui.tf.getText();
if(str == null || str.length() == 0)
{
ui.bn1.setEnabled(false);
ui.bn2.setEnabled(false);
ui.bn3.setEnabled(false);
ui.bn4.setEnabled(false);
ui.bn5.setEnabled(false);
ui.bn6.setEnabled(false);
ui.bn7.setEnabled(false);
ui.bn8.setEnabled(false);
JOptionPane.showMessageDialog(this, "请输入函数表达式 !");
return;
}
}else flag = -2;
ui.bn1.setEnabled(true);
ui.bn2.setEnabled(true);
ui.bn3.setEnabled(true);
ui.bn4.setEnabled(true);
ui.bn5.setEnabled(true);
ui.bn6.setEnabled(true);
ui.bn7.setEnabled(true);
ui.bn8.setEnabled(true);
init();
if(ui.cb.isEnabled() == true)
{
flag = ui.cb.getSelectedIndex();
}
}
if(e.getSource() == ui.exit)
System.exit(0);
repaint();
}
public void mouseDragged(MouseEvent arg0)
{
}
public void mouseMoved(MouseEvent e)
{
pt = e.getPoint();
repaint();
}
}
展开全部
package math;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class UI extends JFrame
{
MyPanel mp;
JPanel pl = new JPanel();
JPanel pl1 = new JPanel(),
pl2 = new JPanel(),
pl3 = new JPanel(),
pl4 = new JPanel();
JRadioButton rb1,rb2;
ButtonGroup bg = new ButtonGroup();
JTextField tf = new JTextField(16);
String[] s = {"y = sin(x)", "y = cos(x)", "y = tan(x)",
"y = pow(x, 2)", "y = pow(x, 3)", "y = log(x)",
"y = pow(2, x)", "y = sqrt(x)", "r = a(sita)"};
JComboBox cb;
JButton bn1 = new JButton("变宽"),
bn2 = new JButton("变窄"),
bn3 = new JButton("拉长"),
bn4 = new JButton("压短"),
bn = new JButton("绘图"),
exit = new JButton("退出"),
bn5 = new JButton("左移"),
bn6 = new JButton("右移"),
bn7 = new JButton("上移"),
bn8 = new JButton("下移");
public UI()
{
mp = new MyPanel(this);
pl1.setLayout(new GridLayout(1, 2));
pl2.setLayout(new GridLayout(1, 2));
pl3.setLayout(new GridLayout(1, 2));
pl4.setLayout(new GridLayout(1, 2));
pl1.add(bn1); bn1.setEnabled(false);
pl1.add(bn2); bn2.setEnabled(false);
pl2.add(bn3); bn3.setEnabled(false);
pl2.add(bn4); bn4.setEnabled(false);
pl3.add(bn5); bn5.setEnabled(false);
pl3.add(bn6); bn6.setEnabled(false);
pl4.add(bn7); bn7.setEnabled(false);
pl4.add(bn8); bn8.setEnabled(false);
pl.setLayout(new GridLayout(20, 1));
rb1 = new JRadioButton("输入函数");
rb2 = new JRadioButton("选择已有函数");
rb2.setSelected(true);
tf.setEnabled(false);
bg.add(rb1); bg.add(rb2);
rb1.addActionListener(mp);
rb2.addActionListener(mp);
pl.add(rb1);
pl.add(tf);
pl.add(rb2);
cb = new JComboBox(s);
pl.add(cb);
pl.add(new JLabel());
pl.add(pl1); pl.add(pl2);
pl.add(pl3); pl.add(pl4);
pl.add(bn);
pl.add(exit);
bn1.addActionListener(mp);
bn2.addActionListener(mp);
bn3.addActionListener(mp);
bn4.addActionListener(mp);
bn5.addActionListener(mp);
bn6.addActionListener(mp);
bn7.addActionListener(mp);
bn8.addActionListener(mp);
bn.addActionListener(mp);
exit.addActionListener(mp);
this.setLayout(new BorderLayout());
this.add(mp, BorderLayout.CENTER);
this.add(pl, BorderLayout.EAST);
this.setTitle("平面直角坐标系画图小工具");
this.setSize(797, 600 + 37);
Dimension dn = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((dn.width - 797) / 2, (dn.height - 637) / 2);
this.setVisible(true);
this.setDefaultCloseOperation(3);
}
public static void main(String[] args)
{
new UI();
}
}
package math;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class MyPanel extends JPanel implements ActionListener,MouseMotionListener
{
UI ui;
int flag;
double h_times;
int w_times;
int dx;
int dy;
String str;
Point pt = new Point(0, 0);
void init()
{
flag = -1;
h_times = Math.PI / 100;
w_times = 100;
dx = 300;
dy = 300;
}
public MyPanel(UI ui)
{
this.addMouseMotionListener(this);
init();
this.ui = ui;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
drawCoordinate(g2);
Line2D line;
g2.setColor(Color.BLUE);
g2.drawString("(" + (pt.x - 300) + ", " + (300 - pt.y) + ")", pt.x + 20, pt.y + 20);
switch(flag)
{
case 0:
g2.drawString("y = Asin(Bx + C) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.sin(getReal_X(i)) * w_times, i + 1, dy - Math.sin(getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
case 1:
g2.drawString("y = Acos(Bx + C) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.cos(getReal_X(i)) * w_times, i + 1, dy - Math.cos(getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
case 2:
g2.drawString("y = Atan(Bx + C) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.tan(getReal_X(i)) * w_times, i + 1, dy - Math.tan(getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
case 3:
g2.drawString("y = Apow(Bx + C, 2) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.pow(getReal_X(i), 2) * w_times, i + 1, dy - Math.pow(getReal_X(i + 1), 2) * w_times);
g2.draw(line);
}
break;
case 4:
g2.drawString("y = Apow(Bx + C, 3) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.pow(getReal_X(i), 3) * w_times, i + 1, dy - Math.pow(getReal_X(i + 1), 3) * w_times);
g2.draw(line);
}
break;
case 5:
g2.drawString("y = Alog(Bx + C) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.log(getReal_X(i)) * w_times, i + 1, dy - Math.log(getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
case 6:
g2.drawString("y = Apow(2, Bx + C) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.pow(2, getReal_X(i)) * w_times, i + 1, dy - Math.pow(2, getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
case 7:
g2.drawString("y = Asqrt(Bx + C) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.sqrt(getReal_X(i)) * w_times, i + 1, dy - Math.sqrt(getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
case 8:
g2.drawString("y = a(sita)", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(getReal_X(i) * Math.cos(getReal_X(i)), dy - getReal_X(i) * Math.sin(getReal_X(i)) * w_times, getReal_X(i) * Math.cos(getReal_X(i + 1)), dy - getReal_X(i) * Math.sin(getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
}
if(flag != -1)
{
g2.drawString("A = " + w_times, 105, 90);
g2.drawString("B= " + h_times, 105, 120);
g2.drawString("C= " + (300 - dx), 105, 150);
g2.drawString("D= " + (300 - dy), 105, 180);
}
}
private double getReal_X(double x)
{
return (x - dx) * h_times;
}
private void drawCoordinate(Graphics2D g2)
{
int len = 20;
Line2D line;
for(int i = 0; i <= 600 / len; i++)
{
g2.setColor(Color.PINK.darker());
if(i == 300 / len)
g2.setColor(Color.RED);
else;
line = new Line2D.Double(0, i * len, 600, i * len);
g2.draw(line);
line = new Line2D.Double(i * len, 0, i * len, 600);
g2.draw(line);
}
drawPoint(g2, 300, 300);
}
private void drawPoint(Graphics2D g2, double x, double y)
{
g2.setColor(Color.YELLOW);
Ellipse2D circle = new Ellipse2D.Double(x - 2, y - 2, 4, 4);
g2.fill(circle);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == ui.rb1)
{
ui.tf.setEnabled(true);
ui.cb.setEnabled(false);
flag = -1;
}
if(e.getSource() == ui.rb2)
{
ui.tf.setEnabled(false);
ui.cb.setEnabled(true);
}
if(e.getSource() == ui.bn1)
{
h_times /= 1.1;
}
if(e.getSource() == ui.bn2)
{
h_times *= 1.1;
}
if(e.getSource() == ui.bn3)
{
// ui.bn4.setEnabled(true);
w_times += 10;
// if(w_times >= 300)
// ui.bn3.setEnabled(false);
}
if(e.getSource() == ui.bn4)
{
// ui.bn3.setEnabled(true);
w_times -= 10;
// if(w_times <= 0)
// ui.bn4.setEnabled(false);
}
if(e.getSource() == ui.bn5)
{
dx -= 10;
}
if(e.getSource() == ui.bn6)
{
dx += 10;
}
if(e.getSource() == ui.bn7)
{
// ui.bn8.setEnabled(true);
dy -= 10;
// if(dy <= 0)
// ui.bn7.setEnabled(false);
}
if(e.getSource() == ui.bn8)
{
// ui.bn7.setEnabled(true);
dy += 10;
// if(dy >= 600)
// ui.bn8.setEnabled(false);
}
if(e.getSource() == ui.bn)
{
if(ui.tf.isEnabled() == true)
{
str = ui.tf.getText();
if(str == null || str.length() == 0)
{
ui.bn1.setEnabled(false);
ui.bn2.setEnabled(false);
ui.bn3.setEnabled(false);
ui.bn4.setEnabled(false);
ui.bn5.setEnabled(false);
ui.bn6.setEnabled(false);
ui.bn7.setEnabled(false);
ui.bn8.setEnabled(false);
JOptionPane.showMessageDialog(this, "请输入函数表达式 !");
return;
}
}else flag = -2;
ui.bn1.setEnabled(true);
ui.bn2.setEnabled(true);
ui.bn3.setEnabled(true);
ui.bn4.setEnabled(true);
ui.bn5.setEnabled(true);
ui.bn6.setEnabled(true);
ui.bn7.setEnabled(true);
ui.bn8.setEnabled(true);
init();
if(ui.cb.isEnabled() == true)
{
flag = ui.cb.getSelectedIndex();
}
}
if(e.getSource() == ui.exit)
System.exit(0);
repaint();
}
public void mouseDragged(MouseEvent arg0)
{
}
public void mouseMoved(MouseEvent e)
{
pt = e.getPoint();
repaint();
}
}
刚学java时写着玩的
希望能够帮助到你,望采纳!
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class UI extends JFrame
{
MyPanel mp;
JPanel pl = new JPanel();
JPanel pl1 = new JPanel(),
pl2 = new JPanel(),
pl3 = new JPanel(),
pl4 = new JPanel();
JRadioButton rb1,rb2;
ButtonGroup bg = new ButtonGroup();
JTextField tf = new JTextField(16);
String[] s = {"y = sin(x)", "y = cos(x)", "y = tan(x)",
"y = pow(x, 2)", "y = pow(x, 3)", "y = log(x)",
"y = pow(2, x)", "y = sqrt(x)", "r = a(sita)"};
JComboBox cb;
JButton bn1 = new JButton("变宽"),
bn2 = new JButton("变窄"),
bn3 = new JButton("拉长"),
bn4 = new JButton("压短"),
bn = new JButton("绘图"),
exit = new JButton("退出"),
bn5 = new JButton("左移"),
bn6 = new JButton("右移"),
bn7 = new JButton("上移"),
bn8 = new JButton("下移");
public UI()
{
mp = new MyPanel(this);
pl1.setLayout(new GridLayout(1, 2));
pl2.setLayout(new GridLayout(1, 2));
pl3.setLayout(new GridLayout(1, 2));
pl4.setLayout(new GridLayout(1, 2));
pl1.add(bn1); bn1.setEnabled(false);
pl1.add(bn2); bn2.setEnabled(false);
pl2.add(bn3); bn3.setEnabled(false);
pl2.add(bn4); bn4.setEnabled(false);
pl3.add(bn5); bn5.setEnabled(false);
pl3.add(bn6); bn6.setEnabled(false);
pl4.add(bn7); bn7.setEnabled(false);
pl4.add(bn8); bn8.setEnabled(false);
pl.setLayout(new GridLayout(20, 1));
rb1 = new JRadioButton("输入函数");
rb2 = new JRadioButton("选择已有函数");
rb2.setSelected(true);
tf.setEnabled(false);
bg.add(rb1); bg.add(rb2);
rb1.addActionListener(mp);
rb2.addActionListener(mp);
pl.add(rb1);
pl.add(tf);
pl.add(rb2);
cb = new JComboBox(s);
pl.add(cb);
pl.add(new JLabel());
pl.add(pl1); pl.add(pl2);
pl.add(pl3); pl.add(pl4);
pl.add(bn);
pl.add(exit);
bn1.addActionListener(mp);
bn2.addActionListener(mp);
bn3.addActionListener(mp);
bn4.addActionListener(mp);
bn5.addActionListener(mp);
bn6.addActionListener(mp);
bn7.addActionListener(mp);
bn8.addActionListener(mp);
bn.addActionListener(mp);
exit.addActionListener(mp);
this.setLayout(new BorderLayout());
this.add(mp, BorderLayout.CENTER);
this.add(pl, BorderLayout.EAST);
this.setTitle("平面直角坐标系画图小工具");
this.setSize(797, 600 + 37);
Dimension dn = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((dn.width - 797) / 2, (dn.height - 637) / 2);
this.setVisible(true);
this.setDefaultCloseOperation(3);
}
public static void main(String[] args)
{
new UI();
}
}
package math;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class MyPanel extends JPanel implements ActionListener,MouseMotionListener
{
UI ui;
int flag;
double h_times;
int w_times;
int dx;
int dy;
String str;
Point pt = new Point(0, 0);
void init()
{
flag = -1;
h_times = Math.PI / 100;
w_times = 100;
dx = 300;
dy = 300;
}
public MyPanel(UI ui)
{
this.addMouseMotionListener(this);
init();
this.ui = ui;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
drawCoordinate(g2);
Line2D line;
g2.setColor(Color.BLUE);
g2.drawString("(" + (pt.x - 300) + ", " + (300 - pt.y) + ")", pt.x + 20, pt.y + 20);
switch(flag)
{
case 0:
g2.drawString("y = Asin(Bx + C) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.sin(getReal_X(i)) * w_times, i + 1, dy - Math.sin(getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
case 1:
g2.drawString("y = Acos(Bx + C) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.cos(getReal_X(i)) * w_times, i + 1, dy - Math.cos(getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
case 2:
g2.drawString("y = Atan(Bx + C) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.tan(getReal_X(i)) * w_times, i + 1, dy - Math.tan(getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
case 3:
g2.drawString("y = Apow(Bx + C, 2) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.pow(getReal_X(i), 2) * w_times, i + 1, dy - Math.pow(getReal_X(i + 1), 2) * w_times);
g2.draw(line);
}
break;
case 4:
g2.drawString("y = Apow(Bx + C, 3) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.pow(getReal_X(i), 3) * w_times, i + 1, dy - Math.pow(getReal_X(i + 1), 3) * w_times);
g2.draw(line);
}
break;
case 5:
g2.drawString("y = Alog(Bx + C) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.log(getReal_X(i)) * w_times, i + 1, dy - Math.log(getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
case 6:
g2.drawString("y = Apow(2, Bx + C) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.pow(2, getReal_X(i)) * w_times, i + 1, dy - Math.pow(2, getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
case 7:
g2.drawString("y = Asqrt(Bx + C) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.sqrt(getReal_X(i)) * w_times, i + 1, dy - Math.sqrt(getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
case 8:
g2.drawString("y = a(sita)", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(getReal_X(i) * Math.cos(getReal_X(i)), dy - getReal_X(i) * Math.sin(getReal_X(i)) * w_times, getReal_X(i) * Math.cos(getReal_X(i + 1)), dy - getReal_X(i) * Math.sin(getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
}
if(flag != -1)
{
g2.drawString("A = " + w_times, 105, 90);
g2.drawString("B= " + h_times, 105, 120);
g2.drawString("C= " + (300 - dx), 105, 150);
g2.drawString("D= " + (300 - dy), 105, 180);
}
}
private double getReal_X(double x)
{
return (x - dx) * h_times;
}
private void drawCoordinate(Graphics2D g2)
{
int len = 20;
Line2D line;
for(int i = 0; i <= 600 / len; i++)
{
g2.setColor(Color.PINK.darker());
if(i == 300 / len)
g2.setColor(Color.RED);
else;
line = new Line2D.Double(0, i * len, 600, i * len);
g2.draw(line);
line = new Line2D.Double(i * len, 0, i * len, 600);
g2.draw(line);
}
drawPoint(g2, 300, 300);
}
private void drawPoint(Graphics2D g2, double x, double y)
{
g2.setColor(Color.YELLOW);
Ellipse2D circle = new Ellipse2D.Double(x - 2, y - 2, 4, 4);
g2.fill(circle);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == ui.rb1)
{
ui.tf.setEnabled(true);
ui.cb.setEnabled(false);
flag = -1;
}
if(e.getSource() == ui.rb2)
{
ui.tf.setEnabled(false);
ui.cb.setEnabled(true);
}
if(e.getSource() == ui.bn1)
{
h_times /= 1.1;
}
if(e.getSource() == ui.bn2)
{
h_times *= 1.1;
}
if(e.getSource() == ui.bn3)
{
// ui.bn4.setEnabled(true);
w_times += 10;
// if(w_times >= 300)
// ui.bn3.setEnabled(false);
}
if(e.getSource() == ui.bn4)
{
// ui.bn3.setEnabled(true);
w_times -= 10;
// if(w_times <= 0)
// ui.bn4.setEnabled(false);
}
if(e.getSource() == ui.bn5)
{
dx -= 10;
}
if(e.getSource() == ui.bn6)
{
dx += 10;
}
if(e.getSource() == ui.bn7)
{
// ui.bn8.setEnabled(true);
dy -= 10;
// if(dy <= 0)
// ui.bn7.setEnabled(false);
}
if(e.getSource() == ui.bn8)
{
// ui.bn7.setEnabled(true);
dy += 10;
// if(dy >= 600)
// ui.bn8.setEnabled(false);
}
if(e.getSource() == ui.bn)
{
if(ui.tf.isEnabled() == true)
{
str = ui.tf.getText();
if(str == null || str.length() == 0)
{
ui.bn1.setEnabled(false);
ui.bn2.setEnabled(false);
ui.bn3.setEnabled(false);
ui.bn4.setEnabled(false);
ui.bn5.setEnabled(false);
ui.bn6.setEnabled(false);
ui.bn7.setEnabled(false);
ui.bn8.setEnabled(false);
JOptionPane.showMessageDialog(this, "请输入函数表达式 !");
return;
}
}else flag = -2;
ui.bn1.setEnabled(true);
ui.bn2.setEnabled(true);
ui.bn3.setEnabled(true);
ui.bn4.setEnabled(true);
ui.bn5.setEnabled(true);
ui.bn6.setEnabled(true);
ui.bn7.setEnabled(true);
ui.bn8.setEnabled(true);
init();
if(ui.cb.isEnabled() == true)
{
flag = ui.cb.getSelectedIndex();
}
}
if(e.getSource() == ui.exit)
System.exit(0);
repaint();
}
public void mouseDragged(MouseEvent arg0)
{
}
public void mouseMoved(MouseEvent e)
{
pt = e.getPoint();
repaint();
}
}
刚学java时写着玩的
希望能够帮助到你,望采纳!
更多追问追答
追问
您好,我上次就是采用的您的答案,我现在能够实现输入函数生成函数图像了,但是就是运行一次之后图像就会消失,求指点!!!
追答
你不要运行,就选择调试试试看
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个方法是 java.io.DataOutput 接口中的一个方法。
下面是 JDK1.5 中的参考:
java.io
接口 DataOutput
所有已知子接口:
ImageOutputStream, ObjectOutput
所有已知实现类:
DataOutputStream, FileCacheImageOutputStream, FileImageOutputStream, ImageOutputStreamImpl, MemoryCacheImageOutputStream, ObjectOutputStream, RandomAccessFile
public interface DataOutput
DataOutput 接口用于将任意 Java 基本类型转换为一系列字节,并将这些字节写入二进制流。同时还提供了一个将 String 转换成 UTF-8 修改版格式并写入所得到的系列字节的工具。
再看 writeInt() 方法的说明:
writeIntvoid writeInt(int v) throws IOException
将一个 int 值写入输出流,该值由四个字节组成。要写入的字节值(按顺序显示)是:
(byte)(0xff & (v >> 24))
(byte)(0xff & (v >> 16))
(byte)(0xff & (v >> 8))
(byte)(0xff & v)
此方法写入的字节可由 DataInput 接口的 readInt 方法读取,然后该方法将返回一个等于 v 的 int 值。
参数:
v - 要写入的 int 值。
根据以上说明,这个方法用于把一个 整型数据 转换成 4 个字节的 byte ,然后写入二进制流。如果写入文件的话,那这个文件是二进制文件,而不是文本文件。
在使用的时候,如果使用了 writeInt() 这类方法(writeDouble,writeFloat等),那么在读取的时候,必须使用实现了 DataInput 接口的输入流读取。而且要按照写入的顺序读取:例:
package com.test;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
public class DataStream {
public static void main(String[] args) {
try{
//下面是输出, 这里只是输出到内存
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(bos));
dos.writeInt(22);
dos.writeDouble(33.33);
dos.writeLong(33l);
dos.writeBoolean(true);
dos.writeChar('a');
dos.writeByte(33);
dos.writeFloat(44.4f);
dos.flush();
//下面是输入, 这里也只是从内存输入
DataInputStream dis = new DataInputStream(new BufferedInputStream(new ByteArrayInputStream(bos.toByteArray())));
System.out.println(dis.readInt());
System.out.println(dis.readDouble());
System.out.println(dis.readLong());
System.out.println(dis.readBoolean());
System.out.println(dis.readChar());
System.out.println(dis.readByte());
System.out.println(dis.readFloat());
dis.close();
bos.close();
}catch(Exception e){
}
}
}
上面程序最后的打印语句,你可以调换顺序,结果应该会与你写入的不一样,也就是会造成读取错误,因些读取顺序要和写入的一样。
下面是 JDK1.5 中的参考:
java.io
接口 DataOutput
所有已知子接口:
ImageOutputStream, ObjectOutput
所有已知实现类:
DataOutputStream, FileCacheImageOutputStream, FileImageOutputStream, ImageOutputStreamImpl, MemoryCacheImageOutputStream, ObjectOutputStream, RandomAccessFile
public interface DataOutput
DataOutput 接口用于将任意 Java 基本类型转换为一系列字节,并将这些字节写入二进制流。同时还提供了一个将 String 转换成 UTF-8 修改版格式并写入所得到的系列字节的工具。
再看 writeInt() 方法的说明:
writeIntvoid writeInt(int v) throws IOException
将一个 int 值写入输出流,该值由四个字节组成。要写入的字节值(按顺序显示)是:
(byte)(0xff & (v >> 24))
(byte)(0xff & (v >> 16))
(byte)(0xff & (v >> 8))
(byte)(0xff & v)
此方法写入的字节可由 DataInput 接口的 readInt 方法读取,然后该方法将返回一个等于 v 的 int 值。
参数:
v - 要写入的 int 值。
根据以上说明,这个方法用于把一个 整型数据 转换成 4 个字节的 byte ,然后写入二进制流。如果写入文件的话,那这个文件是二进制文件,而不是文本文件。
在使用的时候,如果使用了 writeInt() 这类方法(writeDouble,writeFloat等),那么在读取的时候,必须使用实现了 DataInput 接口的输入流读取。而且要按照写入的顺序读取:例:
package com.test;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
public class DataStream {
public static void main(String[] args) {
try{
//下面是输出, 这里只是输出到内存
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(bos));
dos.writeInt(22);
dos.writeDouble(33.33);
dos.writeLong(33l);
dos.writeBoolean(true);
dos.writeChar('a');
dos.writeByte(33);
dos.writeFloat(44.4f);
dos.flush();
//下面是输入, 这里也只是从内存输入
DataInputStream dis = new DataInputStream(new BufferedInputStream(new ByteArrayInputStream(bos.toByteArray())));
System.out.println(dis.readInt());
System.out.println(dis.readDouble());
System.out.println(dis.readLong());
System.out.println(dis.readBoolean());
System.out.println(dis.readChar());
System.out.println(dis.readByte());
System.out.println(dis.readFloat());
dis.close();
bos.close();
}catch(Exception e){
}
}
}
上面程序最后的打印语句,你可以调换顺序,结果应该会与你写入的不一样,也就是会造成读取错误,因些读取顺序要和写入的一样。
追问
谢谢,不过我的不是字符输入,而是函数输入,生成图像,楼上,
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询