java绘图小程序代码 java绘制图形代码
如何用Java编写一个绘制图形的小程序?
import java.awt.*;
成都创新互联公司专注于企业全网整合营销推广、网站重做改版、布尔津网站定制设计、自适应品牌网站建设、H5开发、商城开发、集团公司官网建设、外贸网站制作、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为布尔津等各大城市提供网站开发制作服务。
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(); //重绘组件
}
}
}
JAVA的画图小程序
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
class MyLine
{
private int x1,x2,y1,y2;
public MyLine(int x1,int x2,int y1,int y2)
{
this.x1=x1;
this.x2=x2;
this.y1=y1;
this.y2=y2;
}
public void drawMe(Graphics g)
{
g.drawLine(x1,y1,x2,y2);
g.drawString("起始坐标:"+new String(x1+","+y1),x1,y1);
}
}
public class DrawLine extends Frame
{
Vector v=new Vector();
public static void main(String[]args)
{
DrawLine dl=new DrawLine();
dl.init();
}
public void paint(Graphics g)
{
g.setColor(Color.BLUE);
Enumeration en=v.elements();
while(en.hasMoreElements())
{
MyLine m=(MyLine)en.nextElement();
m.drawMe(g);
}
}
public void init()
{
setSize(300,300);
setVisible(true);
addMouseListener(new MouseAdapter()
{
int x1,x2,y1,y2;
public void mousePressed(MouseEvent e)
{
x1=e.getX();
y1=e.getY();
}
public void mouseReleased(MouseEvent e)
{
x2=e.getX();
y2=e.getY();
Graphics g=e.getComponent().getGraphics();
g.setColor(Color.BLUE);
g.drawLine(x1,y1,x2,y2);
g.drawString(new String("起始点坐标:"+x1+","+y1),x1,y1);
v.add(new MyLine(x1,x2,y1,y2)); // here pay attention
}
});
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
((Window)e.getSource()).dispose();
System.exit(0);
}}
);
}
}
求JAVA大神给我发一段完整可运行的java图形小程序的代码(不用太多类),谢谢了
/*计算器*/
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
public class Calculator implements ActionListener{
JFrame frame;
JPanel panel;
JTextField tfShow;/*定义显示文本框*/
JButton b1[]=new JButton[10]; /*数字按钮*/
JButton b2[]=new JButton[6]; /*操作按钮*/
boolean isNumber;/*判断是否输入多位数字的变量*/
double number;/*存储输入数值、显示结果的变量*/
double result;/*存储中间运算结果的变量*/
char operator;/*存储当前操作符的成员变量*/
public Calculator(){
frame=new JFrame("计算器");
frame.setSize(300,300);/*指定框架窗口的大小*/
frame.setResizable(false);/*使框架窗口不可改变大小*/
JPanel contentPane=(JPanel)frame.getContentPane();
contentPane.setBorder(new EmptyBorder(20,20,20,20));/*绘制框架的指定大小的空透明边框*/
tfShow=new JTextField("0",25);/*指定属性的文本域*/
tfShow.setHorizontalAlignment(JTextField.RIGHT);/*设置文本域中文本的对齐方式*/
isNumber=true;/*初始值设置*/
number=0;/*初始值设置*/
result=0;/*初始值设置*/
operator=' ';/*初始值设置*/
for(int i=0;ib1.length;i++){
b1[i]=new JButton(Integer.toString(i));/*创建数字按钮*/
b1[i].setActionCommand(Integer.toString(i));
b1[i].addActionListener(this);
b1[i].setForeground(Color.blue);
}
String bs[]={"/","*","-","C","+","="};
for(int i=0;ib2.length;i++){
b2[i]=new JButton(bs[i]);/*创建操作按钮*/
b2[i].setActionCommand(bs[i]);
b2[i].addActionListener(this);
b2[i].setForeground(Color.red);
}
panel=new JPanel();
panel.setLayout(new GridLayout(4,5));
panel.add(b1[1]);
panel.add(b1[2]);
panel.add(b1[3]);
panel.add(b2[0]);
panel.add(b1[4]);
panel.add(b1[5]);
panel.add(b1[6]);
panel.add(b2[1]);
panel.add(b1[7]);
panel.add(b1[8]);
panel.add(b1[9]);
panel.add(b2[2]);
panel.add(b1[0]);
panel.add(b2[3]);
panel.add(b2[4]);
panel.add(b2[5]);
frame.add(tfShow,BorderLayout.NORTH);/*将文本框放置在框架上方*/
frame.add(panel,BorderLayout.CENTER);/*将装有按钮组的panel放在框架的中心*/
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/*设置框架窗口的默认窗口关闭操作*/
frame.setVisible(true);/*设置框架可见*/
}
public double getDisplay(){/*返回要显示的结果*/
return number;
}
public void reDisplay(){/*刷新文本域的内容*/
tfShow.setText(""+getDisplay());
}
/*对输入数字的处理*/
public void numberProcess(int num){
if(isNumbernum!=0){
String s1=Integer.toString(num);
String s2=Integer.toString((int)(this.number));
this.number=Double.parseDouble(s2+s1);/*对多位数字的处理*/
}else{
this.number=num;
}
isNumber=true;/*输入连续数字(即多位数字)时为真*/
}
public void operationProcess(char operator){/*根据输入的操作符改变当前操作符*/
switch(operator){
case '-':
this.operator='-';
break;
case '+':
this.operator='+';
break;
case '*':
this.operator='*';
break;
case '/':
this.operator='/';
break;
}
result=number;
isNumber=false;/*输入操作符时表示输入连续数字的标记变量为假*/
}
public void clear(){
number=0;
result=0;
}
public void equal(){/*计算运算结果*/
switch(operator){
case '-':
result=result-number;
break;
case '+':
result=result+number;
break;
case '*':
result=result*number;
break;
case '/':
result=result/number;
break;
case ' ':
result=number;
break;
}
number=result; /*把运算结果赋值给显示变量*/
isNumber=false;
operator=' ';
}
public static void main(String args[]){
Calculator cal=new Calculator();/*创建计算器*/
}
public void actionPerformed(ActionEvent e){
String command=e.getActionCommand();/*获取按钮激发的操作事件的命令名称*/
char c=command.charAt(0);/*将按钮命令名称的第一个字符赋值给一个字符c*/
switch(c){
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '0':
int number=Integer.parseInt(command);
numberProcess(number);/*输入数字的处理*/
break;
case '+':
case '-':
case '*':
case '/':
operationProcess(c);/*算数运算符的处理*/
break;
case '=':equal();break;/*计算运算结果*/
case 'C':clear();break;/*清零*/
}
reDisplay(); /*在文本域中显示信息*/
}
}
运行截图:
java绘图,求代码
上代码:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.LayoutManager;
import java.awt.Paint;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.event.*;
public class YuanYiDong extends JFrame{
private static int BANJIN=0;
private static int X=0;
private static int Y=0;
JTextField rTxt=new JTextField(5);
JTextField xField=new JTextField(5);
JTextField yField=new JTextField(5);
JButton paintBt=new JButton("画");
JLabel huaban=new huaban();
JPanel jPanel=new JPanel();
JLabel banjingLabel,xLabel,yLabel;
public YuanYiDong(){
banjingLabel=new JLabel("半径");
xLabel=new JLabel("X坐标");
yLabel=new JLabel("Y坐标");
this.setTitle("圆的移动");
this.setLocation(300,100);
this.setSize(500, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.add(rTxt);
jPanel.setLayout(new FlowLayout());
add(huaban,BorderLayout.CENTER);
jPanel.add(banjingLabel);
jPanel.add(rTxt);
jPanel.add(xLabel);
jPanel.add(xField);
jPanel.add(yLabel);
jPanel.add(yField);
jPanel.add(paintBt);
add(jPanel,BorderLayout.NORTH);
paintBt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
BANJIN=Integer.parseInt(rTxt.getText());
X=Integer.parseInt(xField.getText());
Y=Integer.parseInt(yField.getText());
huaban.repaint();
}
});
}
private void drawCirlce(Graphics g) {
g.setColor(Color.blue);
g.fillOval(X, Y, BANJIN,BANJIN);
}
public static void main(String[] args) {
YuanYiDong frame = new YuanYiDong();
}
public class huaban extends JLabel{
public huaban(){}
public void paint(Graphics g) {
Image image = createImage(getWidth(), getHeight());
drawCirlce(image.getGraphics());
g.drawImage(image, 0, 0, null);
}
}
}
给分吧!
编写一个绘图java 小程序,用户点击某一按钮后,程序绘制相应的图形(如点击"画圆",则绘制圆形);
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Tester extends Applet implements ActionListener
{
Button btn1,btn2,btn3,btn4;
public void init(){
setLayout(null);
btn1=new Button("绘制直线");
btn2=new Button("绘制矩形");
btn3=new Button("绘制圆");
add(btn1);
add(btn2);
add(btn3);
btn1.setBounds(10, 60, 60, 20);
btn2.setBounds(10, 90, 60, 20);
btn3.setBounds(10, 120, 60, 20);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
validate();
setVisible(true);
}
public void start(){
}
public void actionPerformed(ActionEvent e)
{
Graphics g=this.getGraphics();
if(e.getSource()==btn1)
{
g.drawLine(200, 100, 300, 200);
}
else if(e.getSource()==btn2)
{
g.drawRect(200, 250, 200, 200);
}
else
{
g.drawArc(200, 500, 200, 200, 0, 360);
}
}
}
当前标题:java绘图小程序代码 java绘制图形代码
URL地址:http://azwzsj.com/article/hjsoes.html