- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个小程序,就像现在写的那样,在按下“运行”按钮后,它应该在顶部(代码中的 drawingpanel
)绘制和弹跳一个球。在线查看其他示例并使用来自未使用 Canvas 的其他程序的代码,我无法弄清楚为什么我的 Ball 对象没有显示。我已经测试了程序的流程,并且所有方法都按应有的方式调用,因此代码没有到达paint
的情况并非如此。方法。任何关于使用 Canvas/Thread 的好资源都会很棒,或者对以下小程序的任何建议都会非常有用。谢谢!
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Bounce2 extends Applet implements ActionListener, AdjustmentListener, Runnable
{
private final static long serialVersionUID = 1L;
//runtime variables
boolean running = false;
boolean currentlyCircle = true;
boolean showtails = false;
boolean kill = false;
//buttons
Button runbutton = new Button("Run");
Button pausebutton = new Button("Pause");
Button quitbutton = new Button("Quit");
//text
Label speedlabel = new Label("Speed");
Label sizelabel = new Label("Size");
//scrollbars
private final int barHeight = 20;
private final int SLIDER_WIDTH = 10;
private final int MAXSPEED = 110;
private final int MINSPEED = 0;
private final int MAX_SIZE = 110;
private final int MIN_SIZE = 10;
Scrollbar speedbar = new Scrollbar(Scrollbar.HORIZONTAL, MAXSPEED/2, SLIDER_WIDTH, MINSPEED, MAXSPEED);
Scrollbar sizebar = new Scrollbar(Scrollbar.HORIZONTAL, MAX_SIZE/2, SLIDER_WIDTH, MIN_SIZE, MAX_SIZE);
//drawn objs
Ball ball;
Image buffer;
int size = 50;
private Graphics obj;
Point currentlocation = new Point(100,100);
Point previouslocation;
Point nextlocation;
Rectangle circle;
Rectangle screen;
private Thread ballThread;
//boundaries
int bound_x;
int bound_y;
//directions
int dx = 1; //1 = left, -1 = right
int dy = 1; //1 = up, -1 = down
//speed
int speed = speedbar.getValue();
int delay;
//initialize the applet and draw everything
public void init()
{
double colWeight[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};//15 cols
double rowWeight[] = {1,1,1,1,1,1,1,1,1,1}; //10 rows
int colWidth[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};//15 cols
int rowHeight[] = {1,1,1,1,1,1,1,1,1,1}; //10 rows
GridBagConstraints c = new GridBagConstraints();
GridBagLayout gbl = new GridBagLayout();
gbl.rowHeights = rowHeight;
gbl.rowWeights = rowWeight;
gbl.columnWeights = colWeight;
gbl.columnWidths = colWidth;
c.anchor = GridBagConstraints.CENTER;
setBounds(0,0,480,640);
setLayout(new BorderLayout());
Panel controlpanel = new Panel();
controlpanel.setLayout(gbl);
controlpanel.setSize(640,80);
Panel drawingpanel = new Panel(null);
drawingpanel.setSize(640,400);
ball = new Ball();
drawingpanel.add("Center", ball);
Rectangle circle = new Rectangle(size, size);
Rectangle screen = new Rectangle(0,0,640,400);
drawingpanel.setVisible(true);
//speed scrollbar
c.weightx = 1;
c.weighty = 1;
c.gridwidth = 3;
c.gridheight = 1;
c.gridx = 1;
c.gridy = 7;
c.fill= GridBagConstraints.HORIZONTAL;
gbl.setConstraints(this.speedbar,c);
//run button
c.weightx = 1;
c.weighty = 1;
c.gridwidth = 2;
c.gridheight = 1;
c.gridx = 5;
c.gridy = 7;
c.fill= GridBagConstraints.HORIZONTAL;
gbl.setConstraints(this.runbutton,c);
//pause button
c.weightx = 1;
c.weighty = 1;
c.gridwidth = 2;
c.gridheight = 1;
c.gridx = 8;
c.gridy = 7;
c.fill= GridBagConstraints.HORIZONTAL;
gbl.setConstraints(this.pausebutton,c);
//size scrollbar
c.weightx = 1;
c.weighty = 1;
c.gridwidth = 3;
c.gridheight = 1;
c.gridx = 11;
c.gridy = 7;
c.fill= GridBagConstraints.HORIZONTAL;
gbl.setConstraints(this.sizebar,c);
//speed text label
c.weightx = 1;
c.weighty = 1;
c.gridwidth = 3;
c.gridheight = 1;
c.gridx = 1;
c.gridy = 8;
c.fill= GridBagConstraints.HORIZONTAL;
gbl.setConstraints(this.speedlabel,c);
//size text label
c.weightx = 1;
c.weighty = 1;
c.gridwidth = 3;
c.gridheight = 1;
c.gridx = 11;
c.gridy = 8;
c.fill= GridBagConstraints.HORIZONTAL;
gbl.setConstraints(this.sizelabel,c);
//quit button
c.weightx = 1;
c.weighty = 1;
c.gridwidth = 3;
c.gridheight = 1;
c.gridx = 6;
c.gridy = 9;
c.fill= GridBagConstraints.HORIZONTAL;
gbl.setConstraints(this.quitbutton,c);
//add to the screen
controlpanel.add(this.speedbar);
controlpanel.add(this.runbutton);
controlpanel.add(this.pausebutton);
controlpanel.add(this.sizebar);
controlpanel.add(this.speedlabel);
controlpanel.add(this.sizelabel);
controlpanel.add(this.quitbutton);
//add listners
speedbar.addAdjustmentListener(this);
runbutton.addActionListener(this);
pausebutton.addActionListener(this);
sizebar.addAdjustmentListener(this);
quitbutton.addActionListener(this);
//add the panels
add("South", controlpanel);
add("Center", drawingpanel);
//drawing paramaters, draw the first object
System.err.println(obj);
obj = drawingpanel.getGraphics();
nextlocation = new Point(currentlocation.x+dx, currentlocation.y+dy);
setVisible(true);
validate();
}
public void start()
{
if (ballThread == null)
{
ballThread = new Thread(this);
ballThread.start();
repaint();
}
}
public void run()
{
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
while (!kill)
{
if (running)
{
ball.move();
repaint();
}
try
{
Thread.sleep(delay);
}
catch(InterruptedException e){System.err.println("Interrupted.");}
}
stop();
}
public void update(Graphics g)
{
Graphics buffer;
Image offscreen = null;
offscreen = createImage(bound_x, bound_y);
buffer = offscreen.getGraphics();
buffer.setColor(getBackground());
buffer.fillRect(0,0,bound_x, bound_y);
//update
previouslocation = new Point(currentlocation);
currentlocation = nextlocation;
//draw
buffer.setColor(Color.black);
buffer.drawOval(nextlocation.x, nextlocation.y, size, size);
buffer.fillOval(nextlocation.x, nextlocation.y, size, size);
//draw rectangles out of vector
g.drawImage(offscreen, 0,0, null);
paint(buffer);
}
//class to handle animations
class Ball extends Canvas
{
public void move()
{
nextlocation = new Point(currentlocation.x+dx, currentlocation.y+dy);
//if it will hit the right or left, flip the x direction and set it
if (nextlocation.x+size >= bound_x || nextlocation.x <= 0)
{ dx *= -1; }
nextlocation.x += dx;
//if it will hit the top or bottom, flip the y direction and set it
if (nextlocation.y+size >= bound_y + 100 || nextlocation.y <= 0)
{ dy *= -1; }
nextlocation.y += dy;
setBounds(dx,dy,size,size);
System.out.println(dx + "," + dy);
}
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.black);
g.drawOval(0, 0, size, size);
}
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if (source == this.runbutton)
{
running = true;
}
else if (source == this.pausebutton)
{
running = false;
}
else if (source == this.quitbutton)
{
//kill processes
kill = true;
//remove listeners
stop();
}
}
public void adjustmentValueChanged(AdjustmentEvent e)
{
Object source = e.getSource();
//set the new size.
if (source == sizebar)
{
//check for clipping
int newsize = sizebar.getValue();
// x
if (currentlocation.x+newsize >= bound_x)
{
newsize = bound_x - currentlocation.x - 1;
sizebar.setValue(newsize);
}
// y
if (currentlocation.y+newsize >= bound_y + 100)
{
newsize = bound_y+100 - currentlocation.y - 1;
sizebar.setValue(newsize);
}
size = newsize;
}
if (source == speedbar)
{
speed = speedbar.getValue();
delay = MAXSPEED - speedbar.getValue();
}
}
public void stop()
{
this.speedbar.removeAdjustmentListener(this);
this.runbutton.removeActionListener(this);
this.pausebutton.removeActionListener(this);
this.sizebar.removeAdjustmentListener(this);
this.quitbutton.removeActionListener(this);
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
}
}
最佳答案
老实说,我不知道从哪里开始。
这是一个坏主意。
private Graphics obj;
.
.
.
obj = drawingpanel.getGraphics();
paint
方法
public void paint(Graphics obj)
{
// I can see you're trying to double buffer the graphics, but I would have a
// buffer already set up...
buffer = createImage(640,400);
// You should NEVER dispose of Graphics context you didn't created...
if (obj != null)
obj.dispose();
// Now we're really stuffed. You've just overridden the screen graphics context
obj = buffer.getGraphics();
obj.setColor(getBackground());
//update
previouslocation = new Point(currentlocation);
currentlocation = nextlocation;
//draw
obj.fillRect(currentlocation.x, currentlocation.y, size, size);
obj.setColor(Color.black);
obj.drawOval(nextlocation.x, nextlocation.y, size, size);
obj.fillOval(nextlocation.x, nextlocation.y, size, size);
/*
*draw rectangles out of vector
*/
// Now you drawing the buffer onto itself...???
obj.drawImage(buffer, 0,0, null);
}
Ball
组件到您的主要绘画区域,并且您正在使用
null
此油漆区域的布局。
Ball
...所以在您的
Ball
类,您需要覆盖
paint
方法...
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.black);
g.drawOval(0, 0, size, size);
}
Ball#move
方法,你需要更新球的位置...
public void move() {
// You've previous move code
setBounds(dx, dy, size, size);
}
关于java - 在线程中使用 Canvas 对象做简单的动画 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13132491/
我正在努力实现以下目标, 假设我有字符串: ( z ) ( A ( z ) ( A ( z ) ( A ( z ) ( A ( z ) ( A ) ) ) ) ) 我想编写一个正则
给定: 1 2 3 4 5 6
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
大家好,我卡颂。 Svelte问世很久了,一直想写一篇好懂的原理分析文章,拖了这么久终于写了。 本文会围绕一张流程图和两个Demo讲解,正确的食用方式是用电脑打开本文,跟着流程图、Demo一
身份证为15位或者18位,15位的全为数字,18位的前17位为数字,最后一位为数字或者大写字母”X“。 与之匹配的正则表达式: ?
我们先来最简单的,网页的登录窗口; 不过开始之前,大家先下载jquery的插件 本人习惯用了vs2008来做网页了,先添加一个空白页 这是最简单的的做法。。。先在body里面插入 <
1、MySQL自带的压力测试工具 Mysqlslap mysqlslap是mysql自带的基准测试工具,该工具查询数据,语法简单,灵活容易使用.该工具可以模拟多个客户端同时并发的向服务器发出
前言 今天大姚给大家分享一款.NET开源(MIT License)、免费、简单、实用的数据库文档(字典)生成工具,该工具支持CHM、Word、Excel、PDF、Html、XML、Markdown等
Go语言语法类似于C语言,因此熟悉C语言及其派生语言( C++、 C#、Objective-C 等)的人都会迅速熟悉这门语言。 C语言的有些语法会让代码可读性降低甚至发生歧义。Go语言在C语言的
我正在使用快速将 mkv 转换为 mp4 ffmpeg 命令 ffmpeg -i test.mkv -vcodec copy -acodec copy new.mp4 但不适用于任何 mkv 文件,当
我想计算我的工作簿中的工作表数量,然后从总数中减去特定的工作表。我错过了什么?这给了我一个对象错误: wsCount = ThisWorkbook.Sheets.Count - ThisWorkboo
我有一个 perl 文件,用于查看文件夹中是否存在 ini。如果是,它会从中读取,如果不是,它会根据我为它制作的模板创建一个。 我在 ini 部分使用 Config::Simple。 我的问题是,如果
尝试让一个 ViewController 通过标准 Cocoa 通知与另一个 ViewController 进行通信。 编写了一个简单的测试用例。在我最初的 VC 中,我将以下内容添加到 viewDi
我正在绘制高程剖面图,显示沿路径的高程增益/损失,类似于下面的: Sample Elevation Profile with hand-placed labels http://img38.image
嗨,所以我需要做的是最终让 regStart 和 regPage 根据点击事件交替可见性,我不太担心编写 JavaScript 函数,但我根本无法让我的 regPage 首先隐藏。这是我的代码。请简单
我有一个非常简单的程序来测量一个函数花费了多少时间。 #include #include #include struct Foo { void addSample(uint64_t s)
我需要为 JavaScript 制作简单的 C# BitConverter。我做了一个简单的BitConverter class BitConverter{ constructor(){} GetBy
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我是 Simple.Data 的新手。但我很难找到如何进行“分组依据”。 我想要的是非常基本的。 表格看起来像: +________+ | cards | +________+ | id |
我现在正在开发一个 JS UDF,它看起来遵循编码。 通常情况下,由于循环计数为 2,Alert Msg 会出现两次。我想要的是即使循环计数为 3,Alert Msg 也只会出现一次。任何想法都
我是一名优秀的程序员,十分优秀!