我不知道为什么要为WestPanel调用paintComponent,但是不能为East Panel工作......即使代码类似。请一些帮助。
public class MyFrame extends JFrame{
public static final int WIDTH = 1664;
public static final int HEIGHT= 1088;
public static final int TILE_W = 64;
public static final int TILE_H = 64;
private CenterPanel cP;
private WestPanel wP;
private EastPanel eP;
/*private NorthPanel nP;
private SouthPanel sP;*/
public MyFrame(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(WIDTH, HEIGHT);
setTitle("Chuta");
setResizable(false);
setLocationRelativeTo(null);
cP = new CenterPanel();
add(cP, BorderLayout.CENTER);
wP = new WestPanel();
add(wP, BorderLayout.WEST);
eP = new EastPanel();
add(eP, BorderLayout.EAST);
setVisible(true);
}
public static void main(String[] args) {
MyFrame f = new MyFrame();
}
这个西面板正在绘制 包框架;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
import static frame.MyFrame.*;
public class WestPanel extends JPanel {
private int wCP;
public WestPanel(){
wCP=3*TILE_W;
Dimension size = getPreferredSize();
size.width = wCP;
setPreferredSize(size);
setBackground(new Color(0, 115, 102));
new CardGrid(TILE_W/2);
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setStroke(new BasicStroke(5));
g2d.setColor(Color.white);
for (int y = TILE_H ; y < TILE_H*15; y+=TILE_H*3) {
g2d.drawRect(TILE_W / 2, y, 120, TILE_H * 3);
}
}
此东面板未绘制
import static frame.MyFrame.TILE_W;
import static frame.MyFrame.TILE_H;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
public class EastPanel extends JPanel {
private int eCP;
public EastPanel(){
eCP=3*TILE_W;
Dimension size = getPreferredSize();
size.width = eCP;
setPreferredSize(size);
setBackground(new Color(0, 112, 102));
new CardGrid(MyFrame.WIDTH-120-TILE_W/2);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setStroke(new BasicStroke(5));
g2d.setColor(Color.white);
for (int y = TILE_H ; y < TILE_H*15; y+=TILE_H*3) {
g2d.drawRect(MyFrame.WIDTH- 120 -TILE_W / 2, y, 120, TILE_H * 3);
}
}
}
答案 0 :(得分:0)
自动配置Graphics
上下文,以便位置0x0
代表组件的左上角
这意味着当您使用MyFrame.WIDTH - 120
之类的东西时,您将在组件的可见边界之外进行绘画。
请记住,组件有自己的上下文区域0x0xwidthxheight
。您不应该在组件中使用MyFrame.WIDTH