高分求swing中怎么样实现图片的伸缩

hxchawk_jcc 2003-11-30 05:11:03
高分求swing中怎么样实现图片的伸缩,并且要求图片大时能在1个pane中显示,有上下左右scrollbar.
...全文
50 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
binbin2000 2003-12-01
  • 打赏
  • 举报
回复
注意看jdk中带的原码。
icebound1 2003-12-01
  • 打赏
  • 举报
回复
mark
hxchawk_jcc 2003-12-01
  • 打赏
  • 举报
回复
怎样把jScrollPane1 中的边筐去掉呀,缩小的话显示边筐很难看的
longbaduo 2003-11-30
  • 打赏
  • 举报
回复
学习中...
zengruicfan 2003-11-30
  • 打赏
  • 举报
回复
学习中~
现在我有源代码~以后看懂了再告诉你
xjffj 2003-11-30
  • 打赏
  • 举报
回复
以下是我修改后的代码。
import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
import java.awt.event.*;
import javax.swing.event.*;

public class TestImage
extends JFrame {
Image image1 = Toolkit.getDefaultToolkit().getImage(
"E:\\相片\\618017.jpeg");
// Image image2 = Toolkit.getDefaultToolkit().getImage(
// "D:\\airport\\images\\plane1.jpg");
// Image image2 = Toolkit.getDefaultToolkit().getImage(
// "D:\\airport\\images\\airB.gif");

JLabel lbl_peirihiteyi = new JLabel();
JLabel lbl_peri = new JLabel();
JLabel lbl_periseteyi = new JLabel();

JSlider sld_peri = new JSlider();
JTextField txt_peri = new JTextField();
JToggleButton btn_cancel = new JToggleButton();

JScrollPane jScrollPane1 = new JScrollPane();
DrawPicturePanel custom_pane = new DrawPicturePanel();

public TestImage() {
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}

sld_peri.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {

double peri = sld_peri.getValue()/10.0;
txt_peri.setText("" + peri);
//peri = peri/10.0;
System.out.println("changed-----------");
custom_pane = new DrawPicturePanel( (int)(320 * peri), (int)(40 * peri));
jScrollPane1.setViewportView(custom_pane);
}
}
);
}

private void jbInit() throws Exception {

lbl_peirihiteyi.setFont(new java.awt.Font("Dialog", 0, 16));
lbl_peirihiteyi.setVerifyInputWhenFocusTarget(true);
lbl_peirihiteyi.setText("倍率指定");
lbl_peirihiteyi.setBounds(new Rectangle(48, 368, 75, 39));
sld_peri.setMajorTickSpacing(1);
sld_peri.setValue(10);
sld_peri.setMaximum(12);
sld_peri.setMinimum(8);
sld_peri.setMinorTickSpacing(0);
sld_peri.setPaintLabels(false);
sld_peri.setPaintTicks(true);
sld_peri.setBorder(null);
sld_peri.setMinimumSize(new Dimension(36, 40));
sld_peri.setOpaque(true);
sld_peri.setBounds(new Rectangle(126, 376, 113, 28));
lbl_periseteyi.setEnabled(true);
lbl_periseteyi.setFont(new java.awt.Font("Dialog", 0, 16));
lbl_periseteyi.setText("倍率设定");
lbl_periseteyi.setBounds(new Rectangle(246, 382, 67, 15));
txt_peri.setText("1.0");
txt_peri.setBounds(new Rectangle(319, 380, 55, 22));

btn_cancel.setText("消去");
btn_cancel.setBounds(new Rectangle(327, 414, 72, 40));
lbl_peri.setFont(new java.awt.Font("Dialog", 0, 16));
lbl_peri.setText("倍");
lbl_peri.setVerticalAlignment(SwingConstants.CENTER);
lbl_peri.setBounds(new Rectangle(388, 379, 41, 26));
this.setResizable(false);

jScrollPane1.setBorder(null);
//jScrollPane1.getViewport().setBackground(Color.lightGray);
//jScrollPane1.setAlignmentX( (float) 0.5);
jScrollPane1.setBounds(new Rectangle(40, 40, 320, 320));
jScrollPane1.setViewportView(custom_pane);
//jScrollPane1.setVerticalScrollBarPolicy(1);

this.getContentPane().add(jScrollPane1, null);
this.getContentPane().add(lbl_peirihiteyi, null);
this.getContentPane().add(sld_peri, null);
this.getContentPane().add(btn_cancel, null);
this.getContentPane().add(lbl_periseteyi, null);
this.getContentPane().add(txt_peri, null);
this.getContentPane().add(lbl_peri, null);



this.setTitle("");
this.getContentPane().setLayout(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}


class DrawPicturePanel
extends JScrollPane {

int width1;
int width2;
public DrawPicturePanel() {
width1 = 320;
width2 = 40;
}

public DrawPicturePanel(int width1, int width2) {
this.width1 = width1;
this.width2 = width2;
repaint();
}

public void paint(Graphics g) {
super.paint(g);
System.out.println("width1==" + width1);
// System.out.println("width2==" + width2);
g.drawImage(image1, 0, 0, width1, width1, this);
// g.drawImage(image2, width1/2, width1/2, width2, width2, this);
}
//重载此方法后就可以了
public Dimension getPreferredSize()
{
return new Dimension(width1, width1);
}

}

public static void main(String args[]) {
TestImage kakudai1 = new TestImage();
kakudai1.setSize(450, 450);
kakudai1.show();

}


}
hxchawk_jcc 2003-11-30
  • 打赏
  • 举报
回复
这是我的程序,能实现图片的伸缩,但显示不了上下左右scrollbar.

package nxworks.jpcom.net.util;

import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
import java.awt.event.*;
import javax.swing.event.*;

public class Kakudai1
extends JFrame
{
Image image1 = Toolkit.getDefaultToolkit().getImage(
"D:\\airport\\images\\enlarge1.jpg");
// Image image2 = Toolkit.getDefaultToolkit().getImage(
// "D:\\airport\\images\\plane1.jpg");
// Image image2 = Toolkit.getDefaultToolkit().getImage(
// "D:\\airport\\images\\airB.gif");

JLabel lbl_peirihiteyi = new JLabel();
JLabel lbl_peri = new JLabel();
JLabel lbl_periseteyi = new JLabel();

JSlider sld_peri = new JSlider();
JTextField txt_peri = new JTextField();
JToggleButton btn_cancel = new JToggleButton();

JScrollPane jScrollPane1 = new JScrollPane();
DrawPicturePanel custom_pane = new DrawPicturePanel();

public Kakudai1()
{
try
{
jbInit();
}
catch (Exception e)
{
e.printStackTrace();
}

sld_peri.addChangeListener(new ChangeListener()
{
public void stateChanged(ChangeEvent e)
{

double peri = sld_peri.getValue()/10.0;
txt_peri.setText("" + peri);
//peri = peri/10.0;
System.out.println("changed-----------");
custom_pane = new DrawPicturePanel( (int)(320 * peri), (int)(40 * peri));
jScrollPane1.getViewport().add(custom_pane, null);
}
}
);
}

private void jbInit() throws Exception
{

lbl_peirihiteyi.setFont(new java.awt.Font("Dialog", 0, 16));
lbl_peirihiteyi.setVerifyInputWhenFocusTarget(true);
lbl_peirihiteyi.setText("倍率指定");
lbl_peirihiteyi.setBounds(new Rectangle(48, 368, 75, 39));
sld_peri.setMajorTickSpacing(1);
sld_peri.setValue(10);
sld_peri.setMaximum(12);
sld_peri.setMinimum(8);
sld_peri.setMinorTickSpacing(0);
sld_peri.setPaintLabels(false);
sld_peri.setPaintTicks(true);
sld_peri.setBorder(null);
sld_peri.setMinimumSize(new Dimension(36, 40));
sld_peri.setOpaque(true);
sld_peri.setBounds(new Rectangle(126, 376, 113, 28));
lbl_periseteyi.setEnabled(true);
lbl_periseteyi.setFont(new java.awt.Font("Dialog", 0, 16));
lbl_periseteyi.setText("倍率设定");
lbl_periseteyi.setBounds(new Rectangle(246, 382, 67, 15));
txt_peri.setText("1.0");
txt_peri.setBounds(new Rectangle(319, 380, 55, 22));

btn_cancel.setText("消去");
btn_cancel.setBounds(new Rectangle(327, 414, 72, 40));
lbl_peri.setFont(new java.awt.Font("Dialog", 0, 16));
lbl_peri.setText("倍");
lbl_peri.setVerticalAlignment(SwingConstants.CENTER);
lbl_peri.setBounds(new Rectangle(388, 379, 41, 26));
this.setResizable(false);

jScrollPane1.setBorder(null);
//jScrollPane1.getViewport().setBackground(Color.lightGray);
//jScrollPane1.setAlignmentX( (float) 0.5);
jScrollPane1.setBounds(new Rectangle(40, 40, 320, 320));
jScrollPane1.getViewport().add(custom_pane, null);
//jScrollPane1.setVerticalScrollBarPolicy(1);

this.getContentPane().add(jScrollPane1, null);
this.getContentPane().add(lbl_peirihiteyi, null);
this.getContentPane().add(sld_peri, null);
this.getContentPane().add(btn_cancel, null);
this.getContentPane().add(lbl_periseteyi, null);
this.getContentPane().add(txt_peri, null);
this.getContentPane().add(lbl_peri, null);



this.setTitle("");
this.getContentPane().setLayout(null);
}


class DrawPicturePanel
extends JScrollPane
{

int width1;
int width2;
public DrawPicturePanel()
{
width1 = 320;
width2 = 40;
}

public DrawPicturePanel(int width1, int width2)
{
this.width1 = width1;
this.width2 = width2;
repaint();
}

public void paint(Graphics g)
{
System.out.println("width1==" + width1);
// System.out.println("width2==" + width2);
g.drawImage(image1, 0, 0, width1, width1, this);
// g.drawImage(image2, width1/2, width1/2, width2, width2, this);
}

}

public static void main(String args[])
{
Kakudai1 kakudai1 = new Kakudai1();
kakudai1.setSize(450, 450);
kakudai1.show();

}


}

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧