我正在处理像素艺术编辑器,我遇到了JScrollBar
的问题。
问题是:
我在滚动条轨道上渲染图像,但它每次都会拉伸,所以我必须绘制超过30个单独的滚动条轨道图像,我想知道是否可以创建一个图像并使其尺寸合适
package oktay.notepad.GUI;
import oktay.notepad.ColorSheme;
import oktay.notepad.Main;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.plaf.basic.BasicScrollBarUI;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
/**
* Created by asame on 08.06.2017.
*/
public class UIScrollBar extends BasicScrollBarUI implements MouseListener {
public UIScrollBar() {
}
@Override
protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds) {
try {
g.drawImage(ImageIO.read(ClassLoader.getSystemResource("res/icons/scrolltrack.pn g")), trackBounds.x, trackBounds.y, trackBounds.width, trackBounds.height, null);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) {
super.paintThumb(g, c, thumbBounds);
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
---------------------- CustomScrollBar类----------------------- -----
package oktay.notepad.GUI;
import javax.swing.*;
/**
* Created by asame on 21.06.2017.
*/
public class CustomScrollBar extends JScrollBar {
public CustomScrollBar() {
setUI(new UIScrollBar());
}
}
---------------------- CustomScrollPane类----------------------- ---
package oktay.notepad.GUI;
import javax.swing.*;
import java.awt.*;
/**
* Created by asame on 21.06.2017.
*/
public class CustomScrollPane extends JScrollPane {
public CustomScrollPane () {
super (VERTICAL_SCROLLBAR_ALWAYS, HORIZONTAL_SCROLLBAR_ALWAYS);
//setUI(new UIScrollPane());
setHorizontalScrollBar(new CustomScrollBar());
setVerticalScrollBar(new CustomScrollBar());
}
}
感谢您的帮助
答案 0 :(得分:0)
您可以使用TexturePaint
,如下所示:
BufferedImage bi = ImageIO.read(ClassLoader.getSystemResource("res/icons/scrolltrack.png"));
TexturePaint paint = new TexturePaint(bi, new Rectangle(0, 0, bi.getWidth(), bi.getHeight()));
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(paint);
g2.fill(new Rectangle(trackBounds.x, trackBounds.y, trackBounds.width, trackBounds.height));