java我的画布背景怎么不变颜色_画布背景背景透明化

是嘛,我记得好像试过。失败了

还有,在Applet上加入键盘事件没搞出来,加到Botton上了.

用别人的电脑才发现要点过【开始】才能移动,自己的一开始就有焦点。

半成品,就差历遍墙的数组(length-2)和BOTTON按键事件也没做  ;没发现array,昨天电脑也坏了,写不下去了。

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

class add方块 extends Canvas{

aa a;

add方块(aa o){

a=o;

setSize(301,451);

}

public void paint(Graphics g){

for(int i=0;i

for(int i1=0;i1

if(a.a[i][i1]){

g.setColor(new Color(0xaaaaaa));

g.fillRect((i1+a.n[0])*10,(i+a.n[1])*10,10,10);

g.setColor(new Color(0x333333));

g.drawRect((i1+a.n[0])*10,(i+a.n[1])*10,10,10);

}

}

}

for(int i=0;i<46;i++){

for(int i1=0;i1<30;i1++){

if(a.墙的数据[i][i1]){

g.setColor(new Color(0xcccccc));

g.fillRect(i1*10,i*10,10,10);

g.setColor(new Color(0x666666));

g.drawRect(i1*10,i*10,10,10);

}

}

}

}

}

public class aa extends Applet implements KeyListener,Runnable{

private Panel p1;

private add方块 f;

Thread settime;

private int 速度=300;

public int n[]={13,0};

public boolean 墙的数据[][]=new boolean[46][30];

private boolean a1[][]={{false,true,true,false},{false,true,false,false},{false,true,false,false},{false,false,false,false}};

private boolean a2[][]={{false,true,false,false},{false,true,false,false},{false,true,false,false},{false,true,false,false}};

private boolean a3[][]={{false,false,false,false},{false,true,true,false},{false,true,true,false},{false,false,false,false}};

private boolean a4[][]={{false,true,false,false},{false,true,true,false},{false,true,false,false},{false,false,false,false}};

public boolean a[][]=a4;

public void run(){

while(true){

try{

settime.sleep(速度);

}catch(InterruptedException e){}

if(测试碰撞()){

for(int i=0;i<4;i++){

for(int p=0;p<4;p++){

if(a[i][p]){墙的数据[i+n[1]][p+n[0]]=true;}

}

}

n[0]=13;

n[1]=-1;

int x=(int) (Math.random()*4);

if(x==0){a=a1;}else if(x==1){a=a2;}else if(x==2){a=a3;}else{a=a4;}

}

n[1]++;

f.repaint();

}

}

private boolean 测试碰撞(){

for(int i=0;i<4;i++){

for(int p=0;p<4;p++){

if(a[i][p]==true && 墙的数据[i+n[1]+1][p+n[0]]==true){return true;}

}

}

return false;

}

public void init(){

for(int i=0;i<46;i++){

for(int o=0;o<30;o++){

墙的数据[i][o]=false;

}

}

for(int o=0;o<30;o++){

墙的数据[44][o]=true;

}

setBackground(new Color(0x88000000,true));

setLayout(null);

p1=new Panel(null);

p1.setBackground(new Color(0xffffff));

p1.setBounds(20,20,301,451);

add(p1);

f=new add方块(this);

p1.add(f);

f.setLocation(0,0);

Button b=new Button("开始");

add(b);

b.setBounds(350,20,100,25);

b.addKeyListener(this);

settime=new Thread(this);

settime.start();

}

private  boolean[][] 转动(){

boolean ar[][]=new boolean[4][4];

for(int o=0;o<4;o++){

for(int i=3;i>=0;i--){

ar[o][3-i]=a[i][o];

}

}

return ar;

}

private void 左边界(){

if(n[0]<0){

int i=-n[0]-1;

for(int p=0;p<4;p++){

if(a[p][i]){

n[0]++;

return;

}

}

}

}

private void 右边界(){

if(n[0]>26){

int i=4-(n[0]-26);

for(int p=0;p<4;p++){

if(a[p][i]){

n[0]--;

return;

}

}

}

}

private boolean 左碰撞(){

for(int i=0;i<4;i++){

for(int p=0;p<4;p++){

if(p+n[0]-1>=0){

if(a[i][p]==true && 墙的数据[i+n[1]][p+n[0]-1]==true){return false;}

}

}

}

return true;

}

private boolean 右碰撞(){

for(int i=0;i<4;i++){

for(int p=0;p<4;p++){

if(p+n[0]-1<=30){

if(a[i][p]==true && 墙的数据[i+n[1]][p+n[0]+1]==true){return false;}

}

}

}

return true;

}

public void keyPressed(KeyEvent e){

if(e.getKeyCode()==KeyEvent.VK_UP){

for(int i=0;i<3;i++){a=转动();}

for(int i=0;i<2;i++){

左边界();

右边界();

}

}else if(e.getKeyCode()==KeyEvent.VK_LEFT){

if(左碰撞()){n[0]--;}

左边界();

}else if(e.getKeyCode()==KeyEvent.VK_RIGHT){

n[0]++;

右边界();

}else if(e.getKeyCode()==KeyEvent.VK_DOWN){

速度=30;

}

f.repaint();

}

public void keyReleased(KeyEvent e){

if(e.getKeyCode()==KeyEvent.VK_DOWN){

速度=300;

}

}

public void keyTyped(KeyEvent e){}

}

[本帖最后由 虾B写 于 2010-6-30 17:33 编辑]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java实现图片透明效果,从演示截图可看到,上层图片与下层图片互相重叠,也就是上层图片透明了,看到了下层以的图片,具体的代码实现:   public class HalfTransparentImageDemo extends JFrame {    private Container content = getContentPane(); //获得窗口的容器    private JSlider jSlider = new JSlider(JSlider.HORIZONTAL,0,100,75); //改变图像的透明度    DisplayPanel displayPanel = new DisplayPanel(); //显示图形面板    public HalfTransparentImageDemo() {    super("半透明图片"); //调用父类构造器    jSlider.setPaintTicks(true); //绘制标志位    jSlider.setMajorTickSpacing(25); //设置标志尺寸    jSlider.setMinorTickSpacing(5);    jSlider.setPaintLabels(true); //绘制出数字    jSlider.setBorder(new TitledBorder(BorderFactory.createEmptyBorder(), "图像的透明度(%)")); //设置边框    jSlider.addChangeListener(new ChangeListener() { //滑动条jSlider事件处理    public void stateChanged(ChangeEvent ce) {    float alpha =((float) ((JSlider) ce.getSource()).getValue()) / 100;    displayPanel.alpha = alpha; //改变图像的透明度    displayPanel.repaint(); //重绘displayPanel    }    g2d.drawImage(bufImage1, 0, 0, this); //在图形环境绘制缓冲区图像1    g2d.drawString("Destination",5,20); //绘制文字    int compositeRule = AlphaComposite.SRC_OVER; //源排斥目标法合成规则    AlphaComposite alphaComposite=AlphaComposite.getInstance(compositeRule,alpha); //创建AlphaComposite对象    g2d.setComposite(alphaComposite); //设置图形环境的合成方式    g2d.drawImage(bufImage2, 0, 0, this); //在图形环境绘制缓冲区图像2    g2d.drawString("Source",150,20); //绘制文字    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值