============= 代码块 1==================
public void paint(Graphics g) {
super.paint(g);
paintTest();
//g.setColor(Color.BLACK);
//g.drawOval(409,231,15,15);
//g.fillOval(327,372,15,15);
}
public void paintTest() {
Graphics2D g = (Graphics2D)this.getGraphics();
g.setColor(Color.BLACK);
g.drawOval(409, 231, 15, 15);
g.drawOval(327, 372, 15, 15);
}
============= 代码块 2==================
public void paint(Graphics g) {
super.paint(g);
//paintTest();
g.setColor(Color.BLACK);
g.drawOval(409,231,15,15);
g.fillOval(327,372,15,15);
}
真是搞不懂,中间只是调用了一个函数,结果就会一个能画得出来,另一个却画不出来了。
下面是全部的源代码。共有两个 java 文件
==================TestPanel.java======================
import java.awt.*;
import javax.swing.*;
public class TestPanel extends JPanel {
BorderLayout borderLayout1 = new BorderLayout();
public TestPanel() {
try {
jbInit();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
public void paint(Graphics g) {
super.paint(g);
paintTest();
//g.setColor(Color.BLACK);
//g.drawOval(409,231,15,15);
//g.fillOval(327,372,15,15);
}
public void paintTest() {
Graphics2D g = (Graphics2D)this.getGraphics();
g.setColor(Color.BLACK);
g.drawOval(409, 231, 15, 15);
g.drawOval(327, 372, 15, 15);
}
void jbInit() throws Exception {
this.setLayout(borderLayout1);
}
}
==================Frame1.java======================
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Frame1 extends JFrame {
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new TestPanel();
JPanel jPanel2 = new JPanel();
JPanel jPanel3 = new JPanel();
JButton jButton2 = new JButton();
public Frame1() {
try {
jbInit();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
void jbInit() throws Exception {
this.getContentPane().setLayout(borderLayout1);
jButton2.setText("Test");
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
this.getContentPane().add(jPanel2, BorderLayout.SOUTH);
jPanel2.add(jButton2, null);
this.getContentPane().add(jPanel3, BorderLayout.NORTH);
}
public static void main(String[] args) {
Frame1 frame = new Frame1();
frame.setLocation(100, 100);
frame.setSize(500, 500);
frame.setVisible(true);
}
}
2 个解决方案
#1
在paintTest()方法里不要用this.getGraphics();得到新的Graphics对象,直接使用paint()方法的参数g就可以了
改成这样
public void paint(Graphics g) {
super.paint(g);
paintTest(g);
//g.setColor(Color.BLACK);
//g.drawOval(409,231,15,15);
//g.fillOval(327,372,15,15);
}
public void paintTest(Graphics g) {
g.setColor(Color.BLACK);
g.drawOval(409, 231, 15, 15);
g.drawOval(327, 372, 15, 15);
}
改成这样
public void paint(Graphics g) {
super.paint(g);
paintTest(g);
//g.setColor(Color.BLACK);
//g.drawOval(409,231,15,15);
//g.fillOval(327,372,15,15);
}
public void paintTest(Graphics g) {
g.setColor(Color.BLACK);
g.drawOval(409, 231, 15, 15);
g.drawOval(327, 372, 15, 15);
}
#2
repaint()由系统调用,
人工调用的时候不是很灵。
人工调用的时候不是很灵。
#1
在paintTest()方法里不要用this.getGraphics();得到新的Graphics对象,直接使用paint()方法的参数g就可以了
改成这样
public void paint(Graphics g) {
super.paint(g);
paintTest(g);
//g.setColor(Color.BLACK);
//g.drawOval(409,231,15,15);
//g.fillOval(327,372,15,15);
}
public void paintTest(Graphics g) {
g.setColor(Color.BLACK);
g.drawOval(409, 231, 15, 15);
g.drawOval(327, 372, 15, 15);
}
改成这样
public void paint(Graphics g) {
super.paint(g);
paintTest(g);
//g.setColor(Color.BLACK);
//g.drawOval(409,231,15,15);
//g.fillOval(327,372,15,15);
}
public void paintTest(Graphics g) {
g.setColor(Color.BLACK);
g.drawOval(409, 231, 15, 15);
g.drawOval(327, 372, 15, 15);
}
#2
repaint()由系统调用,
人工调用的时候不是很灵。
人工调用的时候不是很灵。