首页 \ 问答 \ 如何改变GradientPaint的方向?(How to change the direction of a GradientPaint?)

如何改变GradientPaint的方向?(How to change the direction of a GradientPaint?)

我想绘制一个从红色变为蓝色的渐变矩形。 我有以下代码:

g2d.setPaint(new GradientPaint(0, 0, Color.RED, 1000, 1000, Color.BLUE));
g2d.fillRect(0, 0, 1000, 1000); 

这很有效。 然而,梯度的方向是对角的,从矩形的左上角点到右下角的点(另一种方法是看渐变线跟随y=-x + windowHeight的线)

我希望我的渐变从上到下。 所以矩形的整个顶部是红色的,整个底部是蓝色的。 换句话说,颜色应该只随y坐标变化,给定任何y =点线,颜色在该线上应该是均匀的。

我已经包含了以下图片,以提供我想要做的事情的一般概念: 在这里输入图像描述

我怎么能做到这一点?

在这里输入图像描述


I am trying to draw a gradient rectangle that goes from red to blue. I have the following code:

g2d.setPaint(new GradientPaint(0, 0, Color.RED, 1000, 1000, Color.BLUE));
g2d.fillRect(0, 0, 1000, 1000); 

This is working. However, the direction of the gradient is diagonal, from the top left point of the rectangle to the bottom right point (another way to look at this is that the gradient follows the line of y=-x + windowHeight)

I would like my gradient to go from top to bottom. So the entire top of the rectangle is red, and the entire bottom is blue. In other words, the color should only change with the y coordinate, given any y=point line the color should be uniform across that line.

I have included the following images also to give a general idea of what I am trying to do: enter image description here

How can I accomplish this?

enter image description here


原文:https://stackoverflow.com/questions/35052826
更新时间:2024-05-03 20:05

最满意答案

如果我理解正确,给position:relative对于容器/ wrap divpointer-events:none包含你可以实现的文本的元素

片段

body {
  margin: 0 /* demo */
}
.wrap {
  position: relative;
}
img {
  width: 108px;
  height: 32px;
  z-index: -1;
  position: relative
}
a:hover img {
  opacity: .5  /* demo */
}
span {
  position: absolute;
  left: 0;
  pointer-events: none;
  display: inline-block; /* demo */
  background: red /* demo */
}
<div class="wrap">
  <a href="#">
    <img src="//lorempixel.com/1600/900">
  </a>
  <span>some text</span>
</div>


I rearranged the website layout to use a clickable div instead of an image, and now it works on click even if text is over it. I then put it in a nested div which is the size of the entire window, so the links never get misaligned based on window size:

HTML:

<div id="bodyDiv">
   <div class="secret" style="position:relative; top:770px; left:1168px; width:108px; height:32px; background-color:black;" onclick="location.href='/aboutme/vision.html'"></div>
</div>

CSS:

#bodyDiv {
        background-image: url("homebackgroundsmall.png");
        background-repeat: no-repeat;
        background-position: center;
        height: 1200px;
        width: 1684px;
        position: absolute;
        bottom: 0;
        left: 50%;
        margin-left: -842px;
      }

The downside to this is that there is no mouse change on hover, making the secrets overly hidden. To personally fix this to make it more obvious, I made the .secret class so on hover the background portion of the div fades out by increasing the background opacity to 1 (which is black):

CSS:

.secret {
        opacity: 0;
        transition: opacity 1s ease-in-out;
        -moz-transition: opacity 1s ease-in-out;
        -webkit-transition: opacity 1s ease-in-out;
      }
.secret:hover {
        opacity: 1;
      }

相关问答

更多
  • 您没有通过在视图中编写css来使用rails资产管道。 如果你看一下它说的文档 Asset pipeline concatenate assets, which can reduce the number of requests that a browser makes to render a web page. Web browsers are limited in the number of requests that they can make in parallel, so fewer reques ...
  • 如果您相对定位包含div,然后绝对定位文本元素(h1),您可以使用左上角的右下角定位将文本放在相对于包含div的位置。 如果您需要更多细节,请询问。 If you relatively position your containing div, then absolutely position the text element (h1), you can use top left right bottom positioning to put the text where you like relative ...
  • text-indent没有解决IE中的问题。 因此,我不得不将图像移出文本字段并将其包装在div内:) text-indent didn't fix the problem in IE. Hence I had to move the image out of text-field and wrap that inside a div :)
  • 由于您没有使用重置或默认样式清除样式表,因此您还需要设置background-color属性。 您可以将其设置为transparent 。 Since you are not using a resetting or default style clearing stylesheet you'll need to set the background-color property as well. You can set it to transparent.
  • Bootstrap在代码body { background-color: #fff; }的前几行中设置body的css如下body { background-color: #fff; } body { background-color: #fff; } 。 我对此的建议是进入你的bootstrap.css文件并删除该css。 如果您使用的是cdn,则可以在将到bootstrap css之后添加以下内容: