如何从我的主类向画布绘制一个矩形?

huangapple go评论73阅读模式
英文:

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&lt;Shape&gt; shapes;
    public MyCanvas (){
        shapes = new ArrayList();
    }

    public List&lt;Shape&gt; 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();

}

huangapple
  • 本文由 发表于 2020年10月17日 06:18:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/64397100.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定