「java绘图详解」java如何实现绘图

博主:adminadmin 2023-01-08 06:09:08 564

本篇文章给大家谈谈java绘图详解,以及java如何实现绘图对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

在Java中如何用程序画一个圆

使用java画圆要用到绘图类Graphics,下面是实例代码和运行效果:

package com.dikea.demo01;

import java.awt.*;

import javax.swing.*;

// java绘图原理

public class demo_01  extends JFrame {

MyPanel mp = null;

public static void main(String[] args) {

// TODO 自动生成的方法存根

demo_01 demo01 = new demo_01();

}

public demo_01(){

mp = new MyPanel();

this.add(mp);

this.setSize(400, 300);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

}

}

// 定义一个MyPanel面板,用于绘图区域

class MyPanel extends JPanel{

//覆盖JPanel

// Graphics 是绘图的重要类,可以理解成一支画笔

public void paint(Graphics g){

//  1. 调用父类函数完成初始化任务

//  这句话不可以少

super.paint(g);

// 先画出一个圆圈

g.drawOval(100, 100, 30, 30);

}

}

代码复制进ide编程工具,运行效果如下:

怎么用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 = 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的Graphics类进行绘图

Graphics类提供基本的几何图形绘制方法,主要有:画线段、画矩形、画圆、画带颜色的图形、画椭圆、画圆弧、画多边形等

关于java绘图详解和java如何实现绘图的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。