「java绘图交互」java 绘图

博主:adminadmin 2022-12-28 03:42:09 53

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

本文目录一览:

Java中如何让使用Python的统计包绘图

看来是同道。你提到的这个问题很难。

java调用python容易。 java甚至可以直接调用python的类。python调用java更容易了。

不过GUI要想融合,据目前20年的技术来看,只有本土的可以。 比如以前的微软件ActiveX,不管你是什么语言开发的都可以在windows下用OLE方式嵌入。

java的制图功能,因为它的设计理念 ,它是封闭的。也就是说,除非你使用了它本地化的GUI方法,否则就不可能实现。

那么说,如果我一定要实现怎么办呢?只能走很长的弯路。方法还是有几个的。

方法1:

在java的panel里嵌入一个浏览器,然后在浏览器里显示统计图表。这个真是不要太容易了。 不管是你是python生成的本地图片,还是直接用javascript生成的图都可以嵌入进去。美观不用说

方法2:

绘图使用开源的,比如plt之类的。不过它被本地化成java版本的。然后用java调用python,再用python产生数据后,通过jython调用java本地化的绘图工具。

表面上看,这个东西就是没有价值的,为什么不直接用java调用绘图。关键在于python本身对于数据处理的优势太明显。轻松就可以完成复杂的数据结构处理。所以还是有价值的

方法3:

浮动窗口方式。这个就不说了。如果你的java是固定在窗口特定位置的。这个就容易了。怎么浮动窗口要根据操作系统而定。

方法4:简单方案

python生成图片后,输出成JPEG或者是PNG或者是GIF,然后让JAVA显示这个图片。这个可能是最最简单的。

方法5:windows专用,不知道可否使用

仅限于特定场景,在要显示图片的地方,显示一个品红色的纯色图。然后让python的图形输出转到directshow之类的API,直接写显卡。这样就可以显示动画效果。

java窗口间交互问题

你这个需要监听,并且还要调用repaint函数进行重绘,这个实现没那么难吧,最好使用卡片布局CardLayout,这样你就可以事先绘制好网格,需要的时候通过监听按钮调出来就可以了

如何用java实现画图

使用Java的Graphics类进行绘图

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

怎么用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实现简单的画图板

楼主写一个html,很容易把下面代码嵌入到applet,可以google一下实现,

还有copy自己不知道算不算复制。。。-_-!

--------------------------------------------------------------------

楼主给你一个我的,直接保存成pb.java编译运行,就是你要的画图功能 ,可以参考一下

____________________________________________________________________

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import javax.swing.*;

import java.awt.geom.*;

import java.io.*;

class Point implements Serializable

{

int x,y;

Color col;

int tool;

int boarder;

Point(int x, int y, Color col, int tool, int boarder)

{

this.x = x;

this.y = y;

this.col = col;

this.tool = tool;

this.boarder = boarder;

}

}

class paintboard extends Frame implements ActionListener,MouseMotionListener,MouseListener,ItemListener

{

int x = -1, y = -1;

int con = 1;//画笔大小

int Econ = 5;//橡皮大小

int toolFlag = 0;//toolFlag:工具标记

//toolFlag工具对应表:

//(0--画笔);(1--橡皮);(2--清除);

//(3--直线);(4--圆);(5--矩形);

Color c = new Color(0,0,0); //画笔颜色

BasicStroke size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);//画笔粗细

Point cutflag = new Point(-1, -1, c, 6, con);//截断标志

Vector paintInfo = null;//点信息向量组

int n = 1;

FileInputStream picIn = null;

FileOutputStream picOut = null;

ObjectInputStream VIn = null;

ObjectOutputStream VOut = null;

// *工具面板--画笔,直线,圆,矩形,多边形,橡皮,清除*/

Panel toolPanel;

Button eraser, drLine,drCircle,drRect;

Button clear ,pen;

Choice ColChoice,SizeChoice,EraserChoice;

Button colchooser;

Label 颜色,大小B,大小E;

//保存功能

Button openPic,savePic;

FileDialog openPicture,savePicture;

paintboard(String s)

{

super(s);

addMouseMotionListener(this);

addMouseListener(this);

paintInfo = new Vector();

/*各工具按钮及选择项*/

//颜色选择

ColChoice = new Choice();

ColChoice.add("black");

ColChoice.add("red");

ColChoice.add("blue");

ColChoice.add("green");

ColChoice.addItemListener(this);

//画笔大小选择

SizeChoice = new Choice();

SizeChoice.add("1");

SizeChoice.add("3");

SizeChoice.add("5");

SizeChoice.add("7");

SizeChoice.add("9");

SizeChoice.addItemListener(this);

//橡皮大小选择

EraserChoice = new Choice();

EraserChoice.add("5");

EraserChoice.add("9");

EraserChoice.add("13");

EraserChoice.add("17");

EraserChoice.addItemListener(this);

////////////////////////////////////////////////////

toolPanel = new Panel();

clear = new Button("清除");

eraser = new Button("橡皮");

pen = new Button("画笔");

drLine = new Button("画直线");

drCircle = new Button("画圆形");

drRect = new Button("画矩形");

openPic = new Button("打开图画");

savePic = new Button("保存图画");

colchooser = new Button("显示调色板");

//各组件事件监听

clear.addActionListener(this);

eraser.addActionListener(this);

pen.addActionListener(this);

drLine.addActionListener(this);

drCircle.addActionListener(this);

drRect.addActionListener(this);

openPic.addActionListener(this);

savePic.addActionListener(this);

colchooser.addActionListener(this);

颜色 = new Label("画笔颜色",Label.CENTER);

大小B = new Label("画笔大小",Label.CENTER);

大小E = new Label("橡皮大小",Label.CENTER);

//面板添加组件

toolPanel.add(openPic);

toolPanel.add(savePic);

toolPanel.add(pen);

toolPanel.add(drLine);

toolPanel.add(drCircle);

toolPanel.add(drRect);

toolPanel.add(颜色); toolPanel.add(ColChoice);

toolPanel.add(大小B); toolPanel.add(SizeChoice);

toolPanel.add(colchooser);

toolPanel.add(eraser);

toolPanel.add(大小E); toolPanel.add(EraserChoice);

toolPanel.add(clear);

//工具面板到APPLET面板

add(toolPanel,BorderLayout.NORTH);

setBounds(60,60,900,600); setVisible(true);

validate();

//dialog for save and load

openPicture = new FileDialog(this,"打开图画",FileDialog.LOAD);

openPicture.setVisible(false);

savePicture = new FileDialog(this,"保存图画",FileDialog.SAVE);

savePicture.setVisible(false);

openPicture.addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{ openPicture.setVisible(false); }

});

savePicture.addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{ savePicture.setVisible(false); }

});

addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{ System.exit(0);}

});

}

public void paint(Graphics g)

{

Graphics2D g2d = (Graphics2D)g;

Point p1,p2;

n = paintInfo.size();

if(toolFlag==2)

g.clearRect(0,0,getSize().width,getSize().height);//清除

for(int i=0; in ;i++){

p1 = (Point)paintInfo.elementAt(i);

p2 = (Point)paintInfo.elementAt(i+1);

size = new BasicStroke(p1.boarder,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);

g2d.setColor(p1.col);

g2d.setStroke(size);

if(p1.tool==p2.tool)

{

switch(p1.tool)

{

case 0://画笔

Line2D line1 = new Line2D.Double(p1.x, p1.y, p2.x, p2.y);

g2d.draw(line1);

break;

case 1://橡皮

g.clearRect(p1.x, p1.y, p1.boarder, p1.boarder);

break;

case 3://画直线

Line2D line2 = new Line2D.Double(p1.x, p1.y, p2.x, p2.y);

g2d.draw(line2);

break;

case 4://画圆

Ellipse2D ellipse = new Ellipse2D.Double(p1.x, p1.y, Math.abs(p2.x-p1.x) , Math.abs(p2.y-p1.y));

g2d.draw(ellipse);

break;

case 5://画矩形

Rectangle2D rect = new Rectangle2D.Double(p1.x, p1.y, Math.abs(p2.x-p1.x) , Math.abs(p2.y-p1.y));

g2d.draw(rect);

break;

case 6://截断,跳过

i=i+1;

break;

default :

}//end switch

}//end if

}//end for

}

public void itemStateChanged(ItemEvent e)

{

if(e.getSource()==ColChoice)//预选颜色

{

String name = ColChoice.getSelectedItem();

if(name=="black")

{c = new Color(0,0,0); }

else if(name=="red")

{c = new Color(255,0,0);}

else if(name=="green")

{c = new Color(0,255,0);}

else if(name=="blue")

{c = new Color(0,0,255);}

}

else if(e.getSource()==SizeChoice)//画笔大小

{

String selected = SizeChoice.getSelectedItem();

if(selected=="1")

{

con = 1;

size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);

}

else if(selected=="3")

{

con = 3;

size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);

}

else if(selected=="5")

{con = 5;

size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);

}

else if(selected=="7")

{con = 7;

size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);

}

else if(selected=="9")

{con = 9;

size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);

}

}

else if(e.getSource()==EraserChoice)//橡皮大小

{

String Esize = EraserChoice.getSelectedItem();

if(Esize=="5")

{ Econ = 5*2; }

else if(Esize=="9")

{ Econ = 9*2; }

else if(Esize=="13")

{ Econ = 13*2; }

else if(Esize=="17")

{ Econ = 17*3; }

}

}

public void mouseDragged(MouseEvent e)

{

Point p1 ;

switch(toolFlag){

case 0://画笔

x = (int)e.getX();

y = (int)e.getY();

p1 = new Point(x, y, c, toolFlag, con);

paintInfo.addElement(p1);

repaint();

break;

case 1://橡皮

x = (int)e.getX();

y = (int)e.getY();

p1 = new Point(x, y, null, toolFlag, Econ);

paintInfo.addElement(p1);

repaint();

break;

default :

}

}

public void mouseMoved(MouseEvent e) {}

public void update(Graphics g)

{

paint(g);

}

public void mousePressed(MouseEvent e)

{

Point p2;

switch(toolFlag){

case 3://直线

x = (int)e.getX();

y = (int)e.getY();

p2 = new Point(x, y, c, toolFlag, con);

paintInfo.addElement(p2);

break;

case 4: //圆

x = (int)e.getX();

y = (int)e.getY();

p2 = new Point(x, y, c, toolFlag, con);

paintInfo.addElement(p2);

break;

case 5: //矩形

x = (int)e.getX();

y = (int)e.getY();

p2 = new Point(x, y, c, toolFlag, con);

paintInfo.addElement(p2);

break;

default :

}

}

public void mouseReleased(MouseEvent e)

{

Point p3;

switch(toolFlag){

case 0://画笔

paintInfo.addElement(cutflag);

break;

case 1: //eraser

paintInfo.addElement(cutflag);

break;

case 3://直线

x = (int)e.getX();

y = (int)e.getY();

p3 = new Point(x, y, c, toolFlag, con);

paintInfo.addElement(p3);

paintInfo.addElement(cutflag);

repaint();

break;

case 4: //圆

x = (int)e.getX();

y = (int)e.getY();

p3 = new Point(x, y, c, toolFlag, con);

paintInfo.addElement(p3);

paintInfo.addElement(cutflag);

repaint();

break;

case 5: //矩形

x = (int)e.getX();

y = (int)e.getY();

p3 = new Point(x, y, c, toolFlag, con);

paintInfo.addElement(p3);

paintInfo.addElement(cutflag);

repaint();

break;

default:

}

}

public void mouseEntered(MouseEvent e){}

public void mouseExited(MouseEvent e){}

public void mouseClicked(MouseEvent e){}

public void actionPerformed(ActionEvent e)

{

if(e.getSource()==pen)//画笔

{toolFlag = 0;}

if(e.getSource()==eraser)//橡皮

{toolFlag = 1;}

if(e.getSource()==clear)//清除

{

toolFlag = 2;

paintInfo.removeAllElements();

repaint();

}

if(e.getSource()==drLine)//画线

{toolFlag = 3;}

if(e.getSource()==drCircle)//画圆

{toolFlag = 4;}

if(e.getSource()==drRect)//画矩形

{toolFlag = 5;}

if(e.getSource()==colchooser)//调色板

{

Color newColor = JColorChooser.showDialog(this,"调色板",c);

c = newColor;

}

if(e.getSource()==openPic)//打开图画

{

openPicture.setVisible(true);

if(openPicture.getFile()!=null)

{

int tempflag;

tempflag = toolFlag;

toolFlag = 2 ;

repaint();

try{

paintInfo.removeAllElements();

File filein = new File(openPicture.getDirectory(),openPicture.getFile());

picIn = new FileInputStream(filein);

VIn = new ObjectInputStream(picIn);

paintInfo = (Vector)VIn.readObject();

VIn.close();

repaint();

toolFlag = tempflag;

}

catch(ClassNotFoundException IOe2)

{

repaint();

toolFlag = tempflag;

System.out.println("can not read object");

}

catch(IOException IOe)

{

repaint();

toolFlag = tempflag;

System.out.println("can not read file");

}

}

}

if(e.getSource()==savePic)//保存图画

{

savePicture.setVisible(true);

try{

File fileout = new File(savePicture.getDirectory(),savePicture.getFile());

picOut = new FileOutputStream(fileout);

VOut = new ObjectOutputStream(picOut);

VOut.writeObject(paintInfo);

VOut.close();

}

catch(IOException IOe)

{

System.out.println("can not write object");

}

}

}

}//end paintboard

public class pb

{

public static void main(String args[])

{ new paintboard("画图程序"); }

}

java 绘图程序

我基于你原来画图的方法,添加了事件触发的命令b[j].setActionCommand("b" + j);否则你不能在事件响应处理的方法中使用e.getActionCommand(),而且字符串的比较用equals方法比较好。现在可以运行了,你可以看一下:

import java.applet.Applet;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class drawing extends Applet implements ActionListener {

Button b[] = new Button[5];

String fontname = "仿宋_GB2312";

int style = Font.PLAIN;

int size = 24;

int index = 0;

Font myfont;

public void init() {

setSize(700,700);

myfont = new Font(fontname, style, size);

b[0] = new Button("扇形");

b[1] = new Button("圆形");

b[2] = new Button("三角形");

b[3] = new Button("长方形");

b[4] = new Button("椭圆形");

for (int j = 0; j b.length; j++) {

b[j].setBounds(10, 10, 50, 20);

b[j].addActionListener(this);

b[j].setActionCommand("b" + j);

add(b[j]);

}

}

public void actionPerformed(ActionEvent e) {

if (e.getActionCommand().equals("b0")) {

index = 0;

repaint();

}

if (e.getActionCommand().equals("b1")) {

index = 1;

repaint();

}

if (e.getActionCommand().equals("b2")) {

index = 2;

repaint();

}

if (e.getActionCommand().equals("b3")) {

index = 3;

repaint();

}

if (e.getActionCommand().equals("b4")) {

index = 4;

repaint();

}

}

public void paint(Graphics g) {

switch (index) {

case 0:

g.fillArc(0, 60, 80, 60, 30, 120);

break;

case 1:

g.drawOval( 300, 50, 60, 60);

break;

case 2:

Polygon filledPolygon = new Polygon();

filledPolygon.addPoint(380, 50);

filledPolygon.addPoint(380, 110);

filledPolygon.addPoint(450, 90);

g.drawPolygon(filledPolygon);

break;

case 3:

g.drawRect( 200, 50, 80, 60);

break;

case 4:

g.drawOval(100, 50, 80, 60);

break;

default:

g.fillArc(0, 60, 80, 60, 30, 120);

break;

}

}

/*

* public void paint(Graphics g) { g.fillArc( 0, 60, 80, 60, 30, 120);

* //绘制扇形 g.drawOval( 100, 50, 80, 60); g.drawRect( 200, 50, 80, 60);

* g.drawOval( 300, 50, 60, 60); Polygon filledPolygon=new Polygon();

* filledPolygon.addPoint(380,50); filledPolygon.addPoint(380,110);

* filledPolygon.addPoint(450,90); g.drawPolygon(filledPolygon); }

*/

}

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

The End

发布于:2022-12-28,除非注明,否则均为首码项目网原创文章,转载请注明出处。