网站建设资讯

NEWS

网站建设资讯

java画个图代码 JAVA画图

java绘图,求代码

上代码:

创新互联成都企业网站建设服务,提供网站建设、做网站网站开发,网站定制,建网站,网站搭建,网站设计,响应式网站建设,网页设计师打造企业风格网站,提供周到的售前咨询和贴心的售后服务。欢迎咨询做网站需要多少钱:18980820575

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.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(); //重绘组件

}

}

}

求一个最简单的在JAVA上画图形的代码。

import java.awt.*;

class MyFrame extends Frame{

MyPanel p = new MyPanel();

public MyFrame(){

this.add(p);

this.setSize(300,300);

//省略了关闭窗口的代码

this.setVisible(true);

}

public static void main(String[] args){

new MyFrame();

}

class MyPanel extends Panel

{

public void paint(Graphics g){

g.drawLine(0,0,300,300);

}

}

}


当前标题:java画个图代码 JAVA画图
文章来源:http://cdweb.net/article/hghsep.html