社区
Java SE
帖子详情
java 如何实现Graphics2D画的图形进行缩放?
MinQuanRen
2010-07-27 05:50:44
在java的面板(Jpanel)中用Graphics2D画的图形,如矩形,直线等,该如何实现放大 或 缩小功能呢?
...全文
1245
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 仿射变换。
可以用于平移,旋转,缩放等功能。
多看下文档吧,虽然文档上是以矩阵为例说的。但是理解了原理是一样的。
需要注意,确定变换时候的的中心点,就是逻辑上的坐标原点。
华为荣耀recovery1.0
源码链接: https://pan.quark.cn/s/a4b39357ea24 This directory contains a script ('makefsdata') to create C code suitable for httpd for given html pages (or other files) in a directory. There is also a plain C console application doing the same and extended a bit. Usage: htmlgen [targetdir] [-s] [-i]s targetdir: relative or absolute path to files to convert switch -s: toggle processing of subdirectories (default is on) switch -e: exclude HTTP header from file (header is created at runtime, default is on) switch -11: include HTTP 1.1 header (1.0 is default) if targetdir not specified, makefsdata will attempt to process files in subdirectory 'fs'.
光纤耦合器Matlab模拟.rar
光纤耦合器Matlab模拟.rar
ModbusDoctor软件
官网下载地址:需墙:https://www.kscada.com/modbusdoctor.html 使用非常简单,并且免费、免安装 Modbus Doctor(强烈推荐) 下载地址:搜索 “Modbus Doctor” 官网或 GitHub 界面现代化,支持中文,操作和 Modbus Poll 很像
数据中心光伏-储能-数据中心容量优化配置研究(Matlab代码
实现
)
内容概要:本文围绕“光伏-储能-数据中心”系统的容量优化配置展开研究,旨在通过构建数学模型并结合Matlab编程
实现
,对园区内光伏发电、储能设备与数据中心算力负荷之间的多能互补关系
进行
协同优化。研究综合考虑可再生能源出力的波动性、储能系统的充放电特性以及数据中心的动态用电需求与算力调度特性,建立以最小化综合成本、降低碳排放强度、提升能源自给率等为目标的多目标优化模型,并采用先进的智能优化算法(如粒子群、灰狼、鲸鱼等)
进行
求解,从而确定光伏装机容量与储能系统配置的最优方案。文中提供了完整的Matlab代码
实现
流程,涵盖数据预处理、模型构建、算法求解与结果可视化全过程,属于综合能源系统与信息基础设施深度融合的前沿交叉课题。; 适合人群:具备电力系统、能源工程、自动化或计算机等相关专业背景,熟悉Matlab编程与基本优化算法原理,从事新能源利用、绿色数据中心、综合能源系统规划与低碳调度等方向的研究生、科研人员及工程技术人员。; 使用场景及目标:① 掌握光伏-储能-数据中心耦合系统的建模思路与多能流协同机制;② 学习基于Matlab的多目标容量优化配置方法与智能算法应用技巧;③ 为工业园区、数字经济园区及绿色数据中心的能源系统规划、节能减排与可持续运行提供科学决策依据和技术解决方案。; 阅读建议:建议结合所提供的Matlab代码,深入理解目标函数设计、约束条件设定及算法
实现
细节,可通过调整负荷参数、改变气候数据、引入新的优化目标或约束条件等方式
进行
拓展性实验,以深化对系统特性的认知并提升实际科研与工程应用能力。
2026年数学建模竞赛暑期培训通知(1).pdf_QQ浏览器文档瘦身.pdf
2026年数学建模竞赛暑期培训通知(1).pdf_QQ浏览器文档瘦身.pdf
Java SE
62,621
社区成员
307,257
社区内容
发帖
与我相关
我的任务
Java SE
Java 2 Standard Edition
复制链接
扫一扫
分享
社区描述
Java 2 Standard Edition
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章