英文:
How can I draw a rectangle to my canvas from my main class?
问题
我知道我可以使用bufferstrategy(在循环内部)来实现,但是否有一种在没有bufferstrategy的情况下(在循环内部)实现的方法呢?
import javax.swing.JFrame;
public class JavaApplication28 {
public static void main(String[] args) {
JFrame f = new JFrame("Title");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyCanvas mycanvas = new MyCanvas();
f.add(mycanvas);
f.setSize(400,250);
f.setVisible(true);
//我希望在这里在画布上创建矩形,可以通过bufferstrategy实现
}
}
import java.awt.Canvas;
import java.awt.Graphics;
public class MyCanvas extends Canvas {
public MyCanvas() {
}
//我是否需要在我的主类中调用paint(),类似于mycanvas.paint().drawRect(100, 100, 100, 100)?是否有这样的语法?
public void paint(Graphics g) {
super.paint(g);
g.drawRect(100, 100, 100, 100);
}
}
英文:
I know I can do it with a bufferstrategy (inside a loop), but is there a way to do it without a bufferstrategy (inside a loop)?
import javax.swing.JFrame;
public class JavaApplication28 {
public static void main(String[] args) {
JFrame f = new JFrame("Title");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyCanvas mycanvas = new MyCanvas();
f.add(mycanvas);
f.setSize(400,250);
f.setVisible(true);
//I would like to create the rectangle on the canvas here, which is possible with bufferstrategy
}
}
import java.awt.Canvas;
import java.awt.Graphics;
public class MyCanvas extends Canvas{
public MyCanvas (){
}
//Do I call paint() in my main class, something like mycanvas.paint().drawRect(100, 100, 100, 100)? Is there such syntax?
public void paint(Graphics g) {
super.paint(g);
g.drawRect(100, 100, 100, 100);
}
}
</details>
# 答案1
**得分**: 2
```java
我不记得 AWT 的画布是否有 repaint() 方法了……已经有20年没用过它们了 :)
在画布中持有一个“已渲染”对象的集合,根据需要进行添加/移除并进行重绘。
public class MyCanvas extends Canvas{
private final List<Shape> shapes;
public MyCanvas (){
shapes = new ArrayList();
}
public List<Shape> renderedShapes() {
return shapes;
}
public void paint(Graphics g) {
super.paint(g);
// 图形是在主类中创建的,但一旦将它们添加到此画布的 renderedShapes() 数组中,
// 您就可以在此时访问它们和图形上下文,并对其进行渲染。
for (Shape s : shapes) {
renderShape(g, s);
}
}
}
private void renderShape(Graphics g, Shape shape) {
if (shape instanceof Rectangle) {
Rectangle rect = (Rectangle)shape;
g.drawRect(rect.x, rect.y, rect.width, rect.height);
} else if (shape instanceof Circle) {
Circle circle = (Circle)shape;
g.drawCircle(circle.x, circle.y, circle.radius);
}
}
public static void main(String[] args) {
MyCanvas canvas = new MyCanvas();
canvas.renderedShapes().add(new Rectangle(0,0,10,10));
canvas.repaint();
}
英文:
I don't remember if AWT canvases have repaint()... haven't used them in 20 years
Hold a collection of "rendered" objects in the canvas and just add/remove and repaint as you see fit.
public class MyCanvas extends Canvas{
private final List<Shape> shapes;
public MyCanvas (){
shapes = new ArrayList();
}
public List<Shape> renderedShapes() {
return shapes;
}
public void paint(Graphics g) {
super.paint(g);
// the shapes are created in the main class but once you add them to the renderedShapes() array of this canvas, you have access to them and the graphics context at this point and can render them.
for (Shape s : shapes) {
renderShape(g, s);
}
}
}
private void renderShape(Graphics g, Shape shape) {
if (Shape instanceof Rectangle) {
Rectangle rect = (Rectangle)shape;
g.drawRect(rect.x, rect.y, rect.width, rect.height);
} else if (Shape instanceof Circle) {
Circle circle = (Circle)shape;
g.drawCircle(circle.x, circle.y, circle.radius);
}
}
public static void main(String[] args) {
MyCanvas canvas = new MyCanvas();
canvas.renderedShapes().add(new Rectangle(0,0,10,10));
canvas.repaint();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论