1. 用Java编写一个绘制图形的小程序
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
//不规则图形的绘制
public class IrregularShapeDemo extends JFrame {
GeneralPath gPath= new GeneralPath(); //GeneralPath对象实例
Point aPoint;
//构造函数
public IrregularShapeDemo() {
super("不规则图形的绘制"); //调用父类构造函数
enableEvents(AWTEvent.MOUSE_EVENT_MASK|AWTEvent.MOUSE_MOTION_EVENT_MASK); //允许事件
setSize(300, 200); //设置窗口尺寸
setVisible(true); //设置窗口可视
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序
}
public void paint(Graphics g) { //重载窗口组件的paint()方法
Graphics2D g2D = (Graphics2D)g; //获取图形环境
g2D.draw(gPath); //绘制路径
}
public static void main(String[] args) {
new IrregularShapeDemo();
}
protected void processMouseEvent(MouseEvent e) { //鼠标事件处理
if(e.getID() == MouseEvent.MOUSE_PRESSED) {
aPoint = e.getPoint(); //得到当前鼠标点
gPath = new GeneralPath(); //重新实例化GeneralPath对象
gPath.moveTo(aPoint.x,aPoint.y); //设置路径点
}
}
protected void processMouseMotionEvent(MouseEvent e) { //鼠标运动事件处理
if(e.getID() == MouseEvent.MOUSE_DRAGGED) {
aPoint = e.getPoint(); //得到当前鼠标点
gPath.lineTo(aPoint.x, aPoint.y); //设置路径
gPath.moveTo(aPoint.x, aPoint.y);
repaint(); //重绘组件
}
}
}
2. 求一个JAVA 图形类实现函数曲线自动绘制绘制系统
对不起 我不知道 但........进来给你加点人气吧....................
3. 基于java图形类实现函数曲线的绘制
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import com.bruceeckel.swing.*;// 这个包你要自己去下,此处无法上传,整个程序你还要修改一下,以满足f(x)=x^2+sin(x)在区间[-3,3]的函数图像
class SineDraw extends JPanel {
static final int SCALEFACTOR = 200;
int cycles;
int points;
double[] sines;
int[] pts;
SineDraw() { setCycles(5); }
public void setCycles(int newCycles) {
cycles = newCycles;
points = SCALEFACTOR * cycles * 2;
sines = new double[points];
pts = new int[points];
for(int i = 0; i < points; i++) {
double radians = (Math.PI/SCALEFACTOR) * i;
sines[i] = Math.sin(radians);
}
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
int maxWidth = getWidth();
double hstep = (double)maxWidth/(double)points;
int maxHeight = getHeight();
for(int i = 0; i < points; i++)
pts[i] = (int)(sines[i] * maxHeight/2 * .95
+ maxHeight/2);
g.setColor(Color.red);
for(int i = 1; i < points; i++) {
int x1 = (int)((i - 1) * hstep);
int x2 = (int)(i * hstep);
int y1 = pts[i-1];
int y2 = pts[i];
g.drawLine(x1, y1, x2, y2);
}
}
}
public class SineWave extends JApplet {
SineDraw sines = new SineDraw();
JSlider cycles = new JSlider(1, 30, 5);
public void init() {
Container cp = getContentPane();
cp.add(sines);
cycles.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent e) {
System.out.println( ((JSlider)e.getSource()).getValue());
sines.setCycles( ((JSlider)e.getSource()).getValue());
}
});
cp.add(BorderLayout.SOUTH, cycles);
}
public static void main(String[] args) {
Console.run(new SineWave(), 700, 400);
}
} ///:~
4. 请教如何用java编写一个函数图像生成的应用程序?谢谢!
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 = 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时写着玩的
5. 用java编写一个画图软件
package s;//包名
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JFrame {
int x1,y1,x2,y2;public Test(){
setVisible(true);
setSize(300,300) ;
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0) ; }});
addMouseListener(
new MouseAdapter() {
public void mousePressed(MouseEvent e){
x1=e.getX();
y1=e.getY();} });
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e){
x2=e.getX() ;
y2=e.getY() ;
repaint(); }});
}
public void paint(Graphics g)
{
g.drawLine(x1,y1,x2,y2);
x1=x2;
y1=y2;
}
public static void main(String args[])
{
new Test();
}
}
6. 关于java中画图形的paint方法
应该先调用父类的paint,即super.paint(g),不过最好的方法还是继承出一个JPanel的子类,然后重载它的paint方法,然后把这个子类的实例增加到JFrame的容器里
7. java编写一个简单的画图应用程序
请给我个邮箱。我发你个
8. java 画矩形程序,写得越简单越好!
具体步骤如下,我没有在环境下测试,直接手写的
1、继承容器
2、重写容器中的paint(Graphics g)方法或者paintComponent(Graphics g)
好好看些这两个是怎么回事,要不然会有很多问题
3、获得Graphics2D对象,Graphics2D g2 = (Graphics2D)g;
4、画图
public class test extends JPanel{
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D)g;
g2.setColor(red);
g2.drawRect(x,y,h,w);//x,y起点坐标,h高,w宽
}
}