2014-03-06 54 views
-1

好吧,所以我需要幫助改變這個滑塊的色調。我似乎無法弄清楚。請不要@覆蓋。我需要一些能夠在「準備就緒」中運行的東西。當滑塊返回到0時,色調將恢復正常。我不需要太複雜。只是一個簡單的色相滑塊將是偉大的。謝謝!改變圖像的色調

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import java.applet.Applet; 
import javax.swing.event.*; 
import java.applet.*; 

public class test extends Applet implements ActionListener, ChangeListener 
{ 

    //Widgets, Panels 
    JSlider slider; 
    Panel flow; 
    int colorr; 
    int colorg; 
    int colorb; 
    int stars; 
    //House Coordinates, initialized to 1. (Top Right and No Scaling) 


    public void init() 
    { //Set Up Input Fields for House Coordinates 
     resize (380, 240); 
     setBackground (new Color (102, 179, 255)); 

     slider = new JSlider(); 
     slider.setValue (0); 
     slider.setBackground (new Color (102, 179, 255)); 
     slider.setForeground (Color.white); 
     slider.setMajorTickSpacing (20); 
     slider.setMinorTickSpacing (5); 
     slider.setPaintTicks (true); 
     slider.addChangeListener (this); 


     //Set up layout, add widgets 
     setLayout (new BorderLayout()); 
     flow = new Panel (new FlowLayout()); 
     flow.add (slider); 
     add (flow, "South"); 

    } 


    public void paint (Graphics g) 
    { 
     Graphics2D g2d = (Graphics2D) g; 
     Color color1 = getBackground(); 
     Color color2 = color1.darker(); 
     int x = getWidth(); 
     int y = getHeight() - 30; 
     GradientPaint gp = new GradientPaint (
       0, 0, color1, 
       0, y, color2); 

     g2d.setPaint (gp); 
     g2d.fillRect (0, 0, x, y); 

     stars (10, 10); 
    } 


    public void stars (int x, int y) 
    { 
     Graphics g = getGraphics(); 

     //sun 
     g.setColor (new Color (139, 166, 211)); 
     g.fillOval (-200, 170, 1000, 400); 

     g.setColor (new Color (206, 75, 239)); 
     g.fillOval (x, y, 10, 10);      //First medium star 
     g.drawLine (x + 5, y, x + 5, 0); 
     g.drawLine (x, y + 5, 0, y + 5); 
     g.drawLine (x + 5, y + 10, x + 5, y + 20); 
     g.drawLine (x + 10, y + 5, x + 20, y + 5); 

     g.fillOval (x + 80, y + 30, 12, 12);   //Middle medium star 
     g.drawLine (x + 86, y + 30, x + 86, y + 18); 
     g.drawLine (x + 80, y + 36, x + 68, y + 36); 
     g.drawLine (x + 92, y + 36, x + 104, y + 36); 
     g.drawLine (x + 86, y + 42, x + 86, y + 52); 

     colorr = (int) (Math.random() * 255) + 1; 
     colorg = (int) (Math.random() * 255) + 1; 
     colorb = (int) (Math.random() * 255) + 1; 
     int randomx = (int) (Math.random() * 300) + 10; 
     int randomy = (int) (Math.random() * 150) + 10; 

     stars = 50; //Change for more stars 

     int ax[] = new int [stars]; 
     int ay[] = new int [stars]; 

     for (int i = 0 ; i < stars ; i++) 
     { 
      g.setColor (new Color (colorr, colorg, colorb)); 
      colorr = (int) (Math.random() * 255) + 1; 
      colorg = (int) (Math.random() * 255) + 1; 
      colorb = (int) (Math.random() * 255) + 1; 

      while ((randomx > 88 && randomx < 116) && (randomy < 65 && randomy > 15)) 
      { 
       randomx = (int) (Math.random() * 300) + 10; 
       randomy = (int) (Math.random() * 150) + 10; 
      } 

      while ((randomx > 0 && randomx < 25) && (randomy > 5 && randomy < 35)) 
      { 
       randomx = (int) (Math.random() * 300) + 10; 
       randomy = (int) (Math.random() * 150) + 10; 
      } 
      g.drawOval (randomx, randomy, 5, 5); 
      randomx = (int) (Math.random() * 300) + 10; 
      randomy = (int) (Math.random() * 150) + 10; 
     } 

     g.setColor (Color.white); 
     g.drawLine (320, 0, 315, 40); 
     g.drawLine (320, 0, 325, 40); 
     g.drawLine (320, 120, 315, 80); 
     g.drawLine (320, 120, 325, 80); 
     g.drawLine (260, 60, 300, 55); 
     g.drawLine (260, 60, 300, 65); 
     g.drawLine (380, 60, 340, 55); 
     g.drawLine (380, 60, 340, 65); 
     fillGradOval (280, 20, 80, 80, new Color (254, 238, 44), new Color (255, 251, 191), g); 

     g.setColor (new Color (255, 251, 191)); 
     fillGradOval (300, 40, 40, 40, new Color (255, 251, 191), new Color (254, 238, 44), g); 
    } 


    public void fillGradOval (int X, int Y, int H2, int W2, Color c1, Color c2, Graphics g) 
    { 
     g.setColor (c1); 
     g.fillOval (X, Y, W2, H2); 
     Color Gradient = c1; 
     float red = (c2.getRed() - c1.getRed())/(W2/2); 
     float blue = (c2.getBlue() - c1.getBlue())/(W2/2); 
     float green = (c2.getGreen() - c1.getGreen())/(W2/2); 

     int scale = 1; 
     int r = c1.getRed(); 
     int gr = c1.getGreen(); 
     int b = c1.getBlue(); 

     while (W2 > 10) 
     { 
      r = (int) (r + red); 
      gr = (int) (gr + green); 
      b = (int) (b + blue); 

      Gradient = new Color (r, gr, b); 
      g.setColor (Gradient); 

      W2 = W2 - 2 * scale; 
      H2 = H2 - 2 * scale; 
      X = X + scale; 
      Y = Y + scale; 

      g.fillOval (X, Y, W2, H2); 
     } 
    } 


    public void actionPerformed (ActionEvent e) 
    { 

    } 


    public void stateChanged (ChangeEvent e) 
    { 
     JSlider source = (JSlider) e.getSource(); 
     if (!source.getValueIsAdjusting()) 
     { 

     } 
    } 
} 
+0

* 「請不@override。」 *您已經例如覆蓋方法。 *「我需要一些能夠運行在即可編程的東西。」*那是什麼? –

+0

1)爲什麼要編寫一個小程序?如果這是由於規格。由老師,請參考[爲什麼CS老師應該停止教Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/)。 2)不要沒有充分的理由混合Swing和AWT。所以'Applet' - >'JApplet'。 'Panel' - >'JPanel' .. –

+0

不要破壞你自己的問題,並且不要破壞我的答案!**如果你的答案有問題,請在評論中註明。如果問題需要澄清,請隨時編輯它,但要做出明智的編輯! –

回答

5

我不確定你指的是什麼'顏色',所以我做了一些猜測。這是一個混合應用程序/ applet(開發和測試更容易),它將底部面板的顏色以及漸變顏色的底部顏色與使用滑塊定義的色調相關聯。

enter image description here enter image description here enter image description here

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

/* <applet code=HueSlider width=380 height=240></applet> */ 
public class HueSlider extends JApplet 
{ 
    public void init() { 
     add(new HueSliderGui()); 
    } 

    public static void main(String[] args) { 
     Runnable r = new Runnable() { 
      @Override 
      public void run() { 
       HueSliderGui hsg = new HueSliderGui(); 
       JOptionPane.showMessageDialog(null, hsg); 
      } 
     }; 
     SwingUtilities.invokeLater(r); 
    } 
} 

class HueSliderGui extends JPanel implements ChangeListener { 

    //Widgets, Panels 
    JSlider slider; 
    JPanel flow; 
    int colorr; 
    int colorg; 
    int colorb; 
    Color bg = new Color (102, 179, 255); 
    int stars; 
    //House Coordinates, initialized to 1. (Top Right and No Scaling) 
    Dimension prefSize = new Dimension(380, 240); 

    HueSliderGui() { 
     initGui(); 
    } 

    public void initGui() 
    { 
     //Set Up Input Fields for House Coordinates 
     // an applet size is set in HTML 
     //resize (380, 240); 
     setBackground (bg); 

     slider = new JSlider(); 
     slider.setValue (0); 
     slider.setBackground (new Color (102, 179, 255)); 
     slider.setForeground (Color.white); 
     slider.setMajorTickSpacing (20); 
     slider.setMinorTickSpacing (5); 
     slider.setPaintTicks (true); 
     slider.addChangeListener (this); 


     //Set up layout, add widgets 
     setLayout (new BorderLayout()); 
     flow = new JPanel (new FlowLayout()); 
     flow.add (slider); 
     add (flow, "South"); 
     validate(); 
    } 

    @Override 
    public Dimension getPreferredSize() { 
     return prefSize; 
    } 


    @Override 
    public void paintComponent(Graphics g) 
    { 
     super.paintComponent(g); 
     Graphics2D g2d = (Graphics2D) g; 
     Color color1 = getBackground(); 
     Color color2 = color1.darker(); 
     int x = getWidth(); 
     int y = getHeight() - 30; 
     GradientPaint gp = new GradientPaint (
       0, 0, color1, 
       0, y, flow.getBackground()); 

     g2d.setPaint (gp); 
     g2d.fillRect (0, 0, x, y); 

     stars (10, 10, g2d); 
    } 

    public void stars (int x, int y, Graphics g) 
    { 
//  Graphics g = getGraphics(); we should never call getGraphics 

     //sun 
     g.setColor (new Color (139, 166, 211)); 
     g.fillOval (-200, 170, 1000, 400); 

     g.setColor (new Color (206, 75, 239)); 
     g.fillOval (x, y, 10, 10);      //First medium star 
     g.drawLine (x + 5, y, x + 5, 0); 
     g.drawLine (x, y + 5, 0, y + 5); 
     g.drawLine (x + 5, y + 10, x + 5, y + 20); 
     g.drawLine (x + 10, y + 5, x + 20, y + 5); 

     g.fillOval (x + 80, y + 30, 12, 12);   //Middle medium star 
     g.drawLine (x + 86, y + 30, x + 86, y + 18); 
     g.drawLine (x + 80, y + 36, x + 68, y + 36); 
     g.drawLine (x + 92, y + 36, x + 104, y + 36); 
     g.drawLine (x + 86, y + 42, x + 86, y + 52); 

     colorr = (int) (Math.random() * 255) + 1; 
     colorg = (int) (Math.random() * 255) + 1; 
     colorb = (int) (Math.random() * 255) + 1; 
     int randomx = (int) (Math.random() * 300) + 10; 
     int randomy = (int) (Math.random() * 150) + 10; 

     stars = 50; //Change for more stars 

     int ax[] = new int [stars]; 
     int ay[] = new int [stars]; 

     for (int i = 0 ; i < stars ; i++) 
     { 
      g.setColor (new Color (colorr, colorg, colorb)); 
      colorr = (int) (Math.random() * 255) + 1; 
      colorg = (int) (Math.random() * 255) + 1; 
      colorb = (int) (Math.random() * 255) + 1; 

      while ((randomx > 88 && randomx < 116) && (randomy < 65 && randomy > 15)) 
      { 
       randomx = (int) (Math.random() * 300) + 10; 
       randomy = (int) (Math.random() * 150) + 10; 
      } 

      while ((randomx > 0 && randomx < 25) && (randomy > 5 && randomy < 35)) 
      { 
       randomx = (int) (Math.random() * 300) + 10; 
       randomy = (int) (Math.random() * 150) + 10; 
      } 
      g.drawOval (randomx, randomy, 5, 5); 
      randomx = (int) (Math.random() * 300) + 10; 
      randomy = (int) (Math.random() * 150) + 10; 
     } 

     g.setColor (Color.white); 
     g.drawLine (320, 0, 315, 40); 
     g.drawLine (320, 0, 325, 40); 
     g.drawLine (320, 120, 315, 80); 
     g.drawLine (320, 120, 325, 80); 
     g.drawLine (260, 60, 300, 55); 
     g.drawLine (260, 60, 300, 65); 
     g.drawLine (380, 60, 340, 55); 
     g.drawLine (380, 60, 340, 65); 
     fillGradOval (280, 20, 80, 80, new Color (254, 238, 44), new Color (255, 251, 191), g); 

     g.setColor (new Color (255, 251, 191)); 
     fillGradOval (300, 40, 40, 40, new Color (255, 251, 191), new Color (254, 238, 44), g); 
    } 

    public void fillGradOval (int X, int Y, int H2, int W2, Color c1, Color c2, Graphics g) 
    { 
     g.setColor (c1); 
     g.fillOval (X, Y, W2, H2); 
     Color Gradient = c1; 
     float red = (c2.getRed() - c1.getRed())/(W2/2); 
     float blue = (c2.getBlue() - c1.getBlue())/(W2/2); 
     float green = (c2.getGreen() - c1.getGreen())/(W2/2); 

     int scale = 1; 
     int r = c1.getRed(); 
     int gr = c1.getGreen(); 
     int b = c1.getBlue(); 

     while (W2 > 10) 
     { 
      r = (int) (r + red); 
      gr = (int) (gr + green); 
      b = (int) (b + blue); 

      Gradient = new Color (r, gr, b); 
      g.setColor (Gradient); 

      W2 = W2 - 2 * scale; 
      H2 = H2 - 2 * scale; 
      X = X + scale; 
      Y = Y + scale; 

      g.fillOval (X, Y, W2, H2); 
     } 
    } 

    public void stateChanged (ChangeEvent e) 
    { 
     JSlider source = (JSlider) e.getSource(); 
     if (!source.getValueIsAdjusting()) 
     { 
      int i = source.getValue(); 
      System.out.println(i); 
      float[] hsb = Color.RGBtoHSB(bg.getRed(),bg.getGreen(),bg.getBlue(),null); 
      int colorHue = Color.HSBtoRGB((float)i/100f, hsb[1], hsb[2]); 
      Color c = new Color(colorHue); 
      flow.setBackground(c); 
      this.repaint(); 
     } 
    } 
} 
+2

知道你是藝術家_too_ :) –

+0

@peeskillet哦,不!該圖像是在原始代碼中生成的。我只改變了漸變底色的色調。對於我的藝術努力,請參閱[這個答案](http://stackoverflow.com/a/12216139/418556)(這讓我想起了一個不是藝術大師的孩子的手指畫),或[這個] (http://stackoverflow.com/a/20799711/418556)(確定這是一個真正的數學公式..但我選擇了顏色!)。 –

+0

請刪除評論。請。我求求你。 – user3386236