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. powerdesigner学习笔记【转载】

    转自:http://blog.itpub.net/11968859/viewspace-620440/ 谢谢! 1.做CDM模型的时候,因为开始定义ITEM的时候,没有注意把NAME和CODE全定义成 ...

  2. java基础常见问题和eclipse常用快捷键

    一.java常用库 java.lang中 StringBuffer,StringBuilder,System,Runtime,Math java.util Date,Calendar,Random j ...

  3. 3dmax 2012 贴图通道与uv通道,烘焙场景

    1,修改贴图通道(假设要将贴图由自发光通道改至漫反射通道): 选中材质球,通入贴图栏,选中自发光通道中的贴图路径,拖至漫反射通道,松开鼠标时弹出对话框,选'交换'.(有时候解析fbx文件时解析不出贴图 ...

  4. C 字符串常量和字符串变量定义和区别

    字符串常量 定义:在一个双引号""内的字符序列或者转义字符序列称为字符串常量 例如:"HA HA!"  "abc"  "\n\t& ...

  5. [gj]HK一行所见闻

    香港一行 20多年来,未未去过HK,前段时间由于工作关系去了趟HK.感触良多. 一清早,福田过关,做火车,做地铁,一通到了目的地. 总结对那边的印象: 1,所有人都是粤语,包括工作交流.而且他们不怎么 ...

  6. Linux GCC编译库

    本文主要解决以下几个问题 1).为什么要使用库? 2).库的分类 3).创建自己的库 为什么要使用库? 或许大家对自己初学 Linux时的情形仍记忆尤新吧.如果没有一个能较好的解决依赖关系的包管理器, ...

  7. redis性能提升

    1.redis请求执行原理redis客户端与redis服务器之间使用TCP协议进行连接,一个科幻可以通过一个socket连接发送多个请求命令,但每个请求命令发出后client通常会阻塞并等待redis ...

  8. ClouderaManager启动NodeManager失败!报错Failed to initialize container executor

    报错信息: 2016-07-27 10:53:14,102 WARN org.apache.hadoop.yarn.server.nodemanager.LinuxContainerExecutor: ...

  9. java 多线程9 : synchronized锁机制 之 代码块锁

    synchronized同步代码块 用关键字synchronized声明方法在某些情况下是有弊端的,比如A线程调用同步方法执行一个较长时间的任务,那么B线程必须等待比较长的时间.这种情况下可以尝试使用 ...

  10. How to Use HTML5 FUll Screen API(如何使用HTML5全屏接口) 【精】

    原文链接:http://www.sitepoint.com/use-html5-full-screen-api/ 如果你不太喜欢变化太快的东西,那么web开发可能不适合你.我曾在2012年末有写过Fu ...