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 rfind()方法

    描述 Python rfind() 返回子字符串最后一次出现在字符串中的索引位置,该方法与rindex() 方法一样,只不过如果子字符串不在字符串中不会报异常,而是返回-1. 语法 rfind() 方 ...

  2. HTTP请求流程(二)----Telnet模拟HTTP请求

    http://www.cnblogs.com/stg609/archive/2008/07/06/1237000.html 上一部分"流程简介", 我们大致了解了下HTTP请求的流 ...

  3. JEECG图表配置说明

    图表配置可以做什么? 图表配置可以通过在线配置,无需编写代码生成图形报表页面.使用highcharts.js实现,可以运行在任何现代浏览器,包括移动终端以及IE6.目前支持曲线图.柱状图等基础报表. ...

  4. mysql-cluster 环境安装&配置

    一.mysql-cluster 的介绍: 1.说心里话mysql-cluster这货性能上是不行的,之前一个同事测试了来的结果是8个主机组成的mysql-cluster性能 上搞不过一个单机的mysq ...

  5. 【Android】10.1 扩展组件库和其他视图--本章示例主界面

    分类:C#.Android.VS2015: 创建日期:2016-02-18 1.主界面运行截图 2.MainActivity.cs文件中对应的代码 chItems.Add(new Chapter() ...

  6. Top 40 Static Code Analysis Tools

    https://www.softwaretestinghelp.com/tools/top-40-static-code-analysis-tools/ In this article, I have ...

  7. iOS开发-模拟器的小常识

    /* 补充: 让模拟器锁屏: command + l */ 让模拟器变大变小 打开Xcode模拟器,选择Window->Scale->

  8. js 笔记 数组(对象)

    一.javascript push 的元素为指针 var data = {"test":{"201308":"23","20130 ...

  9. MySQL Daemon failed to start

    http://blog.163.com/cmdbat@126/blog/static/17029212320122804743900/

  10. 基于jQuery商品分类选择提交表单代码

    分享一款基于jQuery商品分类选择提交表单代码.这是一款基于jQuery实现的商品信息选择列表表单提交代码. 在线预览   源码下载 实现的代码: <div class="yList ...