求java画图的小问题 应用程序画图和applet里面画图都是一样的啊,比如JPanel上画图,你只要重载paint方法就可以画你自己的图形了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在http://javagarden.yeah.net/有一些关于画图的文章。 这是一个用箭头键控制绿色方块移动的程序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.*;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); }}此程序是借鉴上面程序来面的 package java2dwdztest;/** * <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(); }} 下面是一个类,太长点,分开2帖,拼起来就能运行!========================== 第一部分 ================================/** * [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(); } } 一画图就调用paint()我就不想这样干没其他的方法了吗?? onefox(一品狐):pack()很重要,没它就不能画图,这个语句什么意思? 要是没有paint()的话,你最小化后就看不到所绘的图了,这个比较重要!它是重画 CSDN好像不让上传文件,有些遗憾啊,这样好多例子,包括自己作的一些东西就不能分享了 java环境变量 path变量无效 java线程的问题,大家帮个忙啊!小弟急等!!! ★★★★★求《JAVA编程思想〉(第三版)练习答案★★★★★ 找出2个字符串中所有相同的字符 百分请问用HTTP与服务器端的通信问题 欢迎来讨论! 求教! 请问:有没有对数组(int)进行排序的的方法??? 所有的java程序都要从main()开始吗? String 格式化的问题,求大神帮助,急,急急!!! 根本不会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();
}
}
我就不想这样干
没其他的方法了吗??