使用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编程工具,运行效果如下:
package mathimport 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 = 0i <600i += 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 = 0i <600i += 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 = 0i <600i += 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 = 0i <600i += 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 = 0i <600i += 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 = 0i <600i += 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 = 0i <600i += 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 = 0i <600i += 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 = 0i <600i += 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 = 0i <= 600 / leni++)
{
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绘图基本步骤一步步来。
swing 编程步骤:
1. 继承JFrame
2. 定义组件
3.创建组件(构造函数)
4.添加组件
5.对窗体设置
6.显示窗体
最终效果如下:
代码如下:
/*** 功能:画一个乌龟
*/
package com.test1
import java.awt.*
import javax.swing.*
public class MyTortoise extends JFrame{
MyPanel2 mp = null
//构造函数
public MyTortoise(){
mp = new MyPanel2()
this.add(mp)
this.setTitle("小乌龟,丑丑哒")
this.setSize(400,300)
this.setVisible(true)
this.setLocation(300,200)
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
}
public static void main(String[] args) {
MyTortoise mtg = new MyTortoise()
}
}
//我的面板。只有JPanel有画图方法,JFrame没有,故必须在JFrame中添加JPanel
class MyPanel2 extends JPanel{
//定义一个乌龟
Tortoise t = null
//构造函数
public MyPanel2(){
t = new Tortoise(100,100)
}
//画乌龟
public void drawTortoise(int x, int y, Graphics g){
//1.画脸
g.setColor(Color.green)
g.fillOval(x+60, y, 30, 15)
//2.画左眼
g.setColor(Color.black)
g.fillOval(x+65, y+3, 5, 5)
//3.画右眼
g.fillOval(x+78, y+3, 5, 5)
//4.画脖子
g.setColor(Color.green)
g.fillOval(x+70, y, 10, 42)
//5.画乌龟壳
g.setColor(Color.red)
g.fillOval(x+40, y+40, 70, 100)
//6.画左上脚
g.setColor(Color.green)
g.fillOval(x+15, y+60, 30, 10)
//7.画右上脚
g.fillOval(x+105, y+60, 30, 10)
//8.画左下脚
g.fillOval(x+15, y+110, 30, 10)
//9.画右下脚
g.fillOval(x+105, y+110, 30, 10)
//10.画尾巴
g.setColor(Color.black)
g.drawLine(x+70,y+140,x+130,y+210)
g.drawOval(x+95, y+150, 30, 30)
}
//覆盖JPanel的paint方法
//Graphics 是绘图的重要类。你可以把他理解成一只画笔
public void paint(Graphics g){
//1.调用父类函数完成初始化任务
//这句话不能少
super.paint(g)
//2.画乌龟,调用方法即可
this.drawTortoise(50, 50, g)
}
}
//定义一个乌龟类
class Tortoise {
//表示乌龟的横坐标
int x = 0
//表示乌龟的纵坐标
int y = 0
public int getX() {
return x
}
public void setX(int x) {
this.x = x
}
public int getY() {
return y
}
public void setY(int y) {
this.y = y
}
public Tortoise(int x, int y){
this.x = x
this.y = y
}
}