难难难!Graphics2D搞死我了

bgold 2000-07-05 09:00:00
将一个Component子类对象A放进JScrollPane中如果A是使用Graphics2D画图且使用了setTransform()方法,则JScrollPane不能正常工作。

各路好汉 help me !!!!!
...全文
187 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
bgold 2000-07-11
  • 打赏
  • 举报
回复
我是做transform但java教程中的例子没有将其包含有JScrollPane中
不过我已有办法解决了,就是用transform()替代setTransform().
Jackzhu 2000-07-11
  • 打赏
  • 举报
回复
你难道不是做的变形转换吗?java教程中有例子。
bgold 2000-07-10
  • 打赏
  • 举报
回复
这样不行,setTransform跟本没起作用
Jackzhu 2000-07-07
  • 打赏
  • 举报
回复
class ImproperPanel extends JPanel
{
boolean firstTime = true;

public ImproperPanel()
{
setPreferredSize(new Dimension(800,600));
}

public void paintComponent(Graphics g)
{
super.paintComponent(g);

Graphics2D g2d = (Graphics2D)g;

if (firstTime) {
firstTime = false;
//*********************************************************
g2d.setTransform(AffineTransform.getScaleInstance(1f,1f)); //The reason about the problem!!!
//*********************************************************
}
g2d.fillRect(100,100,100,100);
}
}
bgold 2000-07-06
  • 打赏
  • 举报
回复
写错了!是JComponent子类, 而不是Component子类
补充一个例子:

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

public class Ex extends JApplet implements ActionListener
{
private JButton proper = new JButton("Proper");
private JButton improper = new JButton("Improper");
private JPanel ButtonPanel = new JPanel();
private JFrame properFrame;
private JFrame improperFrame;

public void init()
{

properFrame = new MyFrame("Proper");
improperFrame = new MyFrame("Improper");
proper.addActionListener(this);
improper.addActionListener(this);
getContentPane().add(ButtonPanel);

ButtonPanel.add(proper);
ButtonPanel.add(improper);
}
public void actionPerformed(ActionEvent evt)
{
Object Source = evt.getSource();
if (Source == proper)
{
if(!properFrame.isVisible())
properFrame.show();
}
else if (Source == improper)
{
if(!improperFrame.isVisible())
improperFrame.show();
}

}

}

class ProperPanel extends JPanel
{
public ProperPanel()
{
setPreferredSize(new Dimension(800,600));
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);

Graphics2D g2d = (Graphics2D)g;
g2d.fillRect(100,100,100,100);
}
}

class ImproperPanel extends JPanel
{
public ImproperPanel()
{
setPreferredSize(new Dimension(800,600));
}

public void paintComponent(Graphics g)
{
super.paintComponent(g);

Graphics2D g2d = (Graphics2D)g;

//*********************************************************
g2d.setTransform(AffineTransform.getScaleInstance(1f,1f)); //The reason about the problem!!!
//*********************************************************

g2d.fillRect(100,100,100,100);
}
}

class MyFrame extends JFrame
{
private JPanel MyPanel;
private JScrollPane sp;

public MyFrame(String cl)
{

setSize(500,400);

if (cl == "Proper")
{
setTitle("Proper frame");
MyPanel = new ProperPanel();
}
else if(cl == "Improper")
{
setTitle("Improper frame");
MyPanel = new ImproperPanel();
}
sp = new JScrollPane(MyPanel);
getContentPane().add(sp,"Center");
}
}

62,614

社区成员

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

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