1

我有三个类 - TableApp/Diner/TablePanel。我不太明白图形类是如何工作的。我需要从我的 main 方法向我的paintComponent 发送一个 Graphics 对象引用,但我不确定如何将它链接在一起。任何关于如何如此有帮助的解释!

TableApp 类

import javax.swing.*;
public class TableApp{
  public static void main(String[]args){    
  JFrame frame = new JFrame();
  TablePanel table = new TablePanel();
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setVisible(true);
  frame.setSize(500,500);  //I dont understand how to send my paintComponent a                    reference of a Graphics object
  } 
}

表面板类

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

public class TablePanel extends JPanel{

  Diner diner1,diner2,diner3,diner4,diner5,diner6;

  public TablePanel(){
    diner1 = new Diner(50,50,300,1,"Murray",Color.blue);
    diner2 = new Diner(50,50,300,1,"Murray",Color.blue);//will change once program runs
    diner3 = new Diner(50,50,300,1,"Murray",Color.blue);
    diner4 = new Diner(50,50,300,1,"Murray",Color.blue);
    diner5 = new Diner(50,50,300,1,"Murray",Color.blue);
    diner6 = new Diner(50,50,300,1,"Murray",Color.blue);

    setPreferredSize(new Dimension(300,200));
    setBackground(Color.white);


  }
  public void paintComponent(Graphics g){//need call from main
    super.paintComponent(g);
    diner1.draw(g);
    diner2.draw(g);
    diner3.draw(g);
    diner4.draw(g);
    diner5.draw(g);
    diner6.draw(g);
  }

}

晚餐班

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

    public class Diner{
      private int X,Y,diameter=50,seatNumber;
      private String name;
      private Color colour;

      public Diner(int x,int y,int d,int sN,String n,Color col){//construct
        X=x;
        Y=y;
        name=n;
        diameter=d;
        name=n;
        colour=col;

    }
      public void draw(Graphics g){
            g.setColor(Color.blue);
            g.fillOval(X,Y,diameter,diameter);
        }
}
4

2 回答 2

4
  • paintComponent(..)如果需要,您不要调用,只需调用repaint()它将通过调用update(..),paint(..)等来刷新容器paintComponent(..)

  • 在您的代码中,您永远不会添加TablePanel table到您JFrame应该做的事情以使其可见add(..)

  • 300此外,每个Diner相同的直径xy的坐标将在每个顶部50x50绘制所有s 并且具有首选尺寸的将不适合所有表格。所以基本上坐标需要返工和/或面板尺寸需要改变。DinerTablePanel200x300

  • 您不应该调用setSize(..)而是JFrame实现正确LayoutManager和/或覆盖getPreferredSize()以适应图纸的内容JPanel(注意不是使用的好习惯setPreferredSize(..)),而不是pack()在添加组件之后和设置JFrame可见之前调用。

  • Event Dispatch Thread请记住在viaSwingUtilities.invokeXXX块上创建和操作 Swing 组件。

  • 还有一些怎么样Graphics2DRenderingHints让这些圆圈看起来很完美:)

  • 我没有展示的进一步增强是使用 aList添加(通过TablePanellike中的方法调用addDiner(Diner d))并跟踪Diner您添加到TablePanel. 这您将简单地迭代列表paintComponent而不是冗余的代码行。

这是带有上述修复的代码:

在此处输入图像描述

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TableApp {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                TablePanel table = new TablePanel();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(table);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

class TablePanel extends JPanel {

    Diner diner1, diner2, diner3, diner4, diner5, diner6;

    public TablePanel() {
        diner1 = new Diner(10, 90, 20, 1, "Murray", Color.blue);
        diner2 = new Diner(70, 30, 20, 1, "Murray", Color.blue);//will change once program runs
        diner3 = new Diner(50, 60, 20, 1, "Murray", Color.blue);
        diner4 = new Diner(100, 90, 20, 1, "Murray", Color.blue);
        diner5 = new Diner(80, 120, 20, 1, "Murray", Color.blue);
        diner6 = new Diner(100, 30, 20, 1, "Murray", Color.blue);
        setBackground(Color.white);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(200, 200);
    }

    public void paintComponent(Graphics g) {//need call from main
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        diner1.draw(g2d);
        diner2.draw(g2d);
        diner3.draw(g2d);
        diner4.draw(g2d);
        diner5.draw(g2d);
        diner6.draw(g2d);
    }
}

class Diner {

    private int X, Y, diameter = 50, seatNumber;
    private String name;
    private Color colour;

    public Diner(int x, int y, int d, int sN, String n, Color col) {//construct
        X = x;
        Y = y;
        name = n;
        diameter = d;
        name = n;
        colour = col;

    }

    public void draw(Graphics2D g2d) {
        g2d.setColor(Color.blue);
        g2d.fillOval(X, Y, diameter, diameter);
    }
}
于 2013-01-30T00:14:40.283 回答
2

您不会将 Graphics 对象传递给paintComponent。当您将组件添加到框架时,这将由 Swing 完成并调用(正如有人所说)。所以你在 main 中没有做的是以下

frame.add(table)

将组件添加到框架后,它将在框架上绘制。

于 2013-01-30T00:13:49.437 回答