Создать прямоугольник, залитый градиентом - Java
Формулировка задачи:
Всем здравствуйте! есть такой код. можете подсказать как сделать его градиентным? и в координатах hsv(хотя бы и без них).
Или же можно сделать не сколько фигур с разным цветом. пытался сделать так и так
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author MD
*/
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;
public class DrawSquare extends javax.swing.JFrame {
public DrawSquare(String title) throws HeadlessException {
super(title);
DPanel DPanel = new DPanel();
getContentPane().add(DPanel);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();}
/**
* Creates new form DrawSquare
* @param args
*/
public static void main(String[] args) {
DrawSquare ds = new DrawSquare("MD");
ds.setVisible(true);
}
class DPanel extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int index=0 ;
Color[] colors = { Color.yellow, Color.cyan, Color.orange, Color.pink,
Color.magenta, Color.lightGray };
for (int i= 0; i < 7; i ++ ) {
Graphics2D g2d = (Graphics2D) g ;
// g2d.setColor(Color.blue);
//g2d.setStroke(new BasicStroke(6));
Rectangle2D rect = (new Rectangle2D.Double(10 , 10, 99, 99));
Rectangle2D fill = (new Rectangle2D.Double(10 , 10, index, 99));
// int index = (45) / 45 % colors.length;
// g2d.setPaint(Color.length);
g2d.fill(rect);
}
}
public Dimension getPreferredSize() {
return new Dimension(300,300);
}
}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
panel2 = new java.awt.Panel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout panel2Layout = new javax.swing.GroupLayout(panel2);
panel2.setLayout(panel2Layout);
panel2Layout.setHorizontalGroup(
panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 309, Short.MAX_VALUE)
);
panel2Layout.setVerticalGroup(
panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 195, Short.MAX_VALUE)
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(56, 56, 56)
.addComponent(panel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(86, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(53, 53, 53)
.addComponent(panel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(118, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
// Variables declaration - do not modify
private java.awt.Panel panel2;
// End of variables declaration
мы можем закрасить часть фигуры одним цветом и часть другим?
Решение задачи: «Создать прямоугольник, залитый градиентом»
textual
Листинг программы
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author MD
*/
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.util.TimerTask;
import java.util.Timer;
//import java.awt.event.ActionListener;
public class DrawSquare extends javax.swing.JFrame {
public static float i=0;
private long t = System.nanoTime();
private static Timer mTimer;
public static DPanel dPanel;
public DrawSquare(String title) throws HeadlessException {
super(title);
dPanel = new DPanel();
getContentPane().add(dPanel);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();}
/**
* Creates new form DrawSquare
* @param args
*/
public static void main(String[] args) {
DrawSquare ds = new DrawSquare("MD4");
mTimer = new Timer();
MyTimerTask mMyTimerTask = new MyTimerTask(dPanel);
mTimer.schedule(mMyTimerTask, 1000,1);
ds.setVisible(true);
}
public Dimension getPreferredSize() {
return new Dimension(300,300);
}
}
class DPanel extends JPanel {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g ;
int index=0;
for (float i=0; i < 1; i +=0.01) {
g2d.setColor(new Color(Color.HSBtoRGB(DrawSquare.i, 1, 1)));
Rectangle2D rect = (new Rectangle2D.Double(index , 9, 1, 99));
g2d.fill(rect);
index++;
}
}
}
class MyTimerTask extends TimerTask {
private DPanel dPanel;
public MyTimerTask(DPanel dPanel)
{
this.dPanel=dPanel;
}
@Override
public void run() {
int index=0;
Graphics g=dPanel.getGraphics();
Graphics2D g2d = (Graphics2D) g ;
for (float i=0; i < 1; i +=0.01) {
g2d.setColor(new Color(Color.HSBtoRGB(DrawSquare.i, 1, 1)));
Rectangle2D rect = (new Rectangle2D.Double(index , 9, 1, 99));
g2d.fill(rect);
index++;
// }*/
System.out.println(DrawSquare.i+=0.01);
// dPanel.repaint();
//g2d.fill(rect)i+=0.01;
}
}}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
panel2 = new java.awt.Panel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout panel2Layout = new javax.swing.GroupLayout(panel2);
panel2.setLayout(panel2Layout);
panel2Layout.setHorizontalGroup(
panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 252, Short.MAX_VALUE)
);
panel2Layout.setVerticalGroup(
panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 195, Short.MAX_VALUE)
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(56, 56, 56)
.addComponent(panel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(143, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(53, 53, 53)
.addComponent(panel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(118, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
// Variables declaration - do not modify
private java.awt.Panel panel2;
// End of variables declaration