📄 scrolldemo2.java
字号:
///////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2000 Davanum Srinivas (dims@geocities.com)
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along with
// this program; if not, write to the
// Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
///////////////////////////////////////////////////////////////////////////////
import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class ScrollDemo2 extends JPanel
{
private Dimension size; // indicates size taken up by graphics
private Vector objects1; // rectangular coordinates used to draw graphics
private Vector objects2; // rectangular coordinates used to draw graphics
private final Color colors[] = {
Color.red, Color.blue, Color.green, Color.orange,
Color.cyan, Color.magenta, Color.darkGray, Color.yellow};
private final int color_n = colors.length;
JPanel drawingArea1;
JPanel drawingArea2;
public ScrollDemo2()
{
setOpaque(true);
size = new Dimension(0,0);
objects1 = new Vector();
objects2 = new Vector();
//Set up the instructions.
JLabel instructionsLeft = new JLabel(
"Click left mouse button to place a circle.");
JLabel instructionsRight = new JLabel(
"Click right mouse button to clear drawing area.");
JPanel instructionPanel = new JPanel(new GridLayout(0,1));
instructionPanel.add(instructionsLeft);
instructionPanel.add(instructionsRight);
//Set up the drawing area.
drawingArea1 = new JPanel() {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Rectangle rect;
for (int i = 0; i < objects1.size(); i++) {
rect = (Rectangle)objects1.elementAt(i);
g.setColor(colors[(i % color_n)]);
g.fillOval(rect.x, rect.y, rect.width, rect.height);
}
}
};
drawingArea1.setBackground(Color.white);
drawingArea1.addMouseListener(new MyMouseListener(drawingArea1,objects1));
//Put the drawing area in a scroll pane.
JScrollPane scroller1 = new JScrollPane(drawingArea1);
scroller1.setPreferredSize(new Dimension(200,200));
//Set up the drawing area.
drawingArea2 = new JPanel() {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Rectangle rect;
for (int i = 0; i < objects2.size(); i++) {
rect = (Rectangle)objects2.elementAt(i);
g.setColor(colors[(i % color_n)]);
g.fillOval(rect.x, rect.y, rect.width, rect.height);
}
}
};
drawingArea2.setBackground(Color.white);
drawingArea2.addMouseListener(new MyMouseListener(drawingArea2,objects2));
//Put the drawing area in a scroll pane.
JScrollPane scroller2 = new JScrollPane(drawingArea2);
scroller2.setPreferredSize(new Dimension(200,200));
//Layout this demo.
setLayout(new BorderLayout());
add(instructionPanel, BorderLayout.NORTH);
add(scroller1, BorderLayout.EAST);
add(scroller2, BorderLayout.WEST);
}
class MyMouseListener extends MouseInputAdapter
{
final int W = 100;
final int H = 100;
JPanel drawingArea = null;
Vector objects = null;
public MyMouseListener(JPanel p, Vector v)
{
drawingArea = p;
objects = v;
}
public void mouseReleased(MouseEvent e)
{
boolean changed = false;
if (SwingUtilities.isRightMouseButton(e)) {
// This will clear the graphic objects.
objects.removeAllElements();
size.width=0;
size.height=0;
changed = true;
} else {
int x = e.getX() - W/2;
int y = e.getY() - H/2;
if (x < 0) x = 0;
if (y < 0) y = 0;
Rectangle rect = new Rectangle(x, y, W, H);
objects.addElement(rect);
drawingArea.scrollRectToVisible(rect);
int this_width = (x + W + 2);
if (this_width > size.width)
{size.width = this_width; changed=true;}
int this_height = (y + H + 2);
if (this_height > size.height)
{size.height = this_height; changed=true;}
}
if (changed) {
//Update client's preferred size because
//the area taken up by the graphics has
//gotten larger or smaller (if cleared).
drawingArea.setPreferredSize(size);
//Let the scroll pane know to update itself
//and its scrollbars.
drawingArea.revalidate();
}
drawingArea.repaint();
}
}
public static void main (String args[])
{
JFrameEx frame = new JFrameEx("ScrollDemo2");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
frame.setContentPane(new ScrollDemo2());
frame.pack();
frame.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -