本文目录一览:
- 1、java怎么实现图形化界面
- 2、如何用Java实现图形的放大和缩小
- 3、怎样用java编写图形界面的Application程序?
- 4、java图形类是什么?
- 5、java编写图形抽象类(Shape)
- 6、用JAVA写一个简单图形类
java怎么实现图形化界面
java图形化界面还是有很多内容要学习的,可以参考 如下实例:
public class Test extends JFrame{
MyPanel mp=null;
public static void main(String[] args){
// TODO Auto-generated method stub
Test jf= new Test();
}
public Test(){
mp=new MyPanel();
this.add(mp);
//设置标题
this.setTitle("绘图");
//设置窗体大小
this.setSize(400, 300);
//设置窗体的位置
this.setLocation(100,100);
//限制窗体的大小
this.setResizable(false);
//关闭窗体时,同时退出java虚拟机
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//显示窗体
this.setVisible(true);
}
}
//定义一个MyPanel(我自己的面板,用于绘图和实现绘图区域)
class MyPanel extends JPanel
{
//覆盖JPanel的paint方法
//Graphics是绘图的重要类,可以把它理解成一只画笔
public void paint(Graphics g)
{
//1。调用父类函数完成初始化
super.paint(g);
// //画圆
// g.drawOval(100, 100, 20, 20);
// //画直线
// g.drawLine(50, 150,150, 200);
// //画矩形边框
// g.drawRect(150, 150, 30, 40);
//
// //设置颜色。默认为黑色
// g.setColor(Color.blue);
// //填充矩形
// g.fillRect(10, 10, 20, 30);
//画弧形
g.drawArc(200,10, 100,150, 120,-80);
//在面板上画图片
Image im=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("图片路径"));
//显示图片
g.drawImage(im, 10, 10,200,180,this);
//画字
g.setColor(Color.red);
g.setFont(new Font("华文彩云",Font.BOLD,20));
g.drawString("要写的字", 80,220);
}
}
如何用Java实现图形的放大和缩小
java实现图形的放大和缩小,其实就是在画图时,改变图片的长和宽。以下代码参考一下:
import java.awt.Graphics;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;
public class App extends JFrame implements MouseListener, ActionListener {
int x = 0;
int y = 0;
File[] selectedFiles = null;
int fileIndex = 0;
int width = 200;
int height = 200;
public App() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(400, 300);
setResizable(false);
getContentPane().setLayout(null);
JPanel panel = new ImagePanel();
panel.setBounds(12, 40, 370, 218);
getContentPane().add(panel);
addMouseListener(this);
JButton btnBrowse = new JButton("Browse");
btnBrowse.addActionListener(this);
btnBrowse.setBounds(12, 9, 91, 21);
getContentPane().add(btnBrowse);
setVisible(true);
}
public static void main(String[] args) {
new App();
}
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(true);
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG GIF Images", "jpg", "gif");
// 设置文件类型
chooser.setFileFilter(filter);
// 打开选择器面板
int returnVal = chooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
selectedFiles = chooser.getSelectedFiles();
repaint();
}
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
Point point = MouseInfo.getPointerInfo().getLocation();
x = point.x;
y = point.y;
}
public void mouseReleased(MouseEvent e) {
Point point = MouseInfo.getPointerInfo().getLocation();
int thisX = point.x;
int thisY = point.y;
System.out.println("thisX=" + thisX + " " + "thisY=" + thisY);
if ((y - thisY 20 y - thisY 0)
|| (y - thisY 0 y - thisY -20)) {
// Y 在20范围内移动认为是水平移动
if (x thisX) {
// right
if (selectedFiles != null
fileIndex selectedFiles.length - 1) {
fileIndex++;
}
} else {
// left
if (selectedFiles != null fileIndex 0) {
fileIndex--;
}
}
} else {
if (x thisX) {
// 右下
width += 20;
height += 20;
} else {
// 左上
width -= 20;
height -= 20;
}
}
repaint();
}
class ImagePanel extends JPanel {
public void paint(Graphics g) {
super.paint(g);
if (selectedFiles != null) {
ImageIcon icon = new ImageIcon(selectedFiles[fileIndex]
.getPath());
g.drawImage(icon.getImage(), 0, 0, width, height, this);
}
}
}
}
怎样用java编写图形界面的Application程序?
java编写图形界面需要用到swing等组件,可以在eclipse中安装windowbuilder来开发窗体,自动生成窗体代码,然后自己再根据需要修改,如:
package mainFrame;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame。
Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。
java图形类是什么?
java 图形类库常见的有 swing 和 swt,这两个用的比较多些,像著名的开源工具 eclipse 就是 swt开发的。
如果你问得是画图的类的吧,一般是Graphics2D
java编写图形抽象类(Shape)
我来写一下吧:
abstract class Shape{
private double c;
private double s;
public abstract void girth();
public abstract void area();
public void setGirth(double c){
this.c = c;
}
public void setArea(double s){
this.s = s;
}
public double getGirth(){
return c;
}
public double getArea(){
return s;
}
public void outInfo(){}
}
class Circle extends Shape{
private static final double PI = 3.1415926;
private double r;
//定义一个构造函数
public Circle(double r){
this.r = r;
}
//重写抽象方法
public void girth() {
double a =2*PI*r;
super.setGirth(a);
}
public void area() {
double b =PI*r*r;
super.setArea(b);
}
public void outInfo() {
this.girth();
this.area();
System.out.println("所求圆形周长为:"+super.getGirth());
System.out.println("所求圆形面积为:"+super.getArea());
}
}
class Rectangle extends Shape{
private double height;
private double width;
//定义一个构造函数
public Rectangle(double height,double width){
this.height = height;
this.width = width;
}
//重写抽象方法
public void girth() {
double a =2*(height+width);
super.setGirth(a);
}
public void area() {
double b =(height*width);
super.setArea(b);
}
public void outInfo() {
this.girth();
this.area();
System.out.println("所求矩形周长为:"+super.getGirth());
System.out.println("所求矩形面积为:"+super.getArea());
}
}
class Triangle extends Shape{
private double lengthA;
private double lengthB;
private double lengthC;
//定义一个构造函数
public Triangle(double lengthA,double lengthB,double lengthC){
this.lengthA = lengthA;
this.lengthB = lengthB;
this.lengthC = lengthC;
}
//重写抽象方法
public void girth() {
double a =(lengthA+lengthB+lengthC);
super.setGirth(a);
}
public void area() {
if((lengthA+lengthB lengthC) || (lengthA + lengthC lengthB) || (lengthB+lengthC lengthA)) {
System.out.println("两边之和必须大于第三个边");
System.exit(0);
}
double p = (lengthA+lengthB+lengthC)/2;
double b = Math.sqrt(p*(p-lengthA)*(p-lengthB)*(p-lengthC));
super.setArea(b);
}
public void outInfo() {
this.girth();
this.area();
System.out.println("所求三角形周长为:"+super.getGirth());
System.out.println("所求三角形面积为:"+super.getArea());
}
}
public class ShapeTest {
public static void main (String [] args){
Shape circle = new Circle(3.0);
Shape rectangle = new Rectangle(8.0,7.0);
Shape triangle = new Triangle(3.0,4.0,5.0);
circle.outInfo();
rectangle.outInfo();
triangle.outInfo();
}
}
用JAVA写一个简单图形类
public class Test013 {
/**
* 编写一个图形类MyGraphic。 1)它有两个基本属性:图形线条的颜色String lineColor和图形的填充颜色String
* fillColor。 2)设计矩形类CRectangle,有属性double rLong和宽double rWidth, 使用方法 float
* calCircum()可以返回矩形的周长,使用方法float calSquare()可以返回矩形的面积。
* 编写方法show(),显示图形的线条颜色和填充颜色,输出面积和方法。 3)设计圆形类CCircle,定义属性:半径double
* radius,可以通过同名方法计算周长和面积。 编写方法show(),显示图形的线条颜色和填充颜色,输出面积和方法。
* 4)编写出应用程序对CRectangle类和CCircle类进行验证。 完成上述要求即可
*/
public static void main(String[] args) {
MyGraphic rectangle = new CRectangle(10, 5);
rectangle.setFillColor("紫色"); //设定矩形填充颜色
rectangle.setLineColor("白色"); //设定矩形线条颜色
rectangle.show();
System.out.println("矩形周长 = " + rectangle.calCircum());
System.out.println("矩形面积 = " + rectangle.calSquare());
MyGraphic circle = new CCircle(3);
circle.setFillColor("红色");
circle.setLineColor("黄色");
circle.show();
System.out.println("园形周长 = " + circle.calCircum());
System.out.println("园形面积 = " + circle.calSquare());
}
}
/**
* 图形类
*
*/
abstract class MyGraphic {
private String lineColor; // 图形线条的颜色
private String fillColor; // 图形的填充颜色
public String getLineColor() {
return lineColor;
}
public void setLineColor(String lineColor) {
this.lineColor = lineColor;
}
public String getFillColor() {
return fillColor;
}
public void setFillColor(String fillColor) {
this.fillColor = fillColor;
}
public MyGraphic(String lineColor, String fillColor) {
this.lineColor = lineColor;
this.fillColor = fillColor;
}
public MyGraphic() {
}
/**
* 显示图形的颜色
*/
public abstract void show();
/**
* 计算图形的周长
*/
public abstract float calCircum();
/**
* 计算图形的面积
*/
public abstract float calSquare();
}
/**
* 矩形类
*
*/
class CRectangle extends MyGraphic {
private double rLong; // 长
private double rWidth; // 宽
/**
* 通过构造函数为图形的属性赋值
*
* @param rLong
* @param rWidth
*/
public CRectangle(double rLong, double rWidth) {
this.rLong = rLong;
this.rWidth = rWidth;
}
/**
* @return 矩形的周长
*/
@Override
public float calCircum() {
return (float) (2 * (rLong + rWidth));
}
/**
* @return 矩形的面积
*/
@Override
public float calSquare() {
return (float) (rLong * rWidth);
}
@Override
public void show() {
System.out.println("矩形线条的颜色: " + super.getLineColor());
System.out.println("矩形填充颜色: " + super.getFillColor());
}
public double getrLong() {
return rLong;
}
public void setrLong(double rLong) {
this.rLong = rLong;
}
public double getrWidth() {
return rWidth;
}
public void setrWidth(double rWidth) {
this.rWidth = rWidth;
}
}
/**
* 圆形类
*
*/
class CCircle extends MyGraphic {
private double radius; // 圆形半径
public CCircle(double radius) {
this.radius = radius;
}
/**
* @return 圆形的周长
*/
@Override
public float calCircum() {
return (float) (2 * Math.PI * radius);
}
/**
* @return 圆形的面积
*/
@Override
public float calSquare() {
return (float) (Math.PI * radius * radius);
}
@Override
public void show() {
System.out.println("圆形线条的颜色: " + super.getLineColor());
System.out.println("圆形填充颜色: " + super.getFillColor());
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
}