Canvas仅通过最小化窗口并调整窗口大小来显示内容

时间:2015-03-13 14:37:26

标签: java java-canvas

我使用Canvas类将屏幕保护程序作为学校项目。 但是Canvas生成的窗口不显示我的对象(当前时间) 直到我最小化它再次调整大小。在那之后一切正常。 那有什么不对?

谢谢您的回答! 亲切的问候 利昂

那些是类,我认为问题出在Start或BufferedCanvas类中

import java.awt.*;                                           
import java.util.Date;                                                 
import  java.text.DateFormat;                                     
import java.text.SimpleDateFormat;                              
public class Start
{    
int fensterX = 900;                                        
int fensterY = 700;

Farbengenerator fg = new Farbengenerator();


BufferedCanvas c =newBufferedCanvas("Bild",fensterX,fensterY);         

Zeit z = new Zeit();                                          


SchriftParameter sp = new SchriftParameter();                  


public void zeichneText(){

    double x = 100;                                               
    double y = 100;

    double fy =0.01;                                              
    double fx =0.02;   

    int red=0;
    int green=0;
    int blue=0;
    double colourGrowRate=0.05;

    String uhr;                                                 

    c.setFont(sp.setzteSchrift());                                
    c.setForegroundColour(Color.BLACK);
    c.setBackgroundColour(Color.WHITE);

    for(int i=0;i<100;i++){
        c.drawString("Starting...",(int)x,(int)y);
        c.updateAndShow();
        try{Thread.sleep(50);}
        catch(Exception e){};
        c.updateAndShow();
    }

    CreateButton d = new CreateButton();
    d.run();

    while(true) {

       c.erase();                                                  

       uhr = z.erstelleZeit();                                    
       c.drawString(uhr,(int)x,(int)y);                          

       if((int)x >fensterX-93 || (int)x <5){                            
            fx = fx * (-1);
            red=fg.gibROT();
            green=fg.gibGRUEN();
            blue=fg.gibBLAU();
            Color colour = new Color(red,green,blue);
            c.setForegroundColour(colour); 
       }                                                            
       if((int)y  > fensterY-1 || (int)y < 46){                    
            fy = fy * (-1);

            red=fg.gibROT();
            green=fg.gibGRUEN();
            blue=fg.gibBLAU();

            Color colour = new Color(red,green,blue);
            c.setForegroundColour(colour); 
       } 

       if((int)colourGrowRate>=1){
           fg.generiereFarbe();
           colourGrowRate = 0.05;
       }

       colourGrowRate=colourGrowRate+colourGrowRate;

        x = x + fx;                                               
        y = y + fy;

         c.updateAndShow();                                   
    }
}

}

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferStrategy;

 public class BufferedCanvas
 {
 private JFrame frame;
 private CanvasPane canvas;
 private Graphics2D graphic;
 private Color backgroundColour;
 private Image canvasImage;
 BufferStrategy buff;

/**
 * Create a BufferedCanvas with default height, 
   width and background  colour
 * (300, 300, white).
 * @param title  title to appear in Canvas Frame
 */

public BufferedCanvas(String title)
{
    this(title, 300, 300, Color.white);        
}

/**
 * Create a BufferedCanvas with default background colour (white).
 * @param title  title to appear in Canvas Frame
 * @param width  the desired width for the canvas
 * @param height  the desired height for the canvas
 */
public BufferedCanvas(String title, int width, int height)
{
    this(title, width, height, Color.white);
}

/**
 * Create a BufferedCanvas.
 * @param title  title to appear in Canvas Frame
 * @param width  the desired width for the canvas
 * @param height  the desired height for the canvas
 * @param bgClour  the desired background colour of the canvas
 */
public BufferedCanvas(String title, int width, int height, Color bgColour)
{
    frame = new JFrame();
    canvas = new CanvasPane();
    frame.setContentPane(canvas);
    frame.setTitle(title);
    canvas.setPreferredSize(new Dimension(width, height));
    backgroundColour = bgColour;
    frame.pack();

    frame.createBufferStrategy(2);    
    buff = frame.getBufferStrategy();
    graphic = (Graphics2D)buff.getDrawGraphics();

    setVisible(true);

}

/**
 * Set the canvas visibility and brings canvas to the front of screen
 * when made visible. This method can also be used to bring an already
 * visible canvas to the front of other windows.
 * @param visible  boolean value representing the desired visibility of
 * the canvas (true or false) 
 */
public void setVisible(boolean visible)
{
    if(graphic == null) {
        // first time: instantiate the offscreen image and fill it with
        // the background colour
        Dimension size = canvas.getSize();
        canvasImage = canvas.createImage(size.width, size.height);
        graphic = (Graphics2D)canvasImage.getGraphics();
        graphic.setColor(backgroundColour);
        graphic.fillRect(0, 0, size.width, size.height);
        graphic.setColor(Color.black);
    }
    frame.setVisible(true);
}

/**
 * Update the canvas and show the new image.
 */   
public void updateAndShow(){
    buff.show();
}

/**
 * Provide information on visibility of the Canvas.
 * @return  true if canvas is visible, false otherwise
 */
public boolean isVisible()
{
    return frame.isVisible();
}

/**
 * Draw a given shape onto the canvas.
 * @param  shape  the shape object to be drawn on the canvas
 */
public void draw(Shape shape)
{
    graphic.draw(shape);
    //canvas.repaint();
}

/**
 * Fill the internal dimensions of a given shape with the current 
 * foreground colour of the canvas.
 * @param  shape  the shape object to be filled 
 */
public void fill(Shape shape)
{
    graphic.fill(shape);
    //canvas.repaint();
}

/**
 * Erase the whole canvas.
 */
public void erase()
{
    Color original = graphic.getColor();
    graphic.setColor(backgroundColour);
    Dimension size = canvas.getSize();
    graphic.fill(new Rectangle(0, 0, size.width, size.height));
    graphic.setColor(original);
    //canvas.repaint();
}

/**
 * Erase a given shape's interior on the screen.
 * @param  shape  the shape object to be erased 
 */
public void erase(Shape shape)
{
    Color original = graphic.getColor();
    graphic.setColor(backgroundColour);
    graphic.fill(shape);              // erase by filling background colour
    graphic.setColor(original);
    //canvas.repaint();
}

/**
 * Erases a given shape's outline on the screen.
 * @param  shape  the shape object to be erased 
 */
public void eraseOutline(Shape shape)
{
    Color original = graphic.getColor();
    graphic.setColor(backgroundColour);
    graphic.draw(shape);  // erase by drawing background colour
    graphic.setColor(original);
    //canvas.repaint();
}

/**
 * Draws an image onto the canvas.
 * @param  image   the Image object to be displayed 
 * @param  x       x co-ordinate for Image placement 
 * @param  y       y co-ordinate for Image placement 
 * @return  returns boolean value representing whether the image was 
 *          completely loaded 
 */
public boolean drawImage(Image image, int x, int y)
{
    boolean result = graphic.drawImage(image, x, y, null);
    //canvas.repaint();
    return result;
}

/**
 * Draws a String on the Canvas.
 * @param  text   the String to be displayed 
 * @param  x      x co-ordinate for text placement 
 * @param  y      y co-ordinate for text placement
 */
public void drawString(String text, int x, int y)
{
    graphic.drawString(text, x, y);   
    //canvas.repaint();
}

/**
 * Erases a String on the Canvas.
 * @param  text     the String to be displayed 
 * @param  x        x co-ordinate for text placement 
 * @param  y        y co-ordinate for text placement
 */
public void eraseString(String text, int x, int y)
{
    Color original = graphic.getColor();
    graphic.setColor(backgroundColour);
    graphic.drawString(text, x, y);   
    graphic.setColor(original);
    //canvas.repaint();
}

/**
 * Draws a line on the Canvas.
 * @param  x1   x co-ordinate of start of line 
 * @param  y1   y co-ordinate of start of line 
 * @param  x2   x co-ordinate of end of line 
 * @param  y2   y co-ordinate of end of line 
 */
public void drawLine(int x1, int y1, int x2, int y2)
{
    graphic.drawLine(x1, y1, x2, y2);   
    //canvas.repaint();
}

/**
 * Draws a dot/pixel on the Canvas.
 * @param  x   x co-ordinate of dot 
 * @param  y   y co-ordinate of dot 
 */
public void drawDot(int x, int y)
{
    graphic.drawLine(x, y, x, y);
    //canvas.repaint();
}

/**
 * Sets the foreground colour of the Canvas.
 * @param  newColour   the new colour for the foreground of the Canvas 
 */
public void setForegroundColour(Color newColour)
{
    graphic.setColor(newColour);
}

/**
 * Returns the current colour of the foreground.
 * @return   the colour of the foreground of the Canvas 
 */
public Color getForegroundColour()
{
    return graphic.getColor();
}

/**
 * Sets the background colour of the Canvas.
 * @param  newColour   the new colour for the background of the Canvas 
 */
public void setBackgroundColour(Color newColour)
{
    backgroundColour = newColour;   
    graphic.setBackground(newColour);
}

/**
 * Returns the current colour of the background
 * @return   the colour of the background of the Canvas 
 */
public Color getBackgroundColour()
{
    return backgroundColour;
}

/**
 * changes the current Font used on the Canvas
 * @param  newFont   new font to be used for String output
 */
public void setFont(Font newFont)
{
    graphic.setFont(newFont);
}

/**
 * Returns the current font of the canvas.
 * @return     the font currently in use
 **/
public Font getFont()
{
    return graphic.getFont();
}

/**
 * Sets the size of the canvas.
 * @param  width    new width 
 * @param  height   new height 
 */
public void setSize(int width, int height)
{
    canvas.setPreferredSize(new Dimension(width, height));
    Image oldImage = canvasImage;
    canvasImage = canvas.createImage(width, height);
    graphic = (Graphics2D)canvasImage.getGraphics();
    graphic.drawImage(oldImage, 0, 0, null);
    frame.pack();
}

/**
 * Returns the size of the canvas.
 * @return     The current dimension of the canvas
 */
public Dimension getSize()
{
    return canvas.getSize();
}

/**
 * Waits for a specified number of milliseconds before finishing.
 * This provides an easy way to specify a small delay which can be
 * used when producing animations.
 * @param  milliseconds  the number 
 */
public void wait(int milliseconds)
{
    try
    {
        Thread.sleep(milliseconds);
    } 
    catch (Exception e)
    {
        // ignoring exception at the moment
    }
}

/************************************************************************
 * Nested class CanvasPane - the actual canvas component contained in the
 * Canvas frame. This is essentially a JPanel with added capability to
 * refresh the image drawn on it.
 */
private class CanvasPane extends JPanel
{
    public void paint(Graphics g)
    {
        g.drawImage(canvasImage, 0, 0, null);
    }
}
}








  import javax.swing.*;
  import java.awt.*;  
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.awt.event.KeyEvent;
  public class CreateButton extends JFrame implements ActionListener{

    public void run() {
    createAndShowGUI();
 }

    public CreateButton() {

        // set layout for the frame
        this.getContentPane().setLayout(new FlowLayout());

        JButton button1 = new JButton();
        button1.setText("closeApp");

        //set actionlisteners for the buttons
        button1.addActionListener(this);

        // define a custom short action command for the button
        button1.setActionCommand("closeApp");



        // add buttons to frame
        add(button1);




    }

    private static void createAndShowGUI() {

         //Create and set up the window.

      JFrame frame = new CreateButton();

       //Display the window.

       frame.pack();

          frame.setVisible(true);

          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
    public void actionPerformed(ActionEvent ae) {
        String action = ae.getActionCommand();
        if (action.equals("closeApp")) {
            System.exit(1);
        }
   }
 }






  import java.awt.*;                                            
  public class SchriftParameter
 {   
 public Font setzteSchrift(){

    Font f = new Font("Fixed",1,24);         
    return (f);

}
}








  public class Farbengenerator
{
int r=0;
int g=0;
int b=0;
public void generiereFarbe(){

        if (r<255&&g==0&&b==0){
            r++;
        }
        else if (r==255&&g<255&&b==0){
            g++;
        }
        else if (r>0&&g==255&&b==0){
            r= r-1;
        }
        else if (r==0&&g==255&&b<255){
            b++;
        }
        else if (r==0&&g>0&&b==255){
            g=g-1;
        }
        else if (r<255&&g==0&&b==255){
            r++;
        }
        else if (r==255&&g==0&&b>0){
            b=b-1;
        }

    }

public int gibROT () {
   return(r);
}
public int gibGRUEN () {
   return(g);
}
public int gibBLAU () {
   return(b);
}

}








import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class Zeit
{
public String erstelleZeit(){
    DateFormat df = new SimpleDateFormat("HH:mm:ss");       
    Date d = new Date();                                    
    String uhr = df.format(d);                              
    return (uhr);                                          
}
}

0 个答案:

没有答案