JAVA 里的Canvas类怎么用? 我画了个矩形,但是不知道怎么把它删除,求高手,最好能个我个例子。
删除的意思是让画板变为和原来一般没有画东西importjava.awt.Dimension;importjava.awt.event.*;publicclassYJY2{...
删除的意思是 让画板变为和原来一般没有画东西
import java.awt.Dimension;
import java.awt.event.*;
public class YJY2
{
Frame f1=new Frame("简单绘图");
Panel p1=new Panel();
Button bt1=new Button("绘制矩形");
Button bt2=new Button("绘制圆形");
Button bt3=new Button("清空");
Mycanvas drawArea1=new Mycanvas();
Mycanvas drawArea2=new Mycanvas();
public void set()
{
p1.add(bt1);
p1.add(bt2);
p1.add(bt3);
bt3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
drawArea1.repaint();
System.out.println("我靠!");
// drawArea1.update(g);
}
});
f1.add(p1,BorderLayout.SOUTH);
drawArea1.setPreferredSize(new Dimension(250,180));
drawArea2.setPreferredSize(new Dimension(300,180));
f1.add(drawArea1,BorderLayout.EAST);
f1.add(drawArea2,BorderLayout.WEST);
f1.pack();
f1.setVisible(true);
}
public static void main(String args[])
{
new YJY().set();
}
class Mycanvas extends Canvas
{
public void paint(Graphics g)
{
g.drawRect(80,60,150,92);
g.drawOval(90,70,100,60);
}
public void update(Graphics g)
{
}
}
}
我想按“清空”的时候把画板的东西清除掉
谢谢,但是我想的是把它清除掉,不是设置为不可见,能不能再帮帮忙,拜托了。 展开
import java.awt.Dimension;
import java.awt.event.*;
public class YJY2
{
Frame f1=new Frame("简单绘图");
Panel p1=new Panel();
Button bt1=new Button("绘制矩形");
Button bt2=new Button("绘制圆形");
Button bt3=new Button("清空");
Mycanvas drawArea1=new Mycanvas();
Mycanvas drawArea2=new Mycanvas();
public void set()
{
p1.add(bt1);
p1.add(bt2);
p1.add(bt3);
bt3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
drawArea1.repaint();
System.out.println("我靠!");
// drawArea1.update(g);
}
});
f1.add(p1,BorderLayout.SOUTH);
drawArea1.setPreferredSize(new Dimension(250,180));
drawArea2.setPreferredSize(new Dimension(300,180));
f1.add(drawArea1,BorderLayout.EAST);
f1.add(drawArea2,BorderLayout.WEST);
f1.pack();
f1.setVisible(true);
}
public static void main(String args[])
{
new YJY().set();
}
class Mycanvas extends Canvas
{
public void paint(Graphics g)
{
g.drawRect(80,60,150,92);
g.drawOval(90,70,100,60);
}
public void update(Graphics g)
{
}
}
}
我想按“清空”的时候把画板的东西清除掉
谢谢,但是我想的是把它清除掉,不是设置为不可见,能不能再帮帮忙,拜托了。 展开
展开全部
重画Canvas 用Graphrics的getColor()方法获得Color.white然后调用drawRect(x,y,getWidth(),getHight())方法重画canvas就ok了,不行再repaint()一下就ok了 x,y值是你的canvas的起点坐标
更多追问追答
追问
我也是这样想的,但是不知道怎么实现,能不能把代码给我?谢谢了。
追答
import java.awt.*;
import java.awt.Dimension;
import java.awt.event.*;
public class YJY2
{
Frame f1=new Frame("简单绘图");
Panel p1=new Panel();
Button bt1=new Button("绘制矩形");
Button bt2=new Button("绘制圆形");
Button bt3=new Button("清空");
Mycanvas drawArea1=new Mycanvas();
Mycanvas drawArea2=new Mycanvas();
public YJY2()
{
p1.add(bt1);
p1.add(bt2);
p1.add(bt3);
bt3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Graphics gg=drawArea1.getGraphics();
drawArea1.update(gg);
System.out.println("我想就是这样的");
}
});
f1.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
f1.add(p1,BorderLayout.SOUTH);
drawArea1.setPreferredSize(new Dimension(250,180));
drawArea2.setPreferredSize(new Dimension(300,180));
f1.add(drawArea1,BorderLayout.EAST);
f1.add(drawArea2,BorderLayout.WEST);
f1.pack();
f1.setVisible(true);
}
public static void main(String args[])
{
new YJY2();
}
class Mycanvas extends Canvas
{
public void paint(Graphics g)
{
g.drawRect(80,60,150,92);
g.drawOval(90,70,100,60);
}
public void update(Graphics g)
{
g.setColor(Color.white);
g.fillRect(0,0,getWidth(),getHeight());
}
}
}
方法是创建一个Graphics的上下文引用gg给drawArea1,然后我写了楼主的update()就是用白色的矩形覆盖原内容,然后在action使用中调用update()传参是gg即drawArea1...主要是这句Graphics gg=drawArea1.getGraphics();程序在netBeans中可以运行但在dos下编译时提示写入匿名类时出错,楼主在重构一下程序应该就ok了
展开全部
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class YJY2 implements ActionListener
{
Frame f1=new Frame("简单绘图");
Panel p1=new Panel();
Button bt1=new Button("绘制矩形");
Button bt2=new Button("绘制圆形");
Button bt3=new Button("清空");
Mycanvas drawArea1=new Mycanvas();
Mycanvas drawArea2=new Mycanvas();
public void set()
{
p1.add(bt1);
p1.add(bt2);
p1.add(bt3);
bt3.addActionListener(this);
f1.add(p1,BorderLayout.SOUTH);
drawArea1.setPreferredSize(new Dimension(250,180));
drawArea2.setPreferredSize(new Dimension(300,180));
f1.add(drawArea1,BorderLayout.EAST);
f1.add(drawArea2,BorderLayout.WEST);
f1.pack();
f1.setVisible(true);
}
public static void main(String args[])
{
new YJY2().set();
}
class Mycanvas extends Canvas
{
public void paint(Graphics g)
{
g.drawRect(80,60,150,92);
g.drawOval(90,70,100,60);
}
public void update(Graphics g)
{
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==bt3){
drawArea1.setVisible(false);
drawArea2.setVisible(false);
}
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class YJY2 implements ActionListener
{
Frame f1=new Frame("简单绘图");
Panel p1=new Panel();
Button bt1=new Button("绘制矩形");
Button bt2=new Button("绘制圆形");
Button bt3=new Button("清空");
Mycanvas drawArea1=new Mycanvas();
Mycanvas drawArea2=new Mycanvas();
public void set()
{
p1.add(bt1);
p1.add(bt2);
p1.add(bt3);
bt3.addActionListener(this);
f1.add(p1,BorderLayout.SOUTH);
drawArea1.setPreferredSize(new Dimension(250,180));
drawArea2.setPreferredSize(new Dimension(300,180));
f1.add(drawArea1,BorderLayout.EAST);
f1.add(drawArea2,BorderLayout.WEST);
f1.pack();
f1.setVisible(true);
}
public static void main(String args[])
{
new YJY2().set();
}
class Mycanvas extends Canvas
{
public void paint(Graphics g)
{
g.drawRect(80,60,150,92);
g.drawOval(90,70,100,60);
}
public void update(Graphics g)
{
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==bt3){
drawArea1.setVisible(false);
drawArea2.setVisible(false);
}
}
}
追问
谢谢,可能是我没说清楚。我想的是把它清除掉,就是画板还在,但是没有图形,不是设置为不可见,能不能再帮帮忙,拜托了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
其实你想复杂了,所谓删除,我的解决就是再画个图把原来的覆盖填充白色,我不了你所说的
给你个例子因为一个小圆球其实就是个图如果要实现运行1线程,2是没跑一步把上一个图覆盖
给你给例子
import java.util.Random;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
public class RandomQiu extends Canvas implements Runnable{
private int x,y,h;
private int red,green,blue;
int flag=0;
public RandomQiu(){
this.setTitle("随机小球");
x=16;
y=16;
h=20;
this.repaint();
new Thread(this).start();
}
protected void paint(Graphics g) {
g.setColor(255,255,255);
g.fillRect(0,0,this.getWidth(),this.getHeight());
g.setColor(red,green,blue);
g.fillRoundRect(x,y,h,h,h,h);
}
protected void keyPressed(int keyCode) {
int keyValue=this.getGameAction(keyCode);
if(keyValue==Canvas.FIRE){
Random r=new Random();
red=r.nextInt(255);
green=r.nextInt(255);
blue=r.nextInt(255);
}
}
public void run() {
while(true){
this.repaint();
if(flag==0){
x++;
y--;
}
if(flag==1){
x++;
y++;
}
if(flag==2){
x--;
y++;
}
if(flag==3){
x--;
y--;
}
if(x<=0){
flag=0;
}
if(y<=0){
flag=1;
}
if(x+h>=this.getWidth()){
flag=2;
}
if(y+h>=this.getHeight()){
flag=3;
}
try {
Thread.sleep(30);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
此2句 g.setColor(255,255,255);
g.fillRect(0,0,this.getWidth(),this.getHeight());
就是给画笔设置颜色白色,并填充屏幕的左脚0,0画到屏幕大小,希望对你有帮助
给你个例子因为一个小圆球其实就是个图如果要实现运行1线程,2是没跑一步把上一个图覆盖
给你给例子
import java.util.Random;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
public class RandomQiu extends Canvas implements Runnable{
private int x,y,h;
private int red,green,blue;
int flag=0;
public RandomQiu(){
this.setTitle("随机小球");
x=16;
y=16;
h=20;
this.repaint();
new Thread(this).start();
}
protected void paint(Graphics g) {
g.setColor(255,255,255);
g.fillRect(0,0,this.getWidth(),this.getHeight());
g.setColor(red,green,blue);
g.fillRoundRect(x,y,h,h,h,h);
}
protected void keyPressed(int keyCode) {
int keyValue=this.getGameAction(keyCode);
if(keyValue==Canvas.FIRE){
Random r=new Random();
red=r.nextInt(255);
green=r.nextInt(255);
blue=r.nextInt(255);
}
}
public void run() {
while(true){
this.repaint();
if(flag==0){
x++;
y--;
}
if(flag==1){
x++;
y++;
}
if(flag==2){
x--;
y++;
}
if(flag==3){
x--;
y--;
}
if(x<=0){
flag=0;
}
if(y<=0){
flag=1;
}
if(x+h>=this.getWidth()){
flag=2;
}
if(y+h>=this.getHeight()){
flag=3;
}
try {
Thread.sleep(30);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
此2句 g.setColor(255,255,255);
g.fillRect(0,0,this.getWidth(),this.getHeight());
就是给画笔设置颜色白色,并填充屏幕的左脚0,0画到屏幕大小,希望对你有帮助
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询