2011-10-06 75 views
1

所以我有4个类,在我的绘画程序中,我试图将滚动条添加到画布上以便我可以滚动,当我将缓冲图像设置为桌面大小时,但JFrame只是我认为700像素左右,所以当你展开或调整大小时,你仍然在画布上。但我无法弄清楚如何添加一个JScrollPane到缓冲图像,所以我可以滚动。缓冲图像上的Java ScrollPane

// MAIN //

import javax.swing.SwingUtilities; 
import javax.swing.UIManager; 

public class Main 
{ 
    public static int choice; 

    public static void main(String[] args) 
    { 
      Board.getInstance(); 


    } 
} 

// // FRAME

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.MouseListener; 

import javax.swing.BoxLayout; 
import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 
import javax.swing.JScrollPane; 
import javax.swing.SwingUtilities; 
import javax.swing.UIManager; 

/** 
* 
* @author Calvin Moss 
*/ 
public class Board extends JFrame implements ActionListener 
{ 

    public static Board inst; 

    Board() 
    { 


     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     Container c = getContentPane(); 
     c.add(Menu.getInstance(), BorderLayout.NORTH); 
     getContentPane().add(Drawing.getInstance(), BorderLayout.CENTER); 

     Drawing.getInstance().add(new JScrollPane()); 

     setSize(1200, 800); 
     setBackground(Color.WHITE); 
     setVisible(true); 


     JMenuBar menuBar = new JMenuBar(); 
     setJMenuBar(menuBar); 
     JMenu help = new JMenu("Help"); 
     menuBar.add(help); 
     JMenuItem about = new JMenuItem("About"); 
     help.add(about); 
     about.addActionListener(this); 
    } 


public static Board getInstance() 
    { 
     if(inst == null) 
      inst = new Board(); 
     return inst; 
    } 

public void actionPerformed(ActionEvent e) { 
     if (e.getActionCommand().equals("About")) 
     { 
      JFrame about = new JFrame("About"); 
      about.setSize(300, 300); 
      JButton picture = new JButton(new ImageIcon("C:/Users/TehRobot/Desktop/Logo.png")); 
      about.add(picture); 
      about.setVisible(true); 
     } 

} 


} 

// MENUPANEL //

import javax.swing.BorderFactory; 
import javax.swing.BoxLayout; 
import javax.swing.ButtonGroup; 
import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JColorChooser; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 
import javax.swing.JPanel; 
import javax.swing.JRadioButton; 
import javax.swing.JScrollPane; 
import javax.swing.SwingUtilities; 
import javax.swing.UIManager; 
import javax.swing.UIManager.LookAndFeelInfo; 
import javax.swing.border.TitledBorder; 

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Component; 
import java.awt.Graphics2D; 
import java.awt.GridLayout; 
import java.awt.event.*; 
import java.awt.image.BufferedImage; 


/** 
* 
* @author Calvin Moss 
*/ 
public class Menu extends JPanel implements MouseListener, ActionListener{ 

    public static Color bgColor = null; 
    public static int stroke = 0; 
    public static Menu instance; 
    private JButton clear; 
    private JButton line; 
    private JButton color; 
    private JButton erase; 
    private JButton pen; 
    public static boolean once = true; 
    public static BufferedImage buf = new BufferedImage(50,25,2); 
    public static Graphics2D gr = buf.createGraphics(); 
    public static BufferedImage buf2 = new BufferedImage(50,25,2); 
    public static Graphics2D gr2 = buf2.createGraphics(); 
    public static BufferedImage buf3 = new BufferedImage(50,25,2); 
    public static Graphics2D gr3 = buf3.createGraphics(); 
    public static BufferedImage buf1 = new BufferedImage(50,25,2); 
    public static Graphics2D gr1 = buf1.createGraphics(); 
    Menu() 
    { 
     setBackground(Color.GRAY); 
     TitledBorder titledBorder = BorderFactory.createTitledBorder("Toolbox"); 
     setBorder(titledBorder); 


     try { 
      for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) { 
       if ("Nimbus".equals(info.getName())) { 
        UIManager.setLookAndFeel(info.getClassName()); 
        break; 
       } 
      } 
     } catch (Exception e) { 
      // If Nimbus is not available, you can set the GUI to another look and feel. 
     } 
; 


     clear = new JButton("Clear"); 
     clear.setActionCommand("Clear"); 
     clear.addActionListener(this); 

     pen = new JButton("Pen"); 
     pen.setActionCommand("Pen"); 
     pen.addActionListener(this); 

     erase = new JButton("Erase"); 
     erase.setActionCommand("e"); 
     erase.addActionListener(this); 

     color = new JButton("Color"); 
     color.setActionCommand("Color"); 
     color.addActionListener(this); 

     ImageIcon ir2 = new ImageIcon(buf3); 
     JButton emptyR = new JButton(ir2); 
     emptyR.addActionListener(this); 
     emptyR.setActionCommand("ER"); 

     ImageIcon ir1 = new ImageIcon(buf2); 
     JButton emptyO = new JButton(ir1); 
     emptyO.addActionListener(this); 
     emptyO.setActionCommand("EO"); 

     ImageIcon ir = new ImageIcon(buf); 
     JButton fillR = new JButton(ir); 
     fillR.addActionListener(this); 
     fillR.setActionCommand("FR"); 

     ImageIcon io = new ImageIcon(buf1); 
     JButton fillO = new JButton(io); 
     fillO.addActionListener(this); 
     fillO.setActionCommand("FO"); 


     line = new JButton("Line"); 
     line.setActionCommand("L"); 
     line.addActionListener(this); 


     JButton button6 = new JButton("Line"); 
     button6.addActionListener(this); 
     JRadioButton thin = new JRadioButton("Thin Line"); 
     thin.addActionListener(this); 
     JRadioButton medium = new JRadioButton("Medium Line"); 
     medium.addActionListener(this); 
     JRadioButton thick = new JRadioButton("Thick Line"); 
     thick.addActionListener(this); 

     ButtonGroup lineOption = new ButtonGroup(); 
     lineOption.add(thin); 
     lineOption.add(medium); 
     lineOption.add(thick); 

     add(clear); 
     add(erase); 
     add(color); 
     add(pen); 
     add(line); 
     add(thin); 
     add(medium); 
     add(thick); 
     add(emptyR); 
     add(emptyO); 
     add(fillR); 
     add(fillO); 
     buttonGraphics(); 
     once = false; 

    } 


    public static Menu getInstance() 
    { 
     if(instance == null) 
      instance = new Menu(); 
     return instance; 
    } 
    public static void buttonGraphics() 
    { 
     if (once == true) 
     { 
      gr.setColor(Color.RED); 
      gr1.setColor(Color.RED); 
      gr2.setColor(Color.RED); 
      gr3.setColor(Color.RED); 
     }else 

     gr3.setColor(bgColor); 
     gr3.drawRect(0, 0, 48, 23); 
     gr2.setColor(bgColor); 
     gr2.drawOval(0, 0, 47, 23); 
     gr.setColor(bgColor); 
     gr.fillRect(0, 0, 50, 23); 
     gr1.setColor(bgColor); 
     gr1.fillOval(0, 0, 50, 25); 

    } 

    public void actionPerformed(ActionEvent e) 
    { 
     if(e.getActionCommand().equals("Clear")) 
     { 
     Drawing.getInstance().clear(); 
     repaint(); 
     } 

    if (e.getActionCommand().equals("e")) 
    { 
     Main.choice = 8; 
     Drawing.getInstance().draw(); 
    } 
    if (e.getActionCommand().equals("Color")) 
     { 
     bgColor = JColorChooser.showDialog(this,"Choose Background Color", getBackground()); 
     gr.setColor(bgColor); 
     gr1.setColor(bgColor); 
     gr2.setColor(bgColor); 
     gr3.setColor(bgColor); 
     Main.choice = 7; 
     buttonGraphics(); 
     repaint(); 
     Drawing.getInstance().draw(); 
     } 
     if (e.getActionCommand().equals("L")) 
     { 
     Drawing.getInstance().FoE = 2; 
     Main.choice = 1; 
     repaint(); 
     } 
    if (e.getActionCommand().equals("FR")) 
     { 
     Drawing.getInstance().FoE = 2; 
     Main.choice = 2; 
     repaint(); 
     } 
    if (e.getActionCommand().equals("EO")) 
     { 
     Drawing.getInstance().FoE = 2; 
     Main.choice = 3; 
     repaint(); 
     } 
    if (e.getActionCommand().equals("FO")) 
     { 
     Drawing.getInstance().FoE = 2; 
     Main.choice = 4; 
     repaint(); 
     } 
    if(e.getActionCommand().equals("ER")) 
     { 
     Drawing.getInstance().FoE = 2; 
     Main.choice = 5; 

     repaint(); 
     } 
    if (e.getActionCommand().equals("Thin Line")) 
     { 
     stroke = 0; 
     Drawing.getInstance().eraserHeight = 5; 
     Drawing.getInstance().eraserWidth = 5; 
     } 

    if (e.getActionCommand().equals("Medium Line")) 
     { 
     stroke = 1; 
      Drawing.getInstance().eraserHeight = 15; 
      Drawing.getInstance().eraserWidth = 15; 
     } 

    if (e.getActionCommand().equals("Thick Line")) 
     { 
     stroke = 2; 
      Drawing.getInstance().eraserHeight = 25; 
      Drawing.getInstance().eraserWidth = 25; 
     } 
    if(e.getActionCommand().equals("Pen")) 
    { 

     Main.choice = 10; 
     Drawing.getInstance().draw(); 

    } 


} 

    public void mouseClicked(MouseEvent e){} 
    public void mouseEntered(MouseEvent e){} 
    public void mouseExited(MouseEvent e){} 
    public void mousePressed(MouseEvent e){} 
    public void mouseReleased(MouseEvent e){} 



} 

// 绘图画布面板 //

import java.awt.BasicStroke; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Toolkit; 

import javax.swing.JColorChooser; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 

import java.awt.event.*; 
import java.awt.image.BufferedImage; 


/** 
* 
* @author Calvin Moss 
*/ 
public class Drawing extends JPanel implements MouseListener, ActionListener, MouseMotionListener 
{ 
    public int x1, x2 ,y1, y2; 
    public static Drawing instance; 
    public int FoE; 
    public int eraserWidth = 15; 
    public int eraserHeight = 15; 

    BufferedImage grid; 
    static Graphics2D gc; 




    Drawing() 
    { 
     setBackground(Color.RED); 
     addMouseListener(this); 
    } 
    public static Drawing getInstance() 
    { 
     if(instance == null) 
      instance = new Drawing(); 
     return instance; 

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

     Graphics2D g2 = (Graphics2D)g; 
     if(grid == null) 
     { 
      Toolkit toolkit = Toolkit.getDefaultToolkit(); 
      Dimension dim = toolkit.getScreenSize(); 
      grid = (BufferedImage)(this.createImage(dim.width,dim.height)); 
      gc = grid.createGraphics(); 
      gc.setColor(Color.RED); 
      BufferedImage grid; 
      Graphics2D gc; 
     } 
     g2.drawImage(grid, null, 0, 0); 
    } 
public void draw() 
{ 
    Graphics2D g = (Graphics2D)getGraphics(); 
    int w = x2 - x1; 
    if (w<0) 
    w = w *(-1); 
    int h = y2-y1; 
    if (h<0) 
    h= h*(-1); 
    gc.setColor(Menu.bgColor); 
    switch(Main.choice) 
    { 
     case 1: 
     { 
      removeMouseMotionListener(instance); 
      if (Menu.stroke == 0) 
       gc.setStroke(new BasicStroke (1)); 
       if (Menu.stroke == 1) 
       gc.setStroke(new BasicStroke (3)); 
       if (Menu.stroke == 2) 
       gc.setStroke(new BasicStroke (6)); 
      gc.drawLine(x1, y1, x2, y2); 
      repaint(); 
      break; 
     } 
     case 2: 
     { 
      removeMouseMotionListener(instance); 
      check(); 
      gc.drawRect(x1, y1, w, h); 
      gc.fillRect(x1, y1, w, h); 
      repaint(); 
      break; 
     } 
     case 3: 
     { 
      removeMouseMotionListener(instance); 
      check(); 
      gc.drawOval(x1, y1, w, h); 
      repaint(); 
      break; 
     } 
     case 4: 
     { 
      removeMouseMotionListener(instance); 
      check(); 
      gc.drawOval(x1, y1, w, h); 
      gc.fillOval(x1, y1, w, h); 
      repaint(); 
      break; 
     } 
     case 5: 
     { 
      removeMouseMotionListener(instance); 
      check(); 
      gc.drawRect(x1, y1, w, h); 
      repaint(); 
      break; 
     } 
     case 6: 
     { 
      removeMouseMotionListener(instance); 
      repaint(); 
      Color temp = gc.getColor(); 
      gc.setColor(Color.WHITE); 
      gc.fillRect(0, 0, getWidth(), getHeight()); 
      gc.setColor(temp); 
      repaint(); 
      break; 
     } 
     case 7: 
     { 
      removeMouseMotionListener(instance); 
      gc.setColor(Menu.bgColor); 
      break; 
     } 
     case 8: 
     { 
      FoE = 0; 
      addMouseMotionListener(instance); 
      break; 
     } 
     case 10: 
     { 
      FoE = 1; 
      addMouseMotionListener(instance); 
      break; 
     } 

    } 
} 


    public void check() 
    { 
     if (Menu.stroke == 0) 
      gc.setStroke(new BasicStroke (1)); 
      if (Menu.stroke == 1) 
      gc.setStroke(new BasicStroke (3)); 
      if (Menu.stroke == 2) 
      gc.setStroke(new BasicStroke (6)); 


     if (x1 > x2) 
     { 
      int z = 0; 
      z = x1; 
      x1 = x2; 
      x2 =z; 
     } 
     if (y1 > y2) 
     { 
      int z = 0; 
      z = y1; 
      y1 = y2; 
      y2 = z; 
     } 
    } 

    public void clear() 
    { 
     repaint(); 
     Color temp = gc.getColor(); 
     gc.setColor(Color.WHITE); 
     gc.fillRect(0, 0, getWidth(), getHeight()); 
     gc.setColor(temp); 
     repaint(); 
    } 

    public void mouseExited(MouseEvent e){ } 
    public void mouseEntered(MouseEvent e){} 
    public void mouseClicked(MouseEvent e){ 
    } 

    public void mousePressed(MouseEvent evt) 
    { 
     x1 = evt.getX(); 
     y1= evt.getY(); 
    } 
    public void mouseReleased(MouseEvent evt) 
    { 
     x2 = evt.getX(); 
     y2 = evt.getY(); 
     removeMouseMotionListener(instance); 
     draw(); 
    } 


    public void mouseDragged(MouseEvent me) 
    { 
     check(); 
     if (FoE == 0) 
     { 

     Color c = gc.getColor(); 
     gc.setColor(Color.WHITE); 
     gc.drawOval(me.getX(), me.getY(), eraserWidth, eraserHeight); 
     gc.fillOval(me.getX(), me.getY(), eraserWidth, eraserHeight); 
     gc.setColor(c); 
     repaint(); 
     } 
     else if (FoE == 1) 
     { 
     gc.setColor(gc.getColor()); 
     gc.drawOval(me.getX(), me.getY(), eraserWidth, eraserHeight); 
     gc.fillOval(me.getX(), me.getY(), eraserWidth, eraserHeight); 
     repaint(); 
     } 
     else 
     { 

     } 
    } 

    public void mouseMoved(MouseEvent arg0) 
    { 

    } 
    public void actionPerformed(ActionEvent arg0) { 

    } 


} 

回答

4

我没有测试你的代码,但它看起来像你想添加添加JScrollPaneJPanel(图)。它应该是周围的其他方法,你应该在JPanel添加到JScrollPane,然后添加JScrollPane到帧的内容窗格:

JScrollPane scrollPane = new JScrollPane(Drawing.getInstance()); 
getContentPane().add(scrollPane, BorderLayout.CENTER); 

How to use JScrollPane

3

你需要覆盖的的getPreferredSize()方法您的Drawing类返回BufferedImages的大小。

仅当添加到滚动窗格的组件的首选大小大于滚动窗格的大小时才会出现滚动条。