To draw on the screen, it is first necessary to subclass a JComponent and override its paint() method. The paint() method is automatically called by the windowing system whenever component's area needs to be repainted.

The paint() method is supplied a graphics context which is used to draw shapes and images. The coordinate system of a graphics context is such that the origin is at the northwest corner and x-axis increases toward the right while the y-axis increases toward the bottom.

This example defines a component that draws an oval and installs an instance of this component in a frame. See also e586 Drawing Simple Shapes.

    import java.awt.*;
import javax.swing.*; public class BasicDraw {
public static void main(String[] args) {
new BasicDraw();
}
BasicDraw() {
// Create a frame
JFrame frame = new JFrame(); // Add a component with a custom paint method
frame.getContentPane().add(new MyComponent()); // Display the frame
int frameWidth = 300;
int frameHeight = 300;
frame.setSize(frameWidth, frameHeight);
frame.setVisible(true);
} class MyComponent extends JComponent {
// This method is called whenever the contents needs to be painted
public void paint(Graphics g) {
// Retrieve the graphics context; this object is used to paint shapes
Graphics2D g2d = (Graphics2D)g; // Draw an oval that fills the window
int x = 0;
int y = 0;
int width = getSize().width-1;
int height = getSize().height-1;
g2d.drawOval(x, y, width, height);
}
}
}
Related Examples

e575. The Quintessential Drawing Program的更多相关文章

  1. e586. Drawing Simple Shapes

    There are two ways to draw basic shapes like circles, ovals, lines, arcs, squares, rectangles, round ...

  2. e595. Drawing an Image

    See also e575 The Quintessential Drawing Program and e594 Reading an Image or Icon from a File. publ ...

  3. e591. Drawing Simple Text

    See also e575 The Quintessential Drawing Program. public void paint(Graphics g) { // Set the desired ...

  4. e578. Setting the Clipping Area with a Shape

    This example demonstrates how to set a clipping area using a shape. The example sets an oval for the ...

  5. e577. Enabling Antialiasing

    // See e575 The Quintessential Drawing Program public void paint(Graphics g) { // Retrieve the graph ...

  6. HTML5资料

    1 Canvas教程 <canvas>是一个新的用于通过脚本(通常是JavaScript)绘图的HTML元素.例如,他可以用于绘图.制作图片的组合或者简单的动画(当然并不那么简单).It ...

  7. [zt] Android中使用List列表

    原文地址:http://www.vogella.com/tutorials/AndroidListView/article.html 1. Android and Lists 1.1. Using l ...

  8. Marvelous Mazes

    F - Marvelous Mazes Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submi ...

  9. [算法]A General Polygon Clipping Library

    A General Polygon Clipping Library Version 2.32    http://www.cs.man.ac.uk/~toby/alan/software/gpc.h ...

随机推荐

  1. Python islower() 方法

    描述 Python islower() 方法检测字符串是否由小写字母组成. 相反的方法:isupper() 方法. 语法 islower() 方法语法: S.islower() 参数 无. 返回值 如 ...

  2. 跟我一起学习VIM - vim插件合集

    2016-06-14 15:04 13333人阅读 评论(0) 收藏 举报 分类: Linux(104)  目录(?)[+]  前两天同事让我在小组内部分享一下VIM,于是我花了一点时间写了个简短的教 ...

  3. [svc][op]磁盘Inode详解-重要

    另一篇白话总结 一.inode是什么 理解inode,要从文件储存说起. 文件储存在硬盘上,硬盘的最小存储单位叫做"扇区"(Sector).每个扇区储存512字节(相当于0.5KB ...

  4. linux 调试利器gdb, strace, pstack, pstree, lsof

    1) 如何使用strace+pstack利器分析程序性能? http://www.cnblogs.com/bangerlee/archive/2012/04/30/2476190.html 此文有详细 ...

  5. CocoaPods did not set the base configuration of your project 问题解决方式

    今天在使用pod install的时候.出现了 [!] CocoaPods did not set the base configuration of your project because you ...

  6. WebService之CXF注解之四(測试类)

    TeacherTest.java: /** * @Title:TeacherTest.java * @Package:com.test.service * @Description: * @autho ...

  7. dubbo的一些默认变量

    dubbo默认变量表 变量名 描述 默认值 用途 DEFAULT_IO_THREADS 默认IO线程 Math.min(Runtime.getRuntime().availableProcessors ...

  8. xml相关术语说明

    <project xmlns="http://maven.apache.org/POM/4.0.0"  --命名空间,类似包名,因为xml的标签可自定义,需要命名空间来区分x ...

  9. C# 简单Tcp通信demo

    Client 代码 private void btnSend_Click(object sender, EventArgs e) { TcpClient tcpClient = new TcpClie ...

  10. Web应用程序开发的标准架构