【问题标题】:Painting to screen a parameter object's location绘画以屏蔽参数对象的位置
【发布时间】:2010-12-07 05:13:20
【问题描述】:

我们正在尝试在世界地图的背景图像上的不同 x-y 坐标位置上绘制圆形对象。我们有一个名为 Territory 的类,它接受 x 和 y 坐标的两个整数。

Territory(String name, int x , int y)...

我们还有一个面板类(扩展 JPanel),它使用重写的 paintComponent 方法将背景图像绘制到 JPanel 上。但是,我们无法找到一种方法来从另一个类传递坐标值和其他属性而不破坏覆盖,或者是否有另一种方法可以在与背景图像相同的面板上进行绘制。

这是我们在面板类中调用的paintComponent 方法的代码。

 protected void paintComponent(Graphics g){
  super.paintComponent(g);
         if (image != null)
 g.drawImage(image, 0,0,this.getWidth(),this.getHeight(),this);

【问题讨论】:

    标签: java swing


    【解决方案1】:

    自定义绘画都应该在同一个 paintComponent() 方法中完成。所以基本上绘制背景的面板也应该有一个像 addCircleToPaint() 这样的方法。圆圈信息应存储在 ArrayList (或类似的东西)中。然后在 paintComponent 方法中绘制图像,然后循环遍历 ArrayList 并绘制圆圈。

    或者您可以使用完全不同的方法并使用Layered Panes。使用一层作为背景,一层作为圆形。

    【讨论】:

      【解决方案2】:

      下面的代码将产生这个图像:

      请注意,如果您想绘制真实的领土,我会使用 Polygon 类。

      public class Test {
      
          static class Territory {
              String name;
              int x, y;
      
              public Territory(String name, int x, int y) {
                  this.name = name;
                  this.x = x;
                  this.y = y;
              }
      
              public void paint(Graphics g) {
                  g.drawString(name, x, y);
                  g.drawOval(x - 50, y - 50, 101, 101);
              }
          }
      
          static class Map extends JComponent {
      
           Image bgImage;
              List<Territory> territories;
      
              public Map(List<Territory> territories) throws Exception {
                  this.bgImage = ImageIO.read(new URL("http://upload.wikimedia.org/wikipedia/commons/thumb/d/d8/Winkel-tripel-projection.jpg/800px-Winkel-tripel-projection.jpg"));
                  this.territories = territories;
              }
      
              @Override
              protected void paintComponent(Graphics g) {
                  super.paintComponent(g);
      
                  g.drawImage(bgImage, 0, 0, null);
      
                  g.setColor(Color.RED);
                  for (Territory territory : territories)
                      territory.paint(g);
              }
          }
      
          public static void main(String... args) throws Exception {
      
              JFrame frame = new JFrame("Test");
      
              final int w = 800;
              final int h = 400;
      
              Random r = new Random(8208);
              List<Territory> territories = new LinkedList<Territory>();
              for (int i = 0; i < 10; i++)
                  territories.add(new Territory("" + i, r.nextInt(w), r.nextInt(h)));
      
              frame.add(new Map(territories));
      
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.setSize(w, h);
              frame.setVisible(true);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-04-27
        • 2022-12-10
        • 1970-01-01
        • 2015-10-04
        • 1970-01-01
        • 2018-10-10
        • 1970-01-01
        相关资源
        最近更新 更多