e575. The Quintessential Drawing Program
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的更多相关文章
- e586. Drawing Simple Shapes
There are two ways to draw basic shapes like circles, ovals, lines, arcs, squares, rectangles, round ...
- e595. Drawing an Image
See also e575 The Quintessential Drawing Program and e594 Reading an Image or Icon from a File. publ ...
- e591. Drawing Simple Text
See also e575 The Quintessential Drawing Program. public void paint(Graphics g) { // Set the desired ...
- 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 ...
- e577. Enabling Antialiasing
// See e575 The Quintessential Drawing Program public void paint(Graphics g) { // Retrieve the graph ...
- HTML5资料
1 Canvas教程 <canvas>是一个新的用于通过脚本(通常是JavaScript)绘图的HTML元素.例如,他可以用于绘图.制作图片的组合或者简单的动画(当然并不那么简单).It ...
- [zt] Android中使用List列表
原文地址:http://www.vogella.com/tutorials/AndroidListView/article.html 1. Android and Lists 1.1. Using l ...
- Marvelous Mazes
F - Marvelous Mazes Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submi ...
- [算法]A General Polygon Clipping Library
A General Polygon Clipping Library Version 2.32 http://www.cs.man.ac.uk/~toby/alan/software/gpc.h ...
随机推荐
- java实现简单webserver(分析+源码)
在日常的开发中,我们用过非常多开源的webserver,比如tomcat.apache等等.如今我们自己实现一个简单的webserver,主要的功能就是用户点击要訪问的资源,server将资源发送到c ...
- 【Android】7.5 RelativeLayout(相对布局)
分类:C#.Android.VS2015: 创建日期:2016-02-11 一.简介 RelativeLayout是一种相对布局,容器中子元素的位置是相对于其前一个元素或者其他元素的位置来计算的,或者 ...
- 使用VMware安装CentOS7步骤详情
准备资料: CentOS-7-x86_64-Everything-1611 点击下载CentOS 对,资料就这些 第一步. 点击文件 再点击新建虚拟机 第二步 .点击完新建虚拟机之后会跳出一个窗口 ...
- Lua整理——table库
table属性 table库是有一些辅助函数构成的,这些函数将table作为数组来操作. 当中.有对列表中插入和删除元素的函数,有对数组元素进行排序的函数.还有对链接一个数组中全部字符串的函数. 0. ...
- SourceInsight自定义常用快捷键
1.快速显示文件大纲,类似于eclipse中的Ctrl+O: “Options”-"Key Assignments"-"Symbol: Browse Local File ...
- 解决Git在Linux 乱码问题
在Linux如果要提交的文件名是中文的,默认git commit的时候就会把中文显示为一串数字如下: create mode "\346\265\213\350\257\225" ...
- eclipse中maven项目部署到tomcat [转]
其实maven项目部署到tomcat的方式很多,我从一开始的打war包到tomcat/webapps目录,到使用tomcat-maven插件,到直接使用servers部署,一路来走过很多弯路. 下面就 ...
- 每日英语:Mooncake Austerity Hits China's Mid-Autumn Festival
First baijiu, then red carpets, and now mooncakes. For Chinese government officials, the list of tab ...
- hdoj2571 命运
命运 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...
- [转]编写Android.mk中的LOCAL_SRC_FILES的终极技巧
希望看原文的请移步:[原创]编写Android.mk中的LOCAL_SRC_FILES的终极技巧 问题的引入 在使用NDK编译C/C++项目的过程中,免不了要编写Android.mk文件,其中最重要的 ...