在组件中显示信息
import java.awt.*;
import javax.swing.*;
public class hello {
public static void main(String[] args)
{
EventQueue.invokeLater(()->{
NotHelloWordsFrame frame =new NotHelloWordsFrame();
frame.setTitle("NotHelloWorld");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
}
}
class NotHelloWordsFrame extends JFrame{
public NotHelloWordsFrame()
{
add(new NotHelloWordsComponent());
pack();
}
}
class NotHelloWordsComponent extends JComponent{
public static final int MESSAGE_X =75;
public static final int MESSAGE_Y=100;
private static final int DEFAULT_WIDTH =300;
private static final int DEFAULT_HEIGHT=200;
public void paintComponent(Graphics g)
{
g.drawString("NOT a ,world program",MESSAGE_X,MESSAGE_X);
}
public Dimension getPreferredSize()
{
return new Dimension(DEFAULT_WIDTH,DEFAULT_HEIGHT);
}
}
处理2D图形
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import javax.swing.*;
public class hello {
public static void main(String[] args)
{
EventQueue.invokeLater(()->{
DrawFrame frame=new DrawFrame();
frame.setTitle("Drawrame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
}
}
class DrawFrame extends JFrame{
public DrawFrame()
{
add(new DrawComponent());
pack();
}
}
class DrawComponent extends JComponent{
private static int DEFAULT_WIDTH=400;
private static int DEFAULT_HEIGHT=400;
public void paintComponent(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.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);
}
public Dimension getPreferredSize()
{
return new Dimension(DEFAULT_WIDTH,DEFAULT_HEIGHT);
}
}
使用字体
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import javax.swing.*;
public class hello {
public static void main(String[] args)
{
EventQueue.invokeLater(()->{
FontFrame frame =new FontFrame();
frame.setTitle("FontTile");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
}
}
class FontFrame extends JFrame
{
public FontFrame()
{
add(new FrontComponent());
pack();
}
}
class FrontComponent extends JComponent{
private static final int DEFAULT_WIDTH=300;
private static final int DEFAULT_HEIGHT=200;
public void paintComponent(Graphics g)
{
Graphics2D g2=(Graphics2D) g;
String message="Hello World";
Font f= new Font("Serif",Font.BOLD,36);
g2.setFont(f);
FontRenderContext context =g2.getFontRenderContext();
Rectangle2D bounds=f.getStringBounds(message,context);
double x=(getWidth()-bounds.getWidth())/2;
double y=(getHeight()-bounds.getHeight())/2;
double ascent =-bounds.getY();
double baseY=y+ascent;
g2.drawString(message,(int)x,(int)baseY);
g2.setPaint(Color.LIGHT_GRAY);
g2.draw(new Line2D.Double(x,baseY,x+bounds.getWidth(),baseY));
Rectangle2D rect =new Rectangle2D.Double(x,y,bounds.getWidth(),bounds.getHeight());
g2.draw(rect);
}
public Dimension getPreferredSize()
{
return new Dimension(DEFAULT_WIDTH,DEFAULT_HEIGHT);
}
}