【问题标题】:problem getting the grid right and no background Color让网格正确且没有背景颜色的问题
【发布时间】:2011-06-21 10:32:12
【问题描述】:

这是我尝试制作 400 x 400 网格但输出以下内容的代码:

毫无疑问,这不是 400 x 400 网格。

我无法检测出我的逻辑在哪里出错。负责绘图的部分我已经高亮了。

import javax.swing.*;
import java.awt.*;

class tester1 extends JPanel {
 tester1() {
  setPreferredSize(new Dimension(400,400));
  this.setBackground(Color.black);
  JFrame fr = new JFrame();
  fr.add(this);
  fr.pack();
  fr.setVisible(true);
  fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  repaint();
 }

  public void paintComponent(Graphics g) {   // <----- part responsible for drawing
   g.setColor(Color.red); 
     for(int x = 0 ; x <= 400 ; x += 10 ) {
       g.drawLine( x , 0 , 400 , x );
     }
     for(int y = 0 ; y <= 400 ; y += 10 ) {
       g.drawLine( y , 0 , y , 400 );
     }
  }   // <---- till here

  public static void main(String args[]) {
     new tester1();
  }
 }

我在paintComponent方法中犯了什么错误?

在输出中,当我写了this.setBackground(Color.black) 时,为什么我没有得到 BLACK 颜色作为背景?

【问题讨论】:

  • +1 用于提供代码和(裁剪良好,甚至)屏幕截图。顺便说一句 - 对于屏幕截图,我将它们放在白色网页上(在角落有白色 BG),然后在按下“打印屏幕”按钮的同时按住“alt”。 'alt' 键可确保仅捕获聚焦的组件,并且圆角中的白色 BG 与任何使用白色 BG 的站点都很好地保持一致。

标签: java swing graphics 2d


【解决方案1】:

在进行任何绘画之前请致电super.paintComponent(),以便将背景涂成黑色。

就线路而言: 您的 x 和 y 协调混合在一起。应该是:(int x1, int y1, int x2, int y2)

具体看:

 g.drawLine( x , 0 , 400 , x );

问问自己,为什么要改变一个 X 和一个 Y,而保持另一个 X 和另一个 Y 不变。

在哪里:

 g.drawLine( y , 0 , y , 400 );

您同时保持 Y 不变并更改两个 X(提示:您的其他语句应该看起来像这样。)。

【讨论】:

  • 我的坐标怎么混在一起了?是的,虽然我的背景是黑色的,但是super.paintComponent(g) 是做什么的?
  • 了解 super 的作用,这对理解 Java 非常重要。就坐标而言,将它们写在纸上或执行 System.out.printlns 并考虑您希望它做什么与它现在正在做什么(查看生成的坐标。
  • 你能解释一下paintComponent()是做什么的吗
  • paintComponent,在 Graphics 对象上绘制组件。您的代码在方法旁边有一条注释,上面写着“负责绘图”。
【解决方案2】:

我认为您当前的输出很漂亮,但无论如何..

import javax.swing.*;
import java.awt.*;

class tester1 extends JPanel {
 tester1() {
  setPreferredSize(new Dimension(400,400));
  this.setBackground(Color.black);
  JFrame fr = new JFrame();
  fr.add(this);
  fr.pack();
  fr.setVisible(true);
  fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  repaint();
 }

  public void paintComponent(Graphics g) {
   // as mentioned by jzd
   super.paintComponent(g);
   g.setColor(Color.red);
     for(int x = 0 ; x <= getWidth() ; x += 10 ) {
       // as mentioned by ditkin
       g.drawLine( x , 0 , x , getHeight() );
     }
   g.setColor(Color.yellow);
     for(int y = 0 ; y <= getHeight() ; y += 10 ) {
       // also this
       g.drawLine( 0 , y , getWidth() , y );
     }
  }

  public static void main(String args[]) {
     new tester1();
  }
 }

顺便说一句 - 除非您在框架上调用 setResizable(false),否则在绘画时使用 getHeight() / getWidth()

截图

【讨论】:

    【解决方案3】:

    我相信这条线:

    g.drawLine( x , 0 , 400 , x );
    

    应阅读:

    g.drawLine( x , 0 , x, 400);
    

    【讨论】:

    • -1 :那么下一个for语句有什么区别呢?
    • @Meprogrammer:我没有得到这个-1,是因为ditkin 未能检测到代码中的every 错误吗? (如果是的话)从什么时候开始这是回答的先决条件?
    • @Andrew,我认为主要问题不在于这只是一个错误,而是仅更改这一行实际上会使 OP 的结果看起来更糟。 (如果有更多关于为什么要更改这条线的解释,那将有很长的路要走。)
    猜你喜欢
    • 1970-01-01
    • 2011-02-20
    • 2011-01-26
    • 1970-01-01
    • 1970-01-01
    • 2017-01-05
    • 1970-01-01
    • 2011-01-13
    • 1970-01-01
    相关资源
    最近更新 更多