| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 628 人关注过本帖
标题:弱弱的问问......
只看楼主 加入收藏
karenkaren
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-6-1
结帖率:0
收藏
已结贴  问题点数:20 回复次数:3 
弱弱的问问......
老师要我们自学绘图组件.....就是要做一个会动的小球.....

但我希望小球能来回滚动,或Z字型滚动.........怎么改都不行呢.....
还有能不能在滚动的过程中变色呢?
这是小球会往下掉的代码,高手看看怎么改下,能来回动呢?感激感激~~
package cha7;
import javax.swing.*;
import java.awt.*;
public class Canvas {
    private JFrame frame;
    private JPanel canvas;
    private Graphics2D graphic;
    private Color backgroundColour;
    private Image canvasImage;
    Canvas(String title,int width,int height,Color bgColour)
    {frame=new JFrame();
    canvas =new JPanel();
    frame.setTitle(title);
    canvas.setPreferredSize(new Dimension(width,height));
    backgroundColour=bgColour;
    frame.pack();
    }
    Canvas()
    {
        frame=new JFrame();
        canvas=new CanvasPane();
        frame.setContentPane(canvas);
        frame.setTitle("Demo Canvas");
        canvas.setPreferredSize(new Dimension(600,600));
        backgroundColour=Color.blue;
        frame.pack();
        setVisible(true);
    }
    public void setVisible(boolean visible)
    {
        if(graphic==null){
            Dimension size=canvas.getSize();
            canvasImage=canvas.createImage(size.width,size.height);
            
            graphic=(Graphics2D)canvasImage.getGraphics();
            graphic.setColor(backgroundColour);
            graphic.fillRect(0, 0,size.width,size.height);
            graphic.setColor(Color.white);
        }
        frame.setVisible(visible);
    }
    public void draw(Shape shape)
    {
        graphic.draw(shape);
        canvas.repaint();
    }
    public void fill(Shape shape)
    {
        graphic.fill(shape);
        canvas.repaint();
    }
    public void erase(Shape shape)
    {
        Color original=graphic.getColor();
        graphic.setColor(backgroundColour);
        graphic.fill(shape);
        graphic.setColor(original);
        canvas.repaint();
    }
   
    private class CanvasPane extends JPanel
    {
        public void paint(Graphics g)
        {
            g.drawImage(canvasImage,0,0,null);
        }
   
        }
}

package cha7;
import java.awt.*;
import java.awt.geom.*;

public class Circles {
    private int diameter;
    private int xPosition;
    private int yPosition;
    private String color;
    private Canvas canvas;
    public Circles()
    {
        diameter=10;
        xPosition=20;
        yPosition=60;
        color="blue";
        canvas=new Canvas();
        draw();
    }
        public void moveDown()
        {
            erase();
        yPosition +=10;
        xPosition +=2;
        draw();
         }
        
    private void draw()
    {
        canvas.fill(new Ellipse2D.Double(xPosition,yPosition,diameter,diameter));
    }
    public void erase()
    {canvas.erase(new Ellipse2D.Double(xPosition,yPosition,diameter,diameter));
    }
    public static void main(String args[])throws Exception{
        Circles c=new Circles();
        long i=40,j=10,k=0;
        while (i>0){
            c.moveDown();
            for(j=10;j<9000000;j++){k++;}
            i=i-1;
            
        }
        c.erase();
        c.canvas.fill(new Ellipse2D.Double(20,60,10,10));

        
    }

}

搜索更多相关主题的帖子: 变色 希望 graphic private package 
2010-06-01 17:55
linjx0123
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:279
专家分:1362
注 册:2006-4-7
收藏
得分:20 
不知道楼主需要什么样的效果,在你的代码基础稍微改了一下。
程序代码:
mport java.awt.Color;
import java.awt.geom.Ellipse2D;

public class Circles {
    private int diameter;
    private int xPosition;
    private int yPosition;
    private String color;
    private static Canvas canvas;

    public Circles() {
        diameter = 10;
        xPosition = 20;
        yPosition = 60;
        color = "blue";
        canvas = new Canvas();
        draw();
    }
    boolean yDirection = true;
    boolean xDirection = true;
    private static int MAX_Y =400;
    private static int MAX_X =400;
    public void moveDown() {
        erase();
        if(yPosition>MAX_Y)
            yDirection=false;
        if(yPosition<0)
            yDirection=true;
        if(yDirection)
            yPosition+=10;
        else
            yPosition-=10;
       
        if(xPosition>MAX_X)
            xDirection=false;
        if(xPosition<0)
            xDirection=true;
        if(xDirection)
            xPosition+=2;
        else
            xPosition-=2;
        draw();
    }

    private void draw(){
        canvas.fill(new Ellipse2D.Double(xPosition,yPosition,diameter,diameter));
    }

    public void erase() {
        canvas.erase(new Ellipse2D.Double(xPosition, yPosition, diameter, diameter));
    }

    public static void main(String args[]) throws Exception {
        Circles c = new Circles();
        long i = 400, j = 10, k = 0;
        while (i > 0) {
            if(i%50==0)
                canvas.color=Color.red;
            else if(i%50==25)
                canvas.color=Color.black;
            c.moveDown();
            for (j = 10; j < 9000000; j++) {
                k++;
            }
            i = i - 1;

        }
        c.erase();
        c.canvas.fill(new Ellipse2D.Double(20, 60, 10, 10));

    }

}







import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Shape;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Canvas {
    private JFrame frame;
    private JPanel canvas;
    private Graphics2D graphic;
    private Color backgroundColour;
    private Image canvasImage;

    Canvas(String title, int width, int height, Color bgColour) {
        frame = new JFrame();
        canvas = new JPanel();
        frame.setTitle(title);
        canvas.setPreferredSize(new Dimension(width, height));
        backgroundColour = bgColour;
        frame.pack();
    }

    Canvas() {
        frame = new JFrame();
        canvas = new CanvasPane();
        frame.setContentPane(canvas);
        frame.setTitle("Demo Canvas");
        canvas.setPreferredSize(new Dimension(600, 600));
        backgroundColour = Color.blue;
        frame.pack();
        setVisible(true);
    }
   
    public Color color = backgroundColour;

    public void setVisible(boolean visible) {
        if (graphic == null) {
            Dimension size = canvas.getSize();
            canvasImage = canvas.createImage(size.width, size.height);

            graphic = (Graphics2D) canvasImage.getGraphics();
            graphic.setColor(backgroundColour);
            graphic.fillRect(0, 0, size.width, size.height);
            graphic.setColor(Color.white);
        }
        frame.setVisible(visible);
    }

    public void draw(Shape shape) {
        graphic.draw(shape);
        canvas.repaint();
    }

    public void fill(Shape shape) {
        graphic.setColor(color);
        graphic.fill(shape);
        canvas.repaint();
    }

    public void erase(Shape shape) {
        Color original = graphic.getColor();
        graphic.setColor(backgroundColour);
        graphic.fill(shape);
        graphic.setColor(original);
        canvas.repaint();
    }

    private class CanvasPane extends JPanel {
        public void paint(Graphics g) {
            g.drawImage(canvasImage, 0, 0, null);
        }

    }
}



 
2010-06-02 09:32
karenkaren
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-6-1
收藏
得分:0 
太谢谢了~~我大概就是这个意思。太有些地方看不懂。书上好像没讲到这些。
比如说“import java.awt.geom.Ellipse2D;”。这个导入是用于??

还想问下,如果我还想这个画板中有其他一些图形比如说是圆,三角形。不需要动。直接在这个类中打上我要的东西就行了还是要另建一个类呢?
2010-06-02 16:32
linjx0123
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:279
专家分:1362
注 册:2006-4-7
收藏
得分:0 
回复 3楼 karenkaren
恩,你要画其他图形,可以直接在这个类中画的,不需要新建类。
遇到不懂的类,可以直接去查看java的中文api,会有介绍的。以下是api中的介绍

public abstract class Ellipse2Dextends RectangularShapeEllipse2D 类描述窗体矩形定义的椭圆。
此类是所有存储 2D 椭圆的对象的惟一抽象超类。坐标的实际存储表示形式由子类决定。

public static class Ellipse2D.Doubleextends Ellipse2Dimplements SerializableDouble 类以 double 精度定义椭圆。
Ellipse2D.Double
public Ellipse2D.Double(double x,
                        double y,
                        double w,
                        double h)根据指定坐标构造和初始化 Ellipse2D。

参数:
x - 窗体矩形左上角的 X 坐标
y - 窗体矩形左上角的 Y 坐标
w - 窗体矩形的宽度
h - 窗体矩形的高度



2010-06-03 08:53
快速回复:弱弱的问问......
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.048827 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved