社区
Java SE
帖子详情
java 如何实现Graphics2D画的图形进行缩放?
MinQuanRen
2010-07-27 05:50:44
在java的面板(Jpanel)中用Graphics2D画的图形,如矩形,直线等,该如何实现放大 或 缩小功能呢?
...全文
1230
9
打赏
收藏
java 如何实现Graphics2D画的图形进行缩放?
在java的面板(Jpanel)中用Graphics2D画的图形,如矩形,直线等,该如何实现放大 或 缩小功能呢?
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
9 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
huntor
2010-07-28
打赏
举报
回复
java.awt.Graphics2D
java.awt.geom.AffineTransform
提供的scale变换。
awusoft
2010-07-28
打赏
举报
回复
放大缩小实际是重新画的吧.
小p变砖头
2010-07-28
打赏
举报
回复
没有尝试过缩放 但个人认为可以通过改变坐标和长度等属性实现 这就是你编码能力的问题了 貌似java没有现成的方法实现缩放
MinQuanRen
2010-07-28
打赏
举报
回复
csdn里高手哪里去了,难倒没人会吗?
小弟急等............
MinQuanRen
2010-07-28
打赏
举报
回复
(3)NewKFPoint.java这是我自定义的一个结构类
public class NewKFPoint {
private float x;
private float y;
private boolean isRemove;
public NewKFPoint() {
x = 0.0f;
y = 0.0f;
isRemove = false;
}
public NewKFPoint(float x_, float y_) {
this.x = x_;
this.y = y_;
isRemove = false;
}
public void setX(float x_) {
this.x = x_;
}
public void setY(float y_) {
this.y = y_;
}
public float getX() {
return this.x;
}
public float getY() {
return this.y;
}
public void setScale(boolean increase, double scale) {
if (increase) {
this.x *= (float)scale;
// this.y *= (float)scale;
} else {
this.x /= (float)scale;
// this.y /= (float)scale;
}
}
}
MinQuanRen
2010-07-28
打赏
举报
回复
由于不能上传附件我大概把代码贴下来把
(1)NewJFrame.java(控件两个按钮、一个JScrollPane、和NewSubKFEditorPanel)大概如下:
public class NewJFrame extends javax.swing.JFrame {
private double scale[] = {1.0, 1.4, 1.7, 2.0, 2.2, 2.5, 2.7, 2.8, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0};
int i = 0;
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if ((i > -1) && (i < scale.length)) {
System.out.println("i = " + i);
newSubKFEditorPanel1.increaseZoomScale(scale[i++]);
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
if ((i > 0) && (i <= scale.length)) {
System.out.println("i = " + --i);
newSubKFEditorPanel1.decreaseZoomScale(scale[i]);
}
}
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JScrollPane jScrollPane1;
private com.sony.threedee.gui.NewSubKFEditorPanel newSubKFEditorPanel1;
// End of variables declaration
}
(2)NewSubKFEditorPanel.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* NewSubKFEditorPanel.java
*
* Created on 2010年7月27日, 上午10:21:44
*/
package com.sony.threedee.gui;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.math.BigDecimal;
/**
*
* @author lichuanfeng
*/
public class NewSubKFEditorPanel extends javax.swing.JPanel {
//for test code
private static final float dash1[] = {10.0f};
private NewKFPoint[] kfPoints = new NewKFPoint[5];
private boolean bzoomScale = true;
// private double sx = 1.0;
// private double sy = 1.0;
private int panel_Width = 0;
// private int panel_Height = 0;
private int offset = 3;
private int fact_panelWidth = 0;
// private int fact_panelHeight = 0;
private AffineTransform at = new AffineTransform(); //AffineTransform
private Color color = new Color(255, 255, 255);
private static final float panelWidth = 499.0f;
private static final float panelHeight = 100.0f;
private static final float halfPanelWidth = 249.5f;
private static final float halfPanelHeight = 50.0f;
// private static float centerX = 250.0f;
// private static float centerY = 50.0f;
private Line2D centerLine = new Line2D.Float(0.0f, halfPanelHeight, panelWidth, halfPanelHeight);
public NewSubKFEditorPanel() {
initComponents();
Dimension dimension = this.getPreferredSize();
fact_panelWidth = dimension.width;
// fact_panelHeight = dimension.height;
int x = 10;
for (int i = 0; i < 5; i++) {
kfPoints[i] = new NewKFPoint(x, 10);
x += 10;
}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setAutoscrolls(true);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 508, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 143, Short.MAX_VALUE)
);
}// </editor-fold>
// Variables declaration - do not modify
// End of variables declaration
public void setBackGroundColor(Color col) {
color = col;
}
public void setZoomScale() {
bzoomScale = false;
repaint();
}
public void setScale(boolean b, double scale) {
for (int i = 0; i < 5; i++) {
kfPoints[i].setScale(b, scale);
}
}
public void increaseZoomScale(double scale) {
if (at != null) {
// sx = scale;
// sy = scale;
setScale(true, scale);
}
panel_Width = (int)(fact_panelWidth*scale);
setPreferredSize(new Dimension(panel_Width, this.getHeight()));
updateUI();
}
public void decreaseZoomScale(double scale) {
if (at != null) {
// sx = scale;
// sy = scale;
setScale(false, scale);
}
panel_Width = (int)(panel_Width/scale);
setPreferredSize(new Dimension(panel_Width, this.getHeight()));
updateUI();
}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
// panel_Width = this.getWidth();
// panel_Height = this.getHeight();
// at = g2.getTransform();
// at.scale(sx, sy);
// g2.setTransform(at);
g2.setBackground(color);
g2.clearRect(0, 0, getWidth(), getHeight());
g2.setPaint(Color.black);
g2.draw(centerLine);
//for test code
// g2.draw(new Rectangle2D.Double(2, 2, panel_Width-20, panel_Height-20));
GeneralPath polyline = new GeneralPath();
for (int j = 0; j < 5; j++) {
polyline.moveTo (kfPoints[j].getX(), kfPoints[j].getY()-offset);
polyline.lineTo(kfPoints[j].getX()-offset, kfPoints[j].getY());
polyline.lineTo(kfPoints[j].getX(), kfPoints[j].getY()+offset);
polyline.lineTo(kfPoints[j].getX()+offset, kfPoints[j].getY());
polyline.lineTo(kfPoints[j].getX(), kfPoints[j].getY()-offset);
}
polyline.closePath();
g2.setPaint(Color.red);
g2.fill(polyline);
g2.draw(polyline);
g2.dispose();
}
}
MinQuanRen
2010-07-28
打赏
举报
回复
缩放原理我大概能明白了。
现在我们的需求有变化,要求是这样的:
假如我的Panel上画了5个小菱形,现在有两个按钮对进行放大和缩小,要求如下:
(1)按固定比例值进行缩放。
(2)缩放必须横向缩放,纵向不动,缩放过程中菱形的大小不改变,改变的只是坐标位置(其实也就是横向坐标)。
(3)缩放必须以panle的中心进行两边缩放。
我是如下实现的,在一个NewSubKFEditorPanel上进行画图操作,在NewJFrame上拖动了JScrollPane,然后把NewSubKFEditorPanel放到JScrollPane上,通过比例值来改变NewSubKFEditorPanel上面的菱形坐标,同时
根据这个比例值变更NewSubKFEditorPanel的大小,以使JScrollPane滚动条出来。
但是存在两个问题:
(1)根据比例值放大并改变NewSubKFEditorPanel大小时,会导致最后找不到菱形。
(2)没有以中心点进行缩放。
一会把代码提交,请高手帮忙,再次多谢了。
heying876
2010-07-28
打赏
举报
回复
我发过一个写好的类 在csdn的下载中搜索下吧
gentalguo
2010-07-28
打赏
举报
回复
AffineTransform 仿射变换。
可以用于平移,旋转,缩放等功能。
多看下文档吧,虽然文档上是以矩阵为例说的。但是理解了原理是一样的。
需要注意,确定变换时候的的中心点,就是逻辑上的坐标原点。
Graphics2D
类基本使用
Java
语言在Graphics类提供绘制各种基本的几何
图形
的基础上,扩展Graphics类提供一个
Graphics2D
类,它拥用更强大的二维
图形
处理能力,提供、坐标转换、颜色管理以及文字布局等更精确的控制。 一、绘图属性
Graphics2D
定义了几种方法,用于添加或改变
图形
的状态属性。可以通过设定和修改状态属性,指定
画
笔宽度和
画
笔的连接方式;设定平移、旋转、
缩放
或修剪变换
图形
;以及设定填充
图形
的颜色和图案等。
图形
状态属性用特定的对象存储。 1. stroke属性 stroke属性控制线条的宽度、笔形样式、
Java
图像编程之:
Graphics2D
java
.awt.
Graphics2D
是
Java
编程语言中的一个类,它是
java
.awt.Graphics类的子类。它提供了一系列用于绘制
图形
和处理
图形
操作的方法。
Graphics2D
类是
Java
2D API 的一部分,它允许开发人员在
图形
界面应用程序中创建复杂的
图形
和图像效果。
Graphics2D
类提供了一些强大的绘图功能,如绘制线条、矩形、椭圆、多边形等。它还支持图像的平移、旋转、
缩放
和剪切等变换操作。
java
的
graphics2d
_
Java
Graphics2D
类的绘图方法
Java
Graphics2D
类的绘图方法
Java
语言在Graphics类提供绘制基本的几何
图形
的基础上,扩展Graphics类提供一个
Graphics2D
类,它拥用强大的二维
图形
处理能力,提供、坐标转换、颜色管理以及文字布局等的控制。绘图属性
Graphics2D
定义了几种方法,用于添加或改变
图形
的状态属性。可以通过设定和修改状态属性,指定
画
笔宽度和
画
笔的连接方式;设定平移、旋转、
缩放
或修剪变换图...
Java
Graphics2D
实现
流程图绘制完整封装实战
是
Java
中用于表示图像的一种类,位于包中,它不仅用于存储图像数据,还支持对图像的读写、修改和渲染操作。与传统的图像表示方式不同,提供了访问图像像素级别的能力,使得开发者可以对其
进行
各种图像处理操作。在
Java
图形
处理体系中,是一个非常核心的类,常用于
图形
绘制、图像滤镜、水印添加、图像
缩放
、截图保存等场景。其核心结构包括两个部分:图像数据(Raster):存储图像的像素数据;颜色模型(ColorModel):描述像素数据如何映射为颜色。下面是一个简单的。
Java SE
62,621
社区成员
307,257
社区内容
发帖
与我相关
我的任务
Java SE
Java 2 Standard Edition
复制链接
扫一扫
分享
社区描述
Java 2 Standard Edition
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章