求java画图的小问题

chenzhengxi001 2004-05-07 10:23:33
求java画图的小问题
我始终只会用apple画图
我想在应用程序里画图,可是始终不会使用绘图函数.
不是这儿错就是那儿错.
我只求高手写个画图的模板.
这我还是能看懂的.
...全文
232 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
cuij7718 2004-09-02
  • 打赏
  • 举报
回复
CSDN好像不让上传文件,有些遗憾啊,这样好多例子,包括自己作的一些东西就不能分享了
zenithliu 2004-09-02
  • 打赏
  • 举报
回复
paintComponent()
andy_zhang_ping 2004-09-02
  • 打赏
  • 举报
回复
要是没有paint()的话,你最小化后就看不到所绘的图了,这个比较重要!它是重画
nmgtaozi 2004-09-02
  • 打赏
  • 举报
回复
onefox(一品狐):pack()很重要,没它就不能画图,这个语句什么意思?
chenzhengxi001 2004-05-10
  • 打赏
  • 举报
回复
一画图就调用paint()
我就不想这样干
没其他的方法了吗??
onefox 2004-05-08
  • 打赏
  • 举报
回复

========================== 第二部分 ================================


//按钮事件
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();
}

}
onefox 2004-05-08
  • 打赏
  • 举报
回复
下面是一个类,太长点,分开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());
}
}
});
wdz_wdz 2004-05-08
  • 打赏
  • 举报
回复
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();

}
}
山卜居士 2004-05-07
  • 打赏
  • 举报
回复
在http://javagarden.yeah.net/有一些关于画图的文章。
Acylas 2004-05-07
  • 打赏
  • 举报
回复
应用程序画图和applet里面画图都是一样的啊,比如JPanel上画图,你只要重载paint方法就可以画你自己的图形了
fishstudio 2004-05-07
  • 打赏
  • 举报
回复
他们说的很对,up以下,希望你早日看见
minghuitian 2004-05-07
  • 打赏
  • 举报
回复
up
rao99755518 2004-05-07
  • 打赏
  • 举报
回复
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);
}
}
此程序是借鉴上面程序来面的
viano 2004-05-07
  • 打赏
  • 举报
回复


我想应该区别不大!

可能你是走入误区!
无欲则钢 2004-05-07
  • 打赏
  • 举报
回复
这是一个用箭头键控制绿色方块移动的程序
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();
}

}
czhhua28 2004-05-07
  • 打赏
  • 举报
回复
用canvas类

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧