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. Android开发5——文件读写

    一.基本概念 在Android应用中保存文件,保存的位置有两处 ①手机自带的存储空间,较小(如200M),适合保存一些小文件,Android中保存位置在data/data/应用包名/files目录 ② ...

  2. unity3D角色代码控制问题

    ///////////////2015/07/06//////// ///////////////by xbw////////////// //////////////环境 unity4.6.1// ...

  3. [hihoCoder] 骨牌覆盖问题·二

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上一周我们研究了2xN的骨牌问题,这一周我们不妨加大一下难度,研究一下3xN的骨牌问题?所以我们的题目是:对于3xN的棋盘 ...

  4. 关于Virtual的英语含义

    不知道有人注意没有,virtual的英语含义是:实际的,事实上,实质上.但是在计算机英语内却表示:虚拟的意思.比如:virtual memery 虚拟内存,virtual reality 虚拟现实. ...

  5. Firefox清空缓存的快捷键

    有时候调试网页,需要清空缓存,常用的firefox清空缓存的快捷键: Shift+Ctrl+Delete

  6. verilog中的default应该赋什么样的值

    Q:在状态机的case语句中,最后要加上默认项default,可是我看到有的书上写的是一个确定的状态,有的则是不定态xxx,到底应该写那个啊?求助! A1:取决于case条件是否完备啦如果你的case ...

  7. Linux系统中 Sublime Text 中文 GBK 文件乱码问题

    Sublime Text 是一个很不错编辑器,具有漂亮的界面和强大的功能.再加上丰富的插件,而且还跨平台,绝对是一款实打实的神器啊! 众所周知,Sublime Text 对中文支持的极差,可以说几乎就 ...

  8. maven(3)------maven构建web项目详细步骤

    eclipse集成工具,轻松通过maven构建web项目步骤如下: 一, 右键,new -->project, 进入下一页面 二,选择"Maven Project", 点击下 ...

  9. SQL中AND与OR的优先级

    突然发现,把基础给忘了,AND的优先级大于OR,试验如下: Oracle --Y ; --Y ) ; --No value ); 附,Oracle文档: http://docs.oracle.com/ ...

  10. python文件和目录操作方法大全

    一.python中对文件.文件夹操作时经常用到的os模块和shutil模块常用方法. 1.得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd()2.返回指定目录下的所有文件和 ...