import java.awt.*;
import java.awt.geom.Rectangle2D;
import javax.swing.*;public class DrawTest
{
public static void main(String[] args)
{
DrawFrame frame=new DrawFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}class DrawFrame extends JFrame
{
public DrawFrame()
{
setSize(Width,Height);
setTitle("DrawTest");
setLocation(500, 300);
DrawPanel panel=new DrawPanel();
add(panel);
}
public static final int Width=400;
public static final int Height=500;}class DrawPanel extends JPanel
{
public void paintComponet(Graphics g)
{ super.paintComponent(g);
Graphics2D g2=(Graphics2D)g;
//画一个方块
Rectangle2D rect=new Rectangle2D.Double(100,100,200,150);
g2.draw(rect);
}
}
为什么我的rect画不出来???
解决方案 »
- tomcat问题
- 一个Java程序中的继承和动态绑定问题?
- nearestPowerOfTwo原理是什么
- 求教,如何写一个像windows xp开机时的进度条?
- 有关java包的问题
- 问几个关于 Eclipse 的问题
- 谁有没有共享的搜索引擎源码,高分寻找。要JAVA的
- JButton控件的setRollverEnabled(boolean b)方法有什么用?
- 求教!(送分人)
- ***送分了??????怎样选定多个JLabel?
- java拷贝文件的代码带参数运行出错
- 为什么Byte b = new Byte("127"); b.toString() == b.toString 是false???
代码和你一样呢!
import java.awt.*;
import java.awt.color.*;
import java.awt.geom.*;
import java.awt.geom.Ellipse2D.Double;
import java.awt.font.*;
import javax.swing.*;class DrawPanel extends JPanel {
public void paintComponent(Graphics g) { super.paintComponent(g);
// this.setBackground(Color.RED); 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.setColor(Color.red);
g2.draw(rect);
Ellipse2D ellipse = new Ellipse2D.Double();
ellipse.setFrame(rect);
g2.draw(ellipse);
g2.draw(new Line2D.Double(leftx, topy, width + leftx, height + topy));
double ceterX = rect.getCenterX();
double ceterY = rect.getCenterY();
double radius = 150;
Ellipse2D circle = new Ellipse2D.Double();
circle.setFrameFromCenter(ceterX, ceterY, ceterX + radius, radius
+ ceterY);
g2.setColor(Color.blue);
g2.draw(circle);
// g2.setPaint(Color.RED);
String message = "老婆,我爱你!";
Font sansbold114 = new Font("SansSerif", Font.BOLD, 14);
g2.setFont(sansbold114);
FontRenderContext context = g2.getFontRenderContext();
Rectangle2D bouds = sansbold114.getStringBounds(message, context);
double x = (getWidth() - bouds.getWidth()) / 2;
double y = (getHeight() - bouds.getHeight()) / 2;
double ascent = -bouds.getY();
double baseY = y + ascent;
g2.drawString(message, (int) x, (int) baseY);
// g2.fill(circle);
g2.dispose();
}
}class DrawFrame extends JFrame {
public static final int DEFAULT_WIDTH = 400;
public static final int DEFALT_HEGIHT = 400; public DrawFrame() {
this.setTitle("DrawTest");
this.setSize(DEFAULT_WIDTH, DEFALT_HEGIHT); DrawPanel panel = new DrawPanel();
this.add(panel);
}}public class DrawTest { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
DrawFrame frame = new DrawFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}}
当产生重绘情况的时候 系统将自动调用paint()方法对容器进行重绘把把你的代码修改了一下...import java.awt.*;
import java.awt.geom.Rectangle2D;
import javax.swing.*;public class DrawTest
{
public static void main(String[] args)
{
DrawFrame frame=new DrawFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}class DrawFrame extends JFrame
{
public DrawFrame()
{
setSize(Width,Height);
setTitle("DrawTest");
setLocation(500, 300);
DrawPanel panel=new DrawPanel();
this.add(panel, java.awt.BorderLayout.CENTER);//使用了布局管理器 }
public static final int Width=400;
public static final int Height=500;}class DrawPanel extends JPanel
{
//绘图必须在这里进行
public void paint(Graphics g)
{
paintComponet(g);
}
public void paintComponet(Graphics g)
{ super.paintComponent(g);
Graphics2D g2=(Graphics2D)g;
//画一个方块
Rectangle2D rect=new Rectangle2D.Double(100,100,200,150);
g2.draw(rect);
}
}
"[color=#FF0000] "没有这句 代码...