这几天看Javacore 运行上面的画图的例子 出来了一个窗口 但是没有得到想要的图像 面板上什么也没有 大家帮忙看下啊
import javax.swing.JFrame;@SuppressWarnings("serial")
public class DrawFrame extends JFrame { public static final int DEFAULT_WIDTH = 400;
public static final int DEFAULT_HEIGHT = 400;
public DrawFrame(){
setTitle("DrawTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
DrawComponent component = new DrawComponent();
add(component);
}
}import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;import javax.swing.JComponent;public class DrawComponent extends JComponent{
public DrawComponent(){
super.doLayout();
}
public void painComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
double leftX = 100;
double topY = 100;
double width = 200;
double height = 150;
Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height);
g2.draw(rect);
Ellipse2D ellipse = new Ellipse2D.Double();
ellipse.setFrame(rect);
g2.draw(ellipse);
g2.setPaint(Color.RED);
g2.drawString("Warning!", 100, 100);
g2.draw(new Line2D.Double(leftX, topY, leftX + width, topY + height));
double centerX = rect.getCenterX();
double centerY = rect.getCenterY();
double radius = 150;
Ellipse2D circle = new Ellipse2D.Double();
circle.setFrameFromCenter(centerX, centerY, centerX + radius, centerY + radius);
g2.draw(circle);
}
}import java.awt.*;
import javax.swing.*;
public class DrawTest { public static void main(String[] args) {
EventQueue.invokeLater(new Runnable(){
public void run(){
DrawFrame frame = new DrawFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}); }
}
import javax.swing.JFrame;@SuppressWarnings("serial")
public class DrawFrame extends JFrame { public static final int DEFAULT_WIDTH = 400;
public static final int DEFAULT_HEIGHT = 400;
public DrawFrame(){
setTitle("DrawTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
DrawComponent component = new DrawComponent();
add(component);
}
}import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;import javax.swing.JComponent;public class DrawComponent extends JComponent{
public DrawComponent(){
super.doLayout();
}
public void painComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
double leftX = 100;
double topY = 100;
double width = 200;
double height = 150;
Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height);
g2.draw(rect);
Ellipse2D ellipse = new Ellipse2D.Double();
ellipse.setFrame(rect);
g2.draw(ellipse);
g2.setPaint(Color.RED);
g2.drawString("Warning!", 100, 100);
g2.draw(new Line2D.Double(leftX, topY, leftX + width, topY + height));
double centerX = rect.getCenterX();
double centerY = rect.getCenterY();
double radius = 150;
Ellipse2D circle = new Ellipse2D.Double();
circle.setFrameFromCenter(centerX, centerY, centerX + radius, centerY + radius);
g2.draw(circle);
}
}import java.awt.*;
import javax.swing.*;
public class DrawTest { public static void main(String[] args) {
EventQueue.invokeLater(new Runnable(){
public void run(){
DrawFrame frame = new DrawFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}); }
}
import javax.swing.JFrame;
import javax.swing.JLabel;//@SuppressWarnings("serial")
public class DrawFrame extends JFrame {
DrawComponent component = new DrawComponent();
public static final int DEFAULT_WIDTH = 400;
public static final int DEFAULT_HEIGHT = 400;
JLabel btn=new JLabel("anniu");
public DrawFrame(){
setTitle("DrawTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); getContentPane().add(component);
}
} import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D; import javax.swing.JComponent; public class DrawComponent extends JComponent{
//public DrawComponent(){
//super.doLayout();
//}
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D) g;
double leftX = 100;
double topY = 100;
double width = 200;
double height = 150; Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height);
g2.draw(rect);
//g2.drawLine(100,100,150,150);
Ellipse2D ellipse = new Ellipse2D.Double();
ellipse.setFrame(rect);
g2.draw(ellipse);
g2.setPaint(Color.RED);
g2.drawString("Warning!", 100, 100);
g2.draw(new Line2D.Double(leftX, topY, leftX + width, topY + height)); double centerX = rect.getCenterX();
double centerY = rect.getCenterY();
double radius = 150; Ellipse2D circle = new Ellipse2D.Double();
circle.setFrameFromCenter(centerX, centerY, centerX + radius, centerY + radius);
g2.draw(circle);
}
} import java.awt.*;
import javax.swing.*;
public class DrawTest { public static void main(String[] args) {
EventQueue.invokeLater(new Runnable(){
public void run(){
DrawFrame frame = new DrawFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}); }
}
运行下试试!要是不行把那个getContentPane().add()改为add()我们版本不同