本文目錄一覽:
- 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;
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/159216.html