62,615
社区成员
发帖
与我相关
我的任务
分享
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);
}
}
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);
}
}