应用程序画图和applet里面画图都是一样的啊,比如JPanel上画图,你只要重载paint方法就可以画你自己的图形了
解决方案 »
- 如何获取JTree选中节点在整个树中的索引?
- 怎么关闭Socket连接
- 如何用Java实现DDA画线算法,逐点画线算法,BRESENHAM画线算法
- 关于Socket题,利用ObjectOutputStream发送给多个客户端的Vector,客户端接受的数据不一样? 这是怎么回事?
- 请问在哪里可以下载jspsmart组件??下载地址www.jspsmart.com已经关闭
- Linux下web应用中频繁刷新页面产生的问题
- 如何将日期转换为中文?
- eclipse怎么给java文件设置运行参数
- 初学java,请多多指点,谢谢!
- 请问:使用RMI的时候如何知道对方的IP地址?(zhoudan)
- 根本不会JAVA的人提出的问题,请赐教
- 为什么用servlet读入一个HTML文件后再显示出来时确没有了格式?
import java.awt.*;
import java.awt.event.*;public class GraphicTest extends Frame implements KeyListener
{
int x=50,y=50;
public void keyPressed(KeyEvent e)
{
int n=10;
switch(e.getKeyCode())
{
case 37: {x-=n;break;}
case 38: {y-=n;break;}
case 39: {x+=n;break;}
case 40: {y+=n;break;}
}
this.repaint();
}
public void keyTyped(KeyEvent e)
{}
public void keyReleased(KeyEvent e)
{} public GraphicTest() {
super("graphictest");
setSize(400,400);
this.addKeyListener(this);
this.setVisible(true);
}
public void paint(Graphics g)
{
g.setColor(Color.GREEN);
g.fill3DRect(x,y,30,30,true);
}
public static void main(String[] args) {
new GraphicTest();
}}
import java.awt.event.*;
import java.applet.*;
public class GraphicsTest extends Frame
{
int x=50,y=50;int n=10;
public void processKeyEvent(KeyEvent e)
{
if(KeyEvent.KEY_PRESSED==e.getID())
{
switch(e.getKeyCode())
{
case 37: {x-=n;break;}
case 38: {y-=n;break;}
case 39: {x+=n;break;}
case 40: {y+=n;break;}
}
this.repaint();
}
}
public GraphicsTest() {
super("graphictest");
setSize(400,400);
enableEvents(AWTEvent.KEY_EVENT_MASK);
this.setVisible(true);
}
public void paint(Graphics g)
{
g.setColor(Color.green);
g.fill3DRect(x,y,30,30,true);
}
public static void main(String[] args) {
GraphicTest Gra= new GraphicTest();
Gra.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
Gra.setSize(400,400);
Gra.setVisible(true);
}
}
此程序是借鉴上面程序来面的
* <p>Title: test java 2d use</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author wdz
* @version 1.0
*/import java.awt.*;
import java.awt.event.*;
import java.applet.*;public class GraphicsTest
extends Frame {
int x = 50, y = 50;
int n = 100;
public void processKeyEvent(KeyEvent e) {
if (KeyEvent.KEY_PRESSED == e.getID()) {
switch (e.getKeyChar()) {
case 'a': {
x -= n;
break;
}
case 'b': {
y -= n;
break;
}
case 'c': {
x += n;
break;
}
case 'd': {
y += n;
break;
}
default:{
y += n;
x += n;
}
}
this.repaint();
}
} public GraphicsTest() {
super("graphictest");
setSize(400, 400);
enableEvents(AWTEvent.KEY_EVENT_MASK);
this.setVisible(true);
} public void paint(Graphics g) { g.setColor(Color.green);
g.fill3DRect(x, y, 30, 30, true);
System.out.println("x="+x+",y="+y);
} public static void main(String[] args) {
GraphicsTest Gra = new GraphicsTest(); }
}
========================== 第一部分 ================================
/**
* [DrawDemo.java] 2D图形绘制类
*
* 2D图形绘制演示
* 创建日期:(2003-11-27)
* @author:XWTs
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.Math;
import javax.swing.border.*;
public class DrawDemo extends JFrame { private Container contentPane = this.getContentPane();
private Canvas canvas = new Canvas();
private Graphics GPH; //画布Graphics引用
private boolean isFill = false;
private JButton bDraw = new JButton("自由手绘");
private JButton bLine = new JButton("画直线");
private JButton bOval = new JButton("画椭圆");
private JButton bRect = new JButton("画矩形");
private JButton bRound = new JButton("画圆角矩形");
private JButton clear = new JButton("清空画面");
private JToolBar edit = new JToolBar();
private JButton lineColor = new JButton("前景色");
private JButton backColor = new JButton("背景色");
//private JButton catchPIC = new JButton("保存图片");
private JCheckBox noFill = new JCheckBox("不填充",true);
private Point start, end, tmpPoint;
private boolean draw = true;
private boolean line = false;
private boolean oval = false;
private boolean rect = false;
private boolean round = false;
/**
* 构造方法:创建面板
*/
public DrawDemo() {
super("\"二维图形绘制演示\" By 徐文滔");
JPanel btPanel = new JPanel();
btPanel.setBorder(new TitledBorder(new EtchedBorder()));
btPanel.add(bDraw);
btPanel.add(bLine);
btPanel.add(bOval);
btPanel.add(bRect);
btPanel.add(bRound);
btPanel.add(clear);
JPanel cvPanel = new JPanel();
cvPanel.setLayout(new BorderLayout());
cvPanel.setBorder(new CompoundBorder(
BorderFactory.createLineBorder(Color.BLACK,1),
BorderFactory.createLineBorder(Color.BLACK,1)));
canvas.setSize(500,300);
canvas.setBackground(Color.WHITE);
cvPanel.add(canvas);
edit.add(lineColor);
edit.add(backColor);
edit.add(noFill);
//edit.add(catchPIC);
contentPane.add(btPanel,BorderLayout.NORTH);
contentPane.add(cvPanel,BorderLayout.CENTER);
contentPane.add(edit,BorderLayout.SOUTH);
addDrawListener();
addEditListener();
showFace();
GPH = canvas.getGraphics();
}
/**
* 方法:添加绘图按钮事件
*/
public void addDrawListener() {
canvas.addMouseListener(new MouseAdapter() { //鼠标按下
public void mousePressed(MouseEvent e) {
start = new Point(e.getX(),e.getY()); //提取开始点
tmpPoint = new Point(e.getX(),e.getY());
}
public void mouseReleased(MouseEvent e) { //鼠标放开
end = new Point(e.getX(),e.getY()); //提取结束点
int width = end.x - start.x;
int hight = end.y - start.y;
if(line) { //画线
GPH.drawLine(start.x, start.y, end.x, end.y);
}
else {
end = new Point(Math.max(e.getX(),start.x),
Math.max(e.getY(),start.y));
start = new Point(Math.min(e.getX(),start.x),
Math.min(e.getY(),start.y));
if(oval) { //画椭圆
if(isFill)
GPH.fillOval(start.x, start.y, width, hight);
else
GPH.drawOval(start.x, start.y, width, hight);
}
if(rect) { //画矩形
if(isFill)
GPH.fillRect(start.x, start.y, width, hight);
else
GPH.drawRect(start.x, start.y, width, hight);
}
if(round) { //画圆角矩形
if(isFill)
GPH.fillRoundRect(start.x, start.y, width, hight, 10, 10);
else
GPH.drawRoundRect(start.x, start.y, width, hight, 10, 10);
}
}
}
}); canvas.addMouseMotionListener(new MouseMotionAdapter() { //鼠标拖动
public void mouseDragged(MouseEvent e) {
if(draw) { //画自由线
GPH.drawLine(tmpPoint.x,tmpPoint.y,e.getX(),e.getY());
tmpPoint = new Point(e.getX(),e.getY());
}
}
});
========================== 第二部分 ================================
//按钮事件
bDraw.addActionListener(new ActionListener(){ //"自由手绘"
public void actionPerformed(ActionEvent e){
setButtonFalse();
draw = true;
}
});
bLine.addActionListener(new ActionListener(){ //"画线"
public void actionPerformed(ActionEvent e){
setButtonFalse();
line = true;
}
});
bOval.addActionListener(new ActionListener(){ //"画椭圆"
public void actionPerformed(ActionEvent e){
setButtonFalse();
oval = true;
}
});
bRect.addActionListener(new ActionListener(){ //"画矩形"
public void actionPerformed(ActionEvent e){
setButtonFalse();
rect = true;
}
});
bRound.addActionListener(new ActionListener(){ //"画园角矩形"
public void actionPerformed(ActionEvent e){
setButtonFalse();
round = true;
}
});
clear.addActionListener(new ActionListener(){ //"清除"
public void actionPerformed(ActionEvent e){
canvas.repaint();
}
});
}
/**
* 方法:添加编辑按钮事件
*/
private void addEditListener() {
lineColor.addActionListener(new ActionListener(){ //"线条色"
public void actionPerformed(ActionEvent e){
Color tmpColor = JColorChooser.showDialog(null,"线条颜色选择",Color.BLACK);
if(tmpColor != null)
GPH.setColor(tmpColor);
}
});
backColor.addActionListener(new ActionListener(){ //"背景色"
public void actionPerformed(ActionEvent e){
Color tmpColor = JColorChooser.showDialog(null,"背景颜色选择",Color.WHITE);
if(tmpColor != null)
canvas.setBackground(tmpColor);
}
});
noFill.addActionListener(new ActionListener(){ //"填充选项"
public void actionPerformed(ActionEvent e){
if(noFill.isSelected())
isFill = false;
else
isFill = true;
}
});
}
/**
* 方法:按钮重设
*/
private void setButtonFalse() {
draw = false;
line = false;
oval = false;
rect = false;
round = false;
}
/**
* 方法:显示面板
*/
private void showFace() {
pack();
Toolkit tmpTK = Toolkit.getDefaultToolkit(); //新建工具包
Dimension dime = tmpTK.getScreenSize(); //取得屏幕大小
Dimension frameSize = this.getPreferredSize(); //取得面板大小
setLocation(dime.width/2 - (frameSize.width/2), //面板居中显示
dime.height/2 - (frameSize.height/2));
setResizable(false);
setVisible(true); //设置面板可视
//窗口关闭事件
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
/**
* 方法:程序入口
*/
public static void main(String args[]) {
//获取设置系统风格:
try {
String laf = UIManager.getSystemLookAndFeelClassName();
UIManager.setLookAndFeel(laf);
} catch (Exception e) {}
//全局字体设置
UIManager.put("Button.font",new Font("宋体",Font.PLAIN,12));
new DrawDemo();
}
}
我就不想这样干
没其他的方法了吗??